dongqiang1894 2017-05-31 08:01
浏览 68
已采纳

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

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}
  • 写回答

1条回答 默认 最新

  • duanchen7703 2017-05-31 08:25
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题