在 Smarty 中连接变量和字符串以包含文件

Séb*_*uel 4 php concatenation smarty include prestashop

在 tpl 文件中,我需要动态包含一个文件,并连接一个字符串和一个变量。

这项工作(无串联):

{include file="catalog/_partials/faq-86.tpl"}
Run Code Online (Sandbox Code Playgroud)

然后,我想用变量(产品 ID)替换“86”。

这是我尝试过的(基于 stackoverflow、smarty 论坛或 smarty 文档上的其他答案):

1)

{include file="catalog/_partials/{$product.name}.tpl"}
Run Code Online (Sandbox Code Playgroud)

2)

{assign var="id_pr" value="85"}
{include file="catalog/_partials/.$id_pr.tpl"}
Run Code Online (Sandbox Code Playgroud)

3)

{assign var="id_pr" value="85"}
{include file="catalog/_partials/$id_pr.tpl"}
Run Code Online (Sandbox Code Playgroud)

4)

{include file="{'catalog/_partials/'}{$product.name}{'.tpl'}"}
Run Code Online (Sandbox Code Playgroud)

5)

{assign var='url' value="{'catalog/_partials/'}{$product.name}{'.tpl'}"} 
{include file=$url}
Run Code Online (Sandbox Code Playgroud)

这是聪明的错误:

第 273 行模板“templates/catalog/product.tpl”中的语法错误“{include file="catalog/_partials/{$product.name}.tpl"}”变量模板文件名不允许在 {block} 标签中

所以我的问题是,是否可以连接一个变量和一个字符串以包含一个文件?

我知道这不是最好的方法,但出于模板目的,我需要在不同的产品页面上快速加载不同的 tpl 文件。

我认为这是可能的,因为这种情况有效(没有串联,但文件是动态包含的):

{if $product.id === 85}

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

{elseif $product.id === 86}

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

{/if}
Run Code Online (Sandbox Code Playgroud)

sad*_*lue 7

你可以像这样使用 cat 函数:

{assign var='url' value="catalog/_partials/"|cat:$product.name|cat:".tpl"} 
{include file=$url}
Run Code Online (Sandbox Code Playgroud)

看起来您正在使用具有 3.1.19 智能版本的 Prestashop 1.7,正如我在他们的论坛中发现的(并经过测试),您必须编辑文件/vendor/prestashop/smarty/Smarty.class.php,查找inheritance_merge_compiled_includes并设置为false. 然后删除所有缓存模板(删除文件夹/app/cache/dev/app/cache/prod),它应该在块元素内工作。它在我的测试中起作用。