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

Switching templates at run time

Locked

Views: 21,490

Results 1 to 20 of 60
This thread is locked. New replies are disabled.
20 Jul 2006, 7:11 AM
#1
eccentric avatar

eccentric

New Zenner

Join Date:
Jul 2006
Posts:
31
Plugin Contributions:
0

Switching templates at run time

G'day,

I'm in a situation whereby I need to be able to load a different template, depending on where the visitor is in the site. Example: the visitor is viewing the category "Men's Clothing," - I want the template in '/includes/templates/mens' to be loaded. When a visitor is viewing "Women's Clothing," - I want the template in '/includes/templates/womens' to be loaded.

I have found the line in 'admin/template_select.php' that is responsible for updating/switching the template that is used the site (line 52):

$db->Execute("update " . TABLE_TEMPLATE_SELECT . " set template_dir = '" . $_POST['ln'] . "' where template_id = '" . $_GET['tID'] . "'");

So, theoretically, I should be able to execute this query replacing the template_dir and template_id with whatever template I want to use - based on what category/section of the site the visitor is viewing. To do this all that would be needed is something like a variable stored in $_GET that has the template_dir. Let's say I know the template_id is 1. The query would be something like:

$db->Execute("update " . TABLE_TEMPLATE_SELECT . " set template_dir = '" . $_GET['template'] . "' where template_id = '1'");

This piece of code works if placed anywhere after line 111 of 'includes/application_top.php', which reads:

require('includes/autoload_func.php');

But it will not work if placed before that line, I imagine because this line is responsible for running 'autoload_func.php' which in turn initiates all the classes and objects used throughout the site (at least this is my understanding).

At present, the code works, only it will only load the new template after you have refreshed the page more than once (or viewed 2 pages with the same $_GET['template']). This is because the first time the page is run, it updates the template with the query. The second time the page is run, the template has already been updated so you see the new template.

So, I need someone who has some advanced knowledge of Zen Cart to give me a pointer here. How can I run a query that will switch templates as the page is generated?

Many thanks.

20 Jul 2006, 11:26 AM
#2
markrs avatar

markrs

New Zenner

Join Date:
Jul 2006
Location:
Wiltshire, England
Posts:
69
Plugin Contributions:
0

Re: Switching templates at run time

I'm not sure I can claim advanced knowledge, but I've been fiddling with stylesheets too, and in the css directories of the templates (eg includes/templates/template_default/css) there's a file css_readme.txt that suggests you can set up templates for all sorts of things. Here's what it says

The CSS files are sent to the browser in this order: (and alphabetically within each case of more than one match):

             style*.css   // are always loaded and at least ONE should contain site-wide properties.
language_stylesheet.css   // changes to ALL pages, when that language is used
          page_name.css   // changes to one page 
 language_page_name.css   // changes to one page, when that language is used
            c_??_??.css   // changes to all info pages in a category
   language_c_??_??.css   // changes to all info pages in a category, when that language is used
               m_??.css   // changes to a manufacturer's listing page
      language_m_??.css   // changes to a manufacturer's listing page, when that language is used
               p_??.css   // changes to a product's info page
      language_p_??.css   // changes to a product's info page, when that language is used
             print*.css   // printer-friendly global usage site-wide changes for printing-only

Now, setting up categories seems to be a bit of an issue. I've gone some of the way to sorting out a (manual) solution, but it (product categories) seems like such a sensible idea it's too good to miss out on. It's just a bit more manual than it might be at the moment :lookaround:

Hope it helps
Mark

20 Jul 2006, 11:16 PM
#3
eccentric avatar

eccentric

New Zenner

Join Date:
Jul 2006
Posts:
31
Plugin Contributions:
0

Re: Switching templates at run time

Hmm okay, so it looks like you can apply different stylesheets to different categories quite easily. But what if you want to have different HTML/layouts for different categories? CSS can't do that. I think that's why I need to able to switch entire templates.

