Question

I am currently learning template variables and trying to understand how they work and what they mean. I've done a test on {$category->id_cms_category}, which I put in cms.tpl and I get a result 9, but when I put this in header.tpl or blockcms.tpl (left column), there is no results, it's blank.

Can somebody please explain how this works and how I can get the same result in different .tpl file? I think the question is really how to assign $category class to for example header.tpl. Is it something to do with controllers? Why can't I use certain variables everywhere? How does this work? I would be very happy if somebody explain this. I am also still learning smarty.

Was it helpful?

Solution

Unfortunately you're hitting a common problem with smarty, and particularly how it's implemented within Prestashop.

Smarty variables are very much limited in scope within Prestashop and their scope is determined by which point the portion of code they are assigned in is run. In the case of {$category->id_cms_category} it is assigned within the CMSController at the point in which the main content (important stuff in the middle) is rendered, and so will be available within cms.tpl as you have demonstrated.

The reason it isn't available in the left column or in the header is due to the order in which each of these sections are rendered. This will be:

a) Header (top of page rather than the html header block specifically), then b) Left Column, then c) "Main" Content, then d) Right Column, then e) Footer

You should find that if you were to reference it in the right column or the footer of the page, then magically it will be available to you (on CMS pages only of course as we're relying on the CMSController being run and assigning it a value).

If you need to reference things like the cms category within the header of the page (maybe to set a highlight on horizontal navigation) then you're going to need to fetch the value and assign it to smarty yourself. You can do this in one of two ways:

1) Write a module which is hooked into the header and assign your variable there 2) Override the FrontController class and assign the smarty variable there (e.g. in the init function)

An example of 2) which you could try is to create a file /override/classes/FrontController.php containing:

<?php
class FrontController extends FrontControllerCore
{
  function init() {
    parent::init();
    $id_cms_category = (int)Tools::getValue('id_cms_category');
    $id_cms_page = (int)Tools::getValue('id_cms');
    self::$smarty->assign(array(
                                'my_cms_category_id' => $id_cms_category,
                                'my_cms_page_id' => $id_cms_page
                                )
                          );
  }
}

The above should allow you to display {my_cms_category_id} and {my_cms_page_id} anywhere in your theme (because we're setting the smarty variables before everything else is rendered). For a non-cms page they should both be 0, my_cms_category_id should be set non-zero on cms category pages, and {my_cms_page_id} should be non-zero when on a specific cms page.

Hope this goes some way to making it a little clearer!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top