简体   繁体   中英

Smarty include passing array in var

I try to pass an array element in include's var.

But I still have this error :

Fatal error: Uncaught --> Smarty Compiler: Syntax error in template "file:/home/technique/www/site/tpl/home.html" on line 6 "{include file='include/article-latest.html' class='col-50' title=$article.TITLE tag=ARTICLE_CATEGORY.$article.CATEGORY img=$article.THUMBNAIL view='3526' share='564'}" - Unexpected ".", expected one of: "}" <-- thrown in /home/technique/www/common/lib/smarty/libs/sysplugins/smarty_internal_templatecompilerbase.php on line 6

My code :

{foreach $latest_article.0 as $article}
    {include file='include/article-latest.html' class='col-50' title=$article.TITLE tag=ARTICLE_CATEGORY.$article.CATEGORY img=$article.THUMBNAIL view='3526' share='564'}
{/foreach}

Apparently, the problem is using the constant ARTICLE_CATEGORY. It seem like the php constant isn't interpreted by smarty...

PHP constants in Smarty can be called by $smarty.const.CONSTANT_NAME Reference is here - https://www.smarty.net/forums/viewtopic.php?p=25903&sid=92c2eb2c177f8f82ae084361ee6c4400

{foreach $latest_article.0 as $article}
    {include file='include/article-latest.html' class='col-50' title=$article.TITLE tag=$smarty.const.ARTICLE_CATEGORY.$article.CATEGORY img=$article.THUMBNAIL view='3526' share='564'}
{/foreach}

besides take into account the following: - TITLE and CATEGORY might be constants in the code too therefore must be called via $smarty.const.TITLE and $smarty.const.CATEGORY appropriately; - ARTICLE_CATEGORY is constant so it is strange that it is used as array; - ARTICLE.$article.CATEGORY might be too deep-level array and might be processed by Smarty incorrectly (because of too many dots). To fix it, you might need to assign variables, for example:

{assign var="article_category" value=$smarty.const.ARTICLE_CATEGORY}
{foreach $latest_article.0 as $article}
    {include file='include/article-latest.html' class='col-50' title=$article.TITLE tag=$article_category.$article.CATEGORY img=$article.THUMBNAIL view='3526' share='564'}
{/foreach}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM