Dynamically Change a Page's Template Association

Talk about writing modules and plugins for CMS Made Simple, or about specific core functionality. This board is for PHP programmers that are contributing to CMSMS not for site developers
Post Reply
paul
Forum Members
Forum Members
Posts: 21
Joined: Tue Feb 16, 2010 9:54 pm

Dynamically Change a Page's Template Association

Post by paul »

I want to be able to change the template associated with a page, based on certain parameters.

For example.

I have a global PHP variable that hold either "Desktop" or "Mobile". I want to display a page of content using the template "HomeDesktop" (by default) and "HomeMobile" if the variable is "Mobile".

I assume this is easy, but I don't know what I should be looking for in the documentation and forums.

Thanks,
Paul.
paul
Forum Members
Forum Members
Posts: 21
Joined: Tue Feb 16, 2010 9:54 pm

Re: Dynamically Change a Page's Template Association

Post by paul »

What I really need is the variable name that holds the reference to the page's current template.

Then I will be able to change the value myself with some custom PHP code.

Thanks,
Paul.
User avatar
Dr.CSS
Moderator
Moderator
Posts: 12711
Joined: Thu Mar 09, 2006 5:32 am
Location: Arizona

Re: Dynamically Change a Page's Template Association

Post by Dr.CSS »

You could use CSS to do this...

@media screen and (max-width: 650px) {
paul
Forum Members
Forum Members
Posts: 21
Joined: Tue Feb 16, 2010 9:54 pm

Re: Dynamically Change a Page's Template Association

Post by paul »

Thankyou for your reply.

But using media queries will not solve the problem. We need to change the template that "surrounds" the content.
uniqu3

Re: Dynamically Change a Page's Template Association

Post by uniqu3 »

Use one template, check for a variable like $mobile and switch content of template itself.

It's an old post but you might get the idea:
http://www.i-do-this.com/blog/14/Make-y ... bile-ready
paul
Forum Members
Forum Members
Posts: 21
Joined: Tue Feb 16, 2010 9:54 pm

Re: Dynamically Change a Page's Template Association

Post by paul »

Thank you uniqu3. I actually already have a script I wrote that returns a global variable $Platform, which is either "Mobile", "Facebook" or "Desktop".

What I need to figure out is how to change the template (template_id) of the page object before the rendering is done.

You'll have to excuse me but I am new to this PHP stuff, I come from an extensive .NET background.

Thanks,
Paul.
mcDavid
Power Poster
Power Poster
Posts: 377
Joined: Tue Mar 31, 2009 8:45 pm
Location: Delft, Netherlands

Re: Dynamically Change a Page's Template Association

Post by mcDavid »

Don't change the template or anything. Just make ONE template, that contains both the mobile and desktop template and use an {if} statement to show either the one or the other.

If you want to keep everything tidy you might want to make two content blocks containing the "templates" and just load the right content block into your single main template.
paul
Forum Members
Forum Members
Posts: 21
Joined: Tue Feb 16, 2010 9:54 pm

Re: Dynamically Change a Page's Template Association

Post by paul »

Thank you McDavid, but the client is will be adding new templates and content to the exisiting site. We don't trust them enough to use Smary logic.

What I want to do is make my own module that they can use which just keeps a list of template pairs, one for desktop and one for mobile.

Thanks,
Paul.
User avatar
Jo Morg
Dev Team Member
Dev Team Member
Posts: 1936
Joined: Mon Jan 29, 2007 4:47 pm

Re: Dynamically Change a Page's Template Association

Post by Jo Morg »

paul wrote:What I want to do is make my own module that they can use which just keeps a list of template pairs, one for desktop and one for mobile.
Well... most modules use their own templates/template db if their contents may potentially need any kind of formatting. If you take a look at News, CGBlog, Gallery, etc... (there is a huge number of modules that produce contents) you will see that a module can manage its own templates.
On the other hand I'm not sure if that is the best solution...
However... you could use Smarty logic on the Main template (restricted to you, the developer) and call your module templates with a tag such as {yourmodule template='selectedtemplate'}...
This tag could be called inside {if} logic...
Just an idea though...
"There are 10 types of people in this world, those who understand binary... and those who don't."
* by the way: English is NOT my native language (sorry for any mistakes...).
Code of Condut | CMSMS Docs | Help Support CMSMS
My developer Page on the Forge
GeekMoot 2015 in Ghent, Belgium: I was there!
GeekMoot 2016 in Leicester, UK: I was there!
DevMoot 2023 in Cynwyd, Wales: I was there!
mcDavid
Power Poster
Power Poster
Posts: 377
Joined: Tue Mar 31, 2009 8:45 pm
Location: Delft, Netherlands

Re: Dynamically Change a Page's Template Association

Post by mcDavid »

or just tell your client he can edit the templates in GCB's
Post Reply

Return to “Developers Discussion”