New Zenner
- Join Date:
- Jan 2005
- Posts:
- 38
- Plugin Contributions:
- 0
Switching templates at run time
Thanks a lot
Views: 21,490
New Zenner
Thanks a lot
New Zenner
I know this is an old post, but was wondering if anyone could explain exactly what the code in post #36 and #5 actually mean. I.E. - Where do you put which cPath you want the template to change for and where to put which template you want for the given cPath? In one post, it seems like $template_dir is equal to the cPath ($template_dir = "29";) and in another it seems like this is equal to the template name ($template_dir = "mens";).
Can anyone provide some help on this?
I have tried the code and it seems to only work when I use the form like $template_dir = "29"; but put my desired cPath in for 29. However, I feel like when I do this, zen cart just using the template_default settings and not the setting of my custom template.
Any help is much appreciated. Thank you in advance!
Zen Follower
I wont lie about this: the whole template switching idea seemed great at first, but became messy to manage over time. As time has gone by, I have favored edits to the stylesheets to get what I need. (just a little advice for someone starting out who wants to save time)
anyhow:
If your cPath is '3', for example, you would use:
case $template_dir = "3";
```Now you need an actual directory to hold those files. So, in you includes/templates folder, you have "template_default", "your_custom_directory" and a directory called "3".
If you wanted to override a file such as tpl_main_page.php you would need to put it in the templates/3/common/ directory.
I'm not sure how post #5 was supposed to work because to get the cPath ```
$_GET['cPath']
```, you would need to get a number, not a category name.
New Zenner
Thanks for your reply and advice!
Since all I actually needed was to change how product_listing.php displayed products for different categories, I just ended up adding a bunch of custom IF statements around the areas I wanted to show up for some categories. This way I don't have to worry about all of the template mess; though, my solution is not the cleanest, oh well.
Thanks again for the quick reply! :smile:
Zen Follower
edon12:
I just ended up adding a bunch of custom IF statements around the areas I wanted to show up for some categories. :smile:
That is great, your more advanced than I was guessing (its hard to tell where someone is at)
I'm glad you choose your own solution, as the template solution just really ended up being a bit of a mess for me.
Black Belt
Note - for those who want to use the switch/case method, the syntax is
switch($_GET['cPath']) { //test for a matching string
case "29"; // if current cPath is 29 set the appropriate template
$template_dir = "29"; // directory name of template (includes/templates/29)
break;
}
Post 5 used the cPath number in the case, and then set the name accordingly.
Black Belt
Arrgh... lost my original comment in there, which was that the case doesn't get a semicolon, but a colon.
case "29":
Zen Follower
gjh42:
the syntax is
switch($_GET['cPath']) { //test for a matching string
case "29"; // if current cPath is 29 set the appropriate template
$template_dir = "29"; // directory name of template (includes/templates/29)
break;
}
o.k., now I see how that works. Have you noted whether this has worked well/consistently? The way I had it set up (post #36) would work sporadically, probably because the switch/case statement was incorrect.
Also, do you know if this will load the custom-template directory as well, or does it just knock that one aside and load just the cPath-template?
Black Belt
I haven't used this particular switch, but if built correctly it will always work. There are some subtleties to switches which can make unexpected differences. Some incorrectly built switches will always come out true for the first case, some will never be true...
I expect that just the cPath-template would be loaded for a category that matched a case, but have not studied the whole file to understand its behavior.
New Zenner
Thanks guys, this looks good.
I want to be able to switch entire site templates depending upon the language selected from a list of 3 in the Languages side-box.
Is this the same as coded above?
I haven't had a chance to look closely yet. Been at this all day. Just saw the post.
Black Belt
You could use a switch like this:```php
switch($_SESSION['language']){
case 'english':
$template_dir = 'english_template';
break;
case 'french':
$template_dir = 'french_template';
break;
case 'portugues':
$template_dir = 'portugues_template';
break;
}
New Zenner
Thanks, yes, that looks simple to implement, but does it only switch for the user who selects the language, or does it switch for all users.
If I connect and switch to French, and then Bill connects, does he see French? or the default English.
Thanks for your help, can you point me at what file that switch actually goes in to achieve what I want please. I'm just starting with this language switching thing, although I am familiar with PHP. I do have the messages from this thread too.
thanks
Black Belt
This would be a switch that would operate at run-time, and only for the individual user.
As for the file, I haven't looked at the code involved in a long time, so your best bet is to examine it for yourself.
Totally Zenned
Guys, you're over-complicating this. Templates by language is already built into Zen Cart.
If you go to Admin > Tools > Template Select there's a new language button. Click on that and you can select specific templates for each language that you have installed.
Kuroi Web Design and Development | Twitter
(Questions answered in the forum only - so that any forum member can benefit - not by personal message)
Black Belt
One of the downsides of only using English... never had to look at language selections:)
Totally Zenned
If it's any consolation, I don't recall ever having seen this mentioned before in this forum, and it gets just a one line reference in "the book".
Kuroi Web Design and Development | Twitter
(Questions answered in the forum only - so that any forum member can benefit - not by personal message)
New Zenner
ah. Well the problem I am seeing is that the new template has different layouts for the side boxes etc. Changing languages, and changing templates fails because the side-boxes no longer match.
The template I want to change to is completly different to the 'standard' template, but has to completly replace each other depending on language switch.
I don't think it can be don, unless there is a switch also in there to 'reset' the layout.
Totally Zenned
There's no a switch, but each template can have its own arrangement of layout boxes. You have to have the template you want to change as the default template, and then go to Admin > Tools > Layout Boxes controller and set the layout boxes for that template. When you've finished, you'll probably want to set the default template back to what it was originally.
Kuroi Web Design and Development | Twitter
(Questions answered in the forum only - so that any forum member can benefit - not by personal message)
Zen Follower
or you can use this contribution to safe your current template sideboxes as default and just update your other templates with a single click.
Louis - TechnoPro - Optics Online,Hunters Corner,Anything Outdoor
Proudly Zen-Cart 1.5.7c
Totally Zenned
I considered recommending that mod (it was written and submitted by my wife) but, useful though it is, it's really designed for when people want to use the same layout box configuration across several templates, rather than a different layout for each template.
Kuroi Web Design and Development | Twitter
(Questions answered in the forum only - so that any forum member can benefit - not by personal message)
Tell staff why this post should be reviewed.