Page 1 of 6 123 ... LastLast
Results 1 to 10 of 60
  1. #1
    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):

    Code:
    $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:

    Code:
    $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:

    Code:
    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.
    Last edited by eccentric; 20 Jul 2006 at 08:16 AM.

  2. #2
    Join Date
    Jul 2006
    Location
    Wiltshire, England
    Posts
    69
    Plugin Contributions
    0

    Default 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):

    Code:
                 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 d:

    Hope it helps
    Mark

  3. #3
    Join Date
    Jul 2006
    Posts
    31
    Plugin Contributions
    0

    Default 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 (:

  4. #4
    Join Date
    Jul 2006
    Posts
    31
    Plugin Contributions
    0

    Default 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.

    Code:
    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 (:

  5. #5
    Join Date
    Jul 2006
    Posts
    31
    Plugin Contributions
    0

    Default 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:

    Code:
      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.

  6. #6
    Join Date
    Jan 2004
    Posts
    66,444
    Plugin Contributions
    279

    Default Re: Switching templates at run time

    Quote Originally Posted by eccentric View Post
    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.

  7. #7
    Join Date
    Jul 2006
    Posts
    31
    Plugin Contributions
    0

    Default 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.

  8. #8
    Join Date
    Jan 2004
    Posts
    66,444
    Plugin Contributions
    279

    Default Re: Switching templates at run time

    Quote Originally Posted by eccentric View Post
    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.

  9. #9
    Join Date
    Jul 2006
    Posts
    31
    Plugin Contributions
    0

    Default Re: Switching templates at run time

    Quote Originally Posted by DrByte View Post
    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.

  10. #10
    Join Date
    Nov 2005
    Location
    Colorado Springs, CO USA
    Posts
    7,033
    Plugin Contributions
    31

    Default 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.

 

 
Page 1 of 6 123 ... LastLast

Similar Threads

  1. switching templates
    By RichardH in forum Templates, Stylesheets, Page Layout
    Replies: 2
    Last Post: 23 Jul 2008, 10:36 PM
  2. Switching php version in WAMP5 causes install to run
    By GreggShort in forum Installing on a Windows Server
    Replies: 2
    Last Post: 27 Jul 2007, 06:06 AM
  3. switching between templates
    By fairview in forum General Questions
    Replies: 4
    Last Post: 9 Apr 2007, 02:44 PM
  4. switching templates
    By onakat in forum Customization from the Admin
    Replies: 6
    Last Post: 3 Apr 2007, 10:18 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
disjunctive-egg