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.

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

报告相同问题?

悬赏问题

  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算