Forums / Templates, Stylesheets, Page Layout / Switching templates at run time

Switching templates at run time

Locked

Views: 21,490

Results 41 to 60 of 60
This thread is locked. New replies are disabled.
27 Dec 2007, 1:04 PM
#41
soundworker avatar

soundworker

New Zenner

Join Date:
Jan 2005
Posts:
38
Plugin Contributions:
0

Switching templates at run time

Thanks a lot

10 Jun 2009, 4:47 PM
#42
edon12 avatar

edon12

New Zenner

Join Date:
Apr 2009
Posts:
9
Plugin Contributions:
0

Re: Switching templates at run time

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!

10 Jun 2009, 6:55 PM
#43
rainthebat avatar

rainthebat

Zen Follower

Join Date:
Feb 2007
Posts:
314
Plugin Contributions:
0

Re: Switching templates at run time

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.
10 Jun 2009, 7:00 PM
#44
edon12 avatar

edon12

New Zenner

Join Date:
Apr 2009
Posts:
9
Plugin Contributions:
0

Re: Switching templates at run time

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:

10 Jun 2009, 7:16 PM
#45
rainthebat avatar

rainthebat

Zen Follower

Join Date:
Feb 2007
Posts:
314
Plugin Contributions:
0

Re: Switching templates at run time

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.

11 Jun 2009, 12:15 PM
#46
gjh42 avatar

gjh42

Black Belt

Join Date:
Jul 2005
Location:
Upstate NY
Posts:
21,876
Plugin Contributions:
8

Re: Switching templates at run time

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.

11 Jun 2009, 1:51 PM
#48
rainthebat avatar

rainthebat

Zen Follower

Join Date:
Feb 2007
Posts:
314
Plugin Contributions:
0

Re: Switching templates at run time

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?
13 Jun 2009, 6:24 AM
#49
gjh42 avatar

gjh42

Black Belt

Join Date:
Jul 2005
Location:
Upstate NY
Posts:
21,876
Plugin Contributions:
8

Re: Switching templates at run time

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.

9 Jan 2010, 9:49 AM
#50
rachalmers avatar

rachalmers

New Zenner

Join Date:
Mar 2008
Posts:
55
Plugin Contributions:
0

Re: Switching templates at run time

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.

9 Jan 2010, 3:11 PM
#51
gjh42 avatar

gjh42

Black Belt

Join Date:
Jul 2005
Location:
Upstate NY
Posts:
21,876
Plugin Contributions:
8

Re: Switching templates at run time

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;
}

9 Jan 2010, 9:39 PM
#52
rachalmers avatar

rachalmers

New Zenner

Join Date:
Mar 2008
Posts:
55
Plugin Contributions:
0

Re: Switching templates at run time

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

9 Jan 2010, 10:25 PM
#53
gjh42 avatar

gjh42

Black Belt

Join Date:
Jul 2005
Location:
Upstate NY
Posts:
21,876
Plugin Contributions:
8

Re: Switching templates at run time

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.

10 Jan 2010, 12:14 AM
#54
kuroi avatar

kuroi

Totally Zenned

Join Date:
Apr 2006
Location:
London, UK
Posts:
10,475
Plugin Contributions:
11

Re: Switching templates at run time

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)

10 Jan 2010, 12:51 AM
#56
kuroi avatar

kuroi

Totally Zenned

Join Date:
Apr 2006
Location:
London, UK
Posts:
10,475
Plugin Contributions:
11

Re: Switching templates at run time

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)

10 Jan 2010, 8:25 AM
#57
rachalmers avatar

rachalmers

New Zenner

Join Date:
Mar 2008
Posts:
55
Plugin Contributions:
0

Re: Switching templates at run time

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.

10 Jan 2010, 1:03 PM
#58
kuroi avatar

kuroi

Totally Zenned

Join Date:
Apr 2006
Location:
London, UK
Posts:
10,475
Plugin Contributions:
11

Re: Switching templates at run time

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)

10 Jan 2010, 5:15 PM
#60
kuroi avatar

kuroi

Totally Zenned

Join Date:
Apr 2006
Location:
London, UK
Posts:
10,475
Plugin Contributions:
11

Re: Switching templates at run time

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)