Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Concatenate a variable and a string in Smarty in order to include a file

In a tpl file, i need to include a file dynamically, with a concatenation of a string and a variable.

This work (no concatenation) :

{include file="catalog/_partials/faq-86.tpl"}

Then, i would like to replace "86" by a variable (the product id).

Here is what i’ve tried (based on other answers on stackoverflow , on smarty forum or smarty documentation):

1)

{include file="catalog/_partials/{$product.name}.tpl"}

2)

{assign var="id_pr" value="85"}
{include file="catalog/_partials/.$id_pr.tpl"}

3)

{assign var="id_pr" value="85"}
{include file="catalog/_partials/$id_pr.tpl"}

4)

{include file="{'catalog/_partials/'}{$product.name}{'.tpl'}"}

5)

{assign var='url' value="{'catalog/_partials/'}{$product.name}{'.tpl'}"} 
{include file=$url}

Here is the smarty error :

Syntax error in template "templates/catalog/product.tpl" on line 273 "{include file="catalog/_partials/{$product.name}.tpl"}" variable template file names not allow within {block} tags

So my, question, is it possible to concatenate a variable and a string in order to include a file ?

I know it is not the best approach but for templating purpose, I need to quickly load different tpl files on different product page.

I think it is possible since this condition is working (no concatenation but the file is included dynamically) :

{if $product.id === 85}

    {include file="catalog/_partials/faq-85.tpl"}

{elseif $product.id === 86}

    {include file="catalog/_partials/faq-86.tpl"}

{/if}
like image 857
Sébastien Gicquel Avatar asked Mar 09 '23 23:03

Sébastien Gicquel


1 Answers

you can use the cat function like this:

{assign var='url' value="catalog/_partials/"|cat:$product.name|cat:".tpl"} 
{include file=$url}

Looks like you are using Prestashop 1.7 that has smarty version 3.1.19 and as i found in their forums (and tested) you have to edit the file /vendor/prestashop/smarty/Smarty.class.php, look for inheritance_merge_compiled_includes and set to false. Then delete all the cache templates (delete the folder /app/cache/dev and /app/cache/prod) and it should work inside the block element. It worked in my test.

like image 73
sadlyblue Avatar answered Apr 06 '23 08:04

sadlyblue