Jump to content

[Tutorial] Another tip (config files)


Recommended Posts

I thought I'd share one more tip I often use.
You know how you sometimes need to remove a certain part of the template? There is an obvisous way, of course, if you need to remove

Test


, you can just delete it from the template code and you're done. I don't really like this approach: it works fine when you need to remove one line, but if you have a whole large block or, say, a table that you have removed and now need to get back — real pain in certain areas of your body.
There's another way to do it using php constants.
If you type in your php code:

define('MY_TEMPLATE_VAR', 'This is my template var');


Then in your templates you can call this var like this:

{$smarty.const.MY_TEMPLATE_VAR}


Not really a perfect method too.
Prestashop uses Smarty templating engine, which is great, and it has a feature that for some reason is being ignored by most of Smarty users: config files.
By default, your config directory is in tools/smarty/configs
Create a new file there, name it, say, myconfig.conf.
In this file type:

showAddToCartInCenterBlock = true


and save it. We're going to use this variable to determine whether our shop should or should not show the "Add to Cart" button in the center block (featured products)
Now open your theme's header.tpl file, and at the very top of it type:

{config_load file="myconfig.conf"}


This allows us to use showAddToCartInCenterBlock variable in our templates, so we can now open modules/homefeatured/homefeatured.tpl, find:

{if ($product.quantity > 0 OR $product.allow_oosp) AND $product.customizable != 2}
{l s='Add to cart' mod='homefeatured'}
{else}
{l s='Add to cart' mod='homefeatured'}
{/if}


And enclose it within another if statement:

{if #showAddToCartInCenterBlock#}
   {if ($product.quantity > 0 OR $product.allow_oosp) AND $product.customizable != 2}
{l s='Add to cart' mod='homefeatured'}
   {else}
{l s='Add to cart' mod='homefeatured'}
   {/if}
{/if}


Ok, save everything, now refresh your page. Nothing changed? Now change

showAddToCartInCenterBlock = true


to

showAddToCartInCenterBlock = false


And see those "Add to Cart" buttons disappear.

Quick tips:
Notice that you should access your variable using # (hash) symbols — {#myvar#}, not {$myvar} or {myvar}.
In the example I gave we're using boolean variable: true/false, but you can also use strings, numbers, etc:

myvar = test
mysecondvar = 3


You're not restricted to load your config in header.tpl file, you can load it from any template file.
You also may want to change config files directory to be your theme's config directory (themes/yourtheme/config), to do that open config/smarty.config.inc.php and find:

$smarty->config_dir = _PS_SMARTY_DIR_.'configs';


Change it to:

$smarty->config_dir = _PS_THEME_DIR_.'config';


Have fun!

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...