I'm close to finding a sketchy solution using the query I posted above, but as long as it works I don't really care (:

20 Jul 2006, 11:43 PM
#4
eccentric avatar

eccentric

New Zenner

Join Date:
Jul 2006
Posts:
31
Plugin Contributions:
0

Re: Switching templates at run time

Righto, here's my (temporary) solution. Placed at the beginning of index.php.

The variables $zf_host, $zf_user, $zf_password and $db_name represent the MySQL host, username, password and database name.

switch($_GET['cPath']) {
case 1: // Men's Clothing category
  $template = "mens"; // directory name of Men's Clothing template (includes/templates/mens)
  break;
case 2: // Women's Clothing category
  $template = "womens"; // directory name of Women's Clothing template (includes/templates/womens)
  break;
default:
  $template = "default"; // directory name of default template (includes/templates/default)
  break;
}

if($_GET['cPath']) {
  $link = mysql_connect($zf_host, $zf_user, $zf_password, true);
  if (!$link) {
    die('Could not connect: ' . mysql_error());
  } else {
    mysql_select_db($db_name);
  }  
  $query = "UPDATE zen_template_select SET template_dir = '" . $template . "' WHERE template_id = '1'";
  $result = mysql_query($query);
  mysql_close($link);
} 

...Oh dear. I just realised that this actually switches the template for every viewer - not on a per viewer basis. That's not what I want. Oh well! back to the drawing board (:

21 Jul 2006, 12:07 AM
#5
eccentric avatar

eccentric

New Zenner

Join Date:
Jul 2006
Posts:
31
Plugin Contributions:
0

Re: Switching templates at run time

Okay, got it! The final solution. The file that is responsible for selecting the template is 'includes/init_includes/init_templates.php'. By placing this code (and modifying it to have your own template directories, adding more cases, etc.), you can load a different template based on which category a viewer is browsing. It should go on Line 37:

  if($_GET['cPath']) {
    switch($_GET['cPath']) {
    case 1: // Men's Clothing
      $template_dir = "mens"; // directory name of Men's Clothing template (includes/templates/mens)
      break;
    case 2: // Women's Clothing
      $template_dir = "womens"; // directory name of Women's Clothing template (includes/templates/womens)
      break;
    default: // If none of the above
      $template_dir = "default"; // directory name of default template (includes/templates/default)
      break;
    }
  }

I think this should be a standard feature.

28 Oct 2006, 6:02 PM
#6
drbyte avatar

drbyte

Sensei

Join Date:
Jan 2004
Posts:
63,513
Plugin Contributions:
176

Re: Switching templates at run time

eccentric:

I think this should be a standard feature.

As of v1.3.5, you can call it 'index_home.css' and it will only affect the "home" page.

.
Zen Cart - putting the dream of business ownership within reach of anyone!
Donate to: DrByte directly or to the Zen Cart team as a whole

Remember: Any code suggestions you see here are merely suggestions. You assume full responsibility for your use of any such suggestions, including any impact ANY alterations you make to your site may have on your PCI compliance.
Furthermore, any advice you see here about PCI matters is merely an opinion, and should not be relied upon as "official". Official PCI information should be obtained from the PCI Security Council directly or from one of their authorized Assessors.

2 Nov 2006, 10:59 PM
#7
eccentric avatar

eccentric

New Zenner

Join Date:
Jul 2006
Posts:
31
Plugin Contributions:
0

Re: Switching templates at run time

Okay that's cool, thanks for the info. But my idea was to allow entire templates to be applied to categories, not just css files, ie. the template in 'includes/templates/pants' is loaded whenever someone is browsing the 'pants' category. When someone goes to view a product in the 'shirts' category the template in 'includes/templates/shirts' is loaded. It's actually not difficult to implement at all, using the code above.

2 Nov 2006, 11:33 PM
#8
drbyte avatar

drbyte

Sensei

Join Date:
Jan 2004
Posts:
63,513
Plugin Contributions:
176

Re: Switching templates at run time

eccentric:

It's actually not difficult to implement at all, using the code above.

True, as long as one is willing to manually code it for each individual category.

.
Zen Cart - putting the dream of business ownership within reach of anyone!
Donate to: DrByte directly or to the Zen Cart team as a whole

Remember: Any code suggestions you see here are merely suggestions. You assume full responsibility for your use of any such suggestions, including any impact ANY alterations you make to your site may have on your PCI compliance.
Furthermore, any advice you see here about PCI matters is merely an opinion, and should not be relied upon as "official". Official PCI information should be obtained from the PCI Security Council directly or from one of their authorized Assessors.

3 Nov 2006, 12:24 AM
#9
eccentric avatar

eccentric

New Zenner

Join Date:
Jul 2006
Posts:
31
Plugin Contributions:
0

Re: Switching templates at run time

DrByte:

True, as long as one is willing to manually code it for each individual category.

I don't think it'd be too difficult (although somewhat time consuming) to write a mod to achieve the same thing though. Maybe one day I'll get around to that.

3 Nov 2006, 12:39 AM
#10
clydejones avatar

clydejones

Deceased

Join Date:
Nov 2005
Location:
Colorado Springs, CO USA
Posts:
7,017
Plugin Contributions:
12

Re: Switching templates at run time

Why not do a stylesheet for the category call it c_4.css
Where 4 would be the category for "shirts"

the stylesheet would consist of declarations used only by that category.

You could change the header_bg image and any thing else to make that page unique.

The stylesheet would only be called when that category page is displayed.

doing this saves messing with the core code.

NOTE: We are sorry that Clyde is no longer with us.
We are grateful for all his contributions to the Zen Cart community.

3 Nov 2006, 12:54 AM
#11
eccentric avatar

eccentric

New Zenner

Join Date:
Jul 2006
Posts:
31
Plugin Contributions:
0

Re: Switching templates at run time

Another way of switching templates at run time could work like this (a tad sketchy, but placing the code as I suggested earlier in includes/init_includes/init_templates.php) - there could be several ways to do this but this is just one:

// template directory/folder name = array of category ids (cPath) that are to use that template
// also set the template directory names
$mens = array(1,2,4,5,8);
$mens['template_dir'] = 'mens';
$womens = array(3,6,7,9);
$womens['template_dir'] = 'womens';
$kids = array(10,11,12);
$kids['template_dir'] = 'kids';

// put all the templates listed above together in an array
$templates = array($mens, $womens, $kids);

// find the appropriate template to use
// loop through all the template dirs
for($i=0; $i < count($templates); $i++) {
  if($template_dir != '') {
    break;
  }
  // loop through all the category ids (cPath)
  for($i2=0; $i2 < count($templates[$i]); $i2++) {
    // if the current cPath we're looking at matches the cPath that the visitor is browsing, set the current template we're looking at as the template to use
    if($templates[$i][$i2] == $_GET['cPath']) {
      $template_dir = $templates[$i]['template_dir'];
      break;
    }
  }
}
24 Nov 2006, 12:18 AM
#12
eccentric avatar

eccentric

New Zenner

Join Date:
Jul 2006
Posts:
31
Plugin Contributions:
0

Re: Switching templates at run time

clydejones:

Why not do a stylesheet for the category call it c_4.css
Where 4 would be the category for "shirts"

the stylesheet would consist of declarations used only by that category.

You could change the header_bg image and any thing else to make that page unique.

The stylesheet would only be called when that category page is displayed.

doing this saves messing with the core code.

I guess it depends on the layout/structure of your site and how good you are with CSS. But sometimes you might want a completely different layout for a different category - not just a different banner, different fonts, different background, etc. For this reason I needed to be able to switch entire templates (folders).

1 Dec 2006, 7:14 PM
#13
gluckfs avatar

gluckfs

New Zenner

Join Date:
Nov 2006
Posts:
21
Plugin Contributions:
0

Re: Switching templates at run time

You mention the file

includes/init_includes/init_templates.php

as the file that has to be modified.

Being a great believer in the override system, where would I copy the original file to before I make any modifications?

1 Dec 2006, 7:25 PM
#14
kuroi avatar

kuroi

Totally Zenned

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

Re: Switching templates at run time

Your override file would go in includes/init_includes/overrides

Kuroi Web Design and Development | Twitter

(Questions answered in the forum only - so that any forum member can benefit - not by personal message)

3 Dec 2006, 2:31 AM
#15
gluckfs avatar

gluckfs

New Zenner

Join Date:
Nov 2006
Posts:
21
Plugin Contributions:
0

Re: Switching templates at run time

A clarification ... just to see if this solution will work for me...

I have two "categories".

Category #1 are items that needs to be displayed using one large photo. (displaying them using the standard row-based layout on the category page won't do).

The idea here is to click on the category "categ_1", then be taken to a page with a large image that is the first product in categ_1. To see the next product in categ_1, click on next (or previous). If you want to purchase the product, you click on the image and are taken to a product page.

The other type of category (categ_2) contains items that can be displayed in the normal way -- the category page contains 3 or 4 items per row. Click on one of the items and you are taken to a product page.

So.. the question is, how do I get a different layout for the category page for categ_1 and keep the default layout for the category page for categ_2?

14 Feb 2007, 4:33 PM
#16
raimond avatar

raimond

Zen Follower

Join Date:
Jul 2004
Location:
The Netherlands, Wierden
Posts:
423
Plugin Contributions:
0

Re: Switching templates at run time

clydejones:

Why not do a stylesheet for the category call it c_4.css
Where 4 would be the category for "shirts"

the stylesheet would consist of declarations used only by that category.

You could change the header_bg image and any thing else to make that page unique.

The stylesheet would only be called when that category page is displayed.

doing this saves messing with the core code.

This works fine, but it is only for that category and not for that category with his sub-categories and products.

Howe can i set this to al the products and sub-categories?

22 Mar 2007, 2:16 PM
#17
lawbird123 avatar

lawbird123

Zen Follower

Join Date:
Sep 2006
Location:
Jacksonville, FL
Posts:
231
Plugin Contributions:
0

Re: Switching templates at run time

This does not seem to be working for me....

  //change flash header for different categories
  
  if($_GET['cPath']) {
  
  	switch($_GET['cPath']) {
    
    case 1: // Video
    
      $template_dir = "jlawless_video"; // directory name of Men's Clothing template (includes/templates/mens)
    
      break;
    
    case 2: // Software
    
      $template_dir = "jlawless_software"; // directory name of Women's Clothing template (includes/templates/womens)
      
      break;
    
    default: // If none of the above
    
      $template_dir = "default"; // directory name of default template (includes/templates/jlawless)
      
      break;
   
     }
  
  }

ive copied, renamed my default template and changed out my flash header and nothing seems to be happening. is there something wrong with my code? im thinking i havent specified a cPath.... but im not sure. any help with this would be great!

John L.
MultiMedia Designer

22 Mar 2007, 2:32 PM
#18
kuroi avatar

kuroi

Totally Zenned

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

Re: Switching templates at run time

The code has some problems and may be simply being ignored.

For the syntax I would recommend using if(isset($_GET['cPath'])), and switch((int)$_GET['cPath']).

The logic may then be slightly off. Is it your intention to only execute the switch when you are on the index pages and do you have alternative code to set $template_dir when you're not?

Finally, using integer categories will only work for top level categories. Sub category paths are passed in the GET defined as strings showing the category hierarchy (e.g. "2_19" for subcategory 19 in top category 2) and will break your approach if you use them.

Kuroi Web Design and Development | Twitter

(Questions answered in the forum only - so that any forum member can benefit - not by personal message)

22 Mar 2007, 2:41 PM
#19
lawbird123 avatar

lawbird123

Zen Follower

Join Date:
Sep 2006
Location:
Jacksonville, FL
Posts:
231
Plugin Contributions:
0

Re: Switching templates at run time

I have a flash header that i would like to switch when a new category is chosen. If someone clicks on video category the video swf starts. I would like it to work with the subcategories of the video category as well... is this possible? Im having probs just getting it to work with the main cat at the moment, so first things first i guess.

John L.
MultiMedia Designer

22 Mar 2007, 3:00 PM
#20
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 only the flash header that changes, why switch the whole template? It would be easier to put the switch in where you have embedded the call to the flash file.

You haven't said whether you want different flash files for the subcategories or simply for the subcategories to also display the file appropriate to their parent category (ironically the later is marginally more difficult!)

Kuroi Web Design and Development | Twitter

(Questions answered in the forum only - so that any forum member can benefit - not by personal message)