Page 1 of 1
Dynamically Change a Page's Template Association
Posted: Thu Apr 05, 2012 9:41 pm
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.
Re: Dynamically Change a Page's Template Association
Posted: Thu Apr 05, 2012 9:48 pm
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.
Re: Dynamically Change a Page's Template Association
Posted: Thu Apr 05, 2012 10:49 pm
by Dr.CSS
You could use CSS to do this...
@media screen and (max-width: 650px) {
Re: Dynamically Change a Page's Template Association
Posted: Thu Apr 05, 2012 10:53 pm
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.
Re: Dynamically Change a Page's Template Association
Posted: Fri Apr 06, 2012 6:17 am
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
Re: Dynamically Change a Page's Template Association
Posted: Mon Apr 09, 2012 4:48 pm
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.
Re: Dynamically Change a Page's Template Association
Posted: Mon Apr 09, 2012 8:24 pm
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.
Re: Dynamically Change a Page's Template Association
Posted: Mon Apr 09, 2012 8:30 pm
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.
Re: Dynamically Change a Page's Template Association
Posted: Mon Apr 09, 2012 9:12 pm
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...
Re: Dynamically Change a Page's Template Association
Posted: Tue Apr 10, 2012 8:01 am
by mcDavid
or just tell your client he can edit the templates in GCB's