Detecting current page

Is there any way to detect the current page with SMARTY or another way in CS-Cart.



I’d like to be able to add some page specific code to different pages. I’m sure I could do some of this via the blocks, but using the template would be much more flexible.



Thanks for any advice.

There are a few ways to do this. I think a lot of people use if statements, so for example if you wanted to display a banner on just the homepage but not on subpages you could say



{if $location_dir==“blocks/locations/index”}



{else}



{/if}





Having done this before though, I actually prefer using blocks so that you avoid weird layout changes as your content shifts from page to page.

Thank you for the suggestion. is there any way to do this by page types? For example only on category and subcategory pages. I am altering the category/subcategory layouts for the product information (image, name, price, etc.). Unfortunately, it seems the product_multicolumns_list template used is also shared by the related products shown on the product pages. So, whenever I apply the css styles/positioning to the categories layout it is alterng my product page related products section. If I can target just category/subcategory I can just tell my hooked template file to insert a unique css class name to the area and thus only affect layout on category/subcategory pages.

Can’t you check if the category has a parent? If it does then use one class, if does not then another?



“$controller” would tell you what kind of a page that is…

Thank you for taking the time to help.



I am changing the layout for both parent and child (subcategory) categories. Consequently checking for a parent would let me alter the subcategories, but not the main categories, so I think a different approach would be required. Or, two checks one for the main category and then use the “if has a parent” approach to target the sub-categories.



Can you please clarify how I wold use the “”$controller" to tell me the page type. I’m not familiar with it and it may help, either way I’d learn something new.

Well, for main categories you can check if they do NOT have parents, if they are truly main, then they would not have parents (or all have the same one like main page, not sure how CS-Cart implemented it).



Controller would tell you if you are on a catogory, page, product, checkout… It is a string variable telling the type of the page.



Too look for all defined variables in smarty put somewhere:

{php}

echo ‘

’; print_r(get_defined_vars()); echo ‘
’;

{/php}



This will print a huge list of what you can read and do logical conlusions against.

Can’t you just check for the controller:



{if controller == 'categories'}
do this
{else]
do that
{/if}




Bob

Your correct TexasGuy, I missed the obvious.



Thank you Jobosales, I think that’s exactly what I need. Is there a list of controllers anywhere that I can also reference (so I know what’s available in other situations? The forum search and knowledgebase did not yield a controllers list anywhere.

You can get the controller names from the appropriate directories:

/controllers/customer
/controllers/admin
/controllers/common




Bob

Thank you jobosales.



I tried the if/else snippet you posted on a page with the following URL:

/store/?dispatch=categories.view&category_id=227



I’m assuming that’s using the categories controller:



I then added the following to try and wrap a span tag around an area if the page is a categories page:



{if controller == ‘categories’}



{else}



{/if}



I added the same, with a closing span tag lower on the page:



{if controller == ‘categories’}



{else}



{/if}



Nothing occurs (please note I changed the else tag to {else} from the {else] in your post assuming that was a typo.)



The code is being added to the products_multicolumns.tpl file in customer/views/categories/

Ok, I got the if then working. There was a second typo in the reply by jobosales, the omission of the $ character in the controller check. here is what the correct code should look like, if anyone else needs to do conditional logic based on a controller:



{if $controller == ‘categories’}

action to take goes here

{else}

default second action to take goes here

{/if}



If you do not need the two conditions (scenarios), then just remove the else tag like so:



{if $controller == ‘categories’}

action to take goes here

{/if}

Sorry for the confused post - glad you got it working.



Bob