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

在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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?