对ps_socialfollow.tpl进行硬编码以支持多语言后出现Smarty错误

时间:2018-07-18 23:15:46

标签: php task-parallel-library prestashop smarty

我的prestashop版本是php 7.1上的1.7.3.4。

这是ps_socialfollow.tpl的默认内容:

 {block name='block_social'}
  <div class="social_follow">
    <ul>
      {foreach from=$social_links item='social_link'}
        <li class="{$social_link.class}"><a href="{$social_link.url}" target="_blank">{$social_link.label}</a></li>
      {/foreach}
    </ul>
  </div>
{/block}

默认情况下,该模块不支持多语言。因此,我已经对其进行了硬编码以支持多种语言:

{block name='block_social'}
{if $language.language_code == 'en-us'}
 <div class="social_follow">
    <ul>
        <li class="facebook"><a href="" target="_blank">{$social_link.label}</a></li>
        <li class="twitter"><a href="" target="_blank">{$social_link.label}</a></li>
        <li class="instagram"><a href="" target="_blank">{$social_link.label}</a></li>
        <li class="googleplus"><a href="" target="_blank">{$social_link.label}</a></li>
        <li class="pinterest"><a href="" target="_blank">{$social_link.label}</a></li>
    </ul>
  </div>

{elseif $language.language_code == 'ko'}
  <div class="social_follow">
    <ul>
        <li class="facebook"><a href="" target="_blank">{$social_link.label}</a></li>
        <li class="twitter"><a href="" target="_blank">{$social_link.label}</a></li>
        <li class="instagram"><a href="" target="_blank">{$social_link.label}</a></li>
        <li class="googleplus"><a href="" target="_blank">{$social_link.label}</a></li>
        <li class="pinterest"><a href="" target="_blank">{$social_link.label}</a></li>
    </ul>
  </div>

{/if}

{/block}

只要我不激活调试模式,它就可以正常工作。激活调试模式后,会显示以下错误消息:

>   ContextErrorException in smarty_internal_templatebase.php(157) :
> eval()'d code line 68:
>     Notice: Undefined index: social_link
>     in smarty_internal_templatebase.php(157) : eval()'d code line 68
>     at ErrorHandler->handleError('8', 'Undefined index: social_link', '/var/www/html/store/vendor/prestashop/smarty/sysplugins/smarty_internal_templatebase.php(157)
> : eval()'d code', '68', array('_smarty_tpl' =>
> object(SmartyDevTemplate))) in smarty_internal_templatebase.php(157) :
> eval()'d code line 68
>     at content_5b4fc96564eec0_18848187(object(SmartyDevTemplate)) in smarty_internal_templatebase.php line 188
>     at Smarty_Internal_TemplateBase->fetch(object(SmartyDevTemplate), null, null, null, false, true, false) in SmartyDevTemplate.php line 41
>     at SmartyDevTemplateCore->fetch() in Module.php line 2282
>     at ModuleCore->fetch('module:ps_socialfollow/ps_socialfollow.tpl', 'ps_socialfollow|1|3|3|3|1|1') in ps_socialfollow.php line 201
>     at Ps_Socialfollow->renderWidget('displayFooterAfter', array('smarty' => object(SmartyDevTemplate), 'cookie' =>
> object(Cookie), 'cart' => object(Cart), 'altern' => '1')) in Hook.php
> line 929
>     at HookCore::coreRenderWidget(object(Ps_Socialfollow), 'displayFooterAfter', array('smarty' => object(SmartyDevTemplate),
> 'cookie' => object(Cookie), 'cart' => object(Cart), 'altern' => '1'))
> in Hook.php line 881
>     at HookCore::exec('displayFooterAfter', array('smarty' => object(SmartyDevTemplate), 'cookie' => object(Cookie), 'cart' =>
> object(Cart), 'altern' => '1'), null) in smarty.config.inc.php line
> 168
>     at smartyHook(array('h' => 'displayFooterAfter'), object(SmartyDevTemplate)) in SmartyLazyRegister.php line 83
>     at SmartyLazyRegister->__call('smartyHook', array(array('h' => 'displayFooterAfter'), object(SmartyDevTemplate))) in
> smarty_internal_templatebase.php(157) : eval()'d code line 691
>     at content_5b4fc96527b774_70366195(object(SmartyDevTemplate)) in smarty_internal_templatebase.php(157) : eval()'d code line 248
>     at content_5b4fc965282d34_37575711(object(SmartyDevTemplate)) in smarty_internal_templatebase.php line 188
>     at Smarty_Internal_TemplateBase->fetch('index.tpl', null, 'layouts/layout-full-width.tpl', object(SmartyDev), false, true,
> false) in SmartyDev.php line 41
>     at SmartyDev->fetch('index.tpl', null, 'layouts/layout-full-width.tpl') in FrontController.php line 684
>     at FrontControllerCore->smartyOutputContent('index.tpl') in FrontController.php line 668
>     at FrontControllerCore->display() in Controller.php line 273
>     at ControllerCore->run() in Dispatcher.php line 428
>     at DispatcherCore->dispatch() in index.php line 28

我关于php的知识甚至不值得一提,因为它更多地基于我能做的一点逻辑思考,而不是实际知识。因此,我无法自行解决聪明的错误。

我必须做的修改是什么?在此模块中实现多语言(网站)支持的正确方法是什么?

我已经根据Alexander Grosul的建议编辑了ps_socialfollow.tpl。

  <div class="social_follow">
    <ul>
        <li class="facebook"><a href="" target="_blank">{$social_link.facebook.label}</a></li>
        <li class="twitter"><a href="" target="_blank">{$social_link.twitter.label}</a></li>
        <li class="instagram"><a href="" target="_blank">{$social_link.instagram.label}</a></li>
        <li class="googleplus"><a href="" target="_blank">{$social_link.googleplus.label}</a></li>
        <li class="pinterest"><a href="" target="_blank">{$social_link.pinterest.label}</a></li>
    </ul>
  </div>

结果是完全相同的聪明错误。

我也尝试过此版本:

{block name='block_social'}
  <div class="social_follow">
    <ul>
      {foreach from=$social_links item='social_link'}

{if $language.language_code == 'en-us'}
         <li class="facebook"><a href="" target="_blank">{$social_link.label}</a></li>
        <li class="twitter"><a href="" target="_blank">{$social_link.label}</a></li>
        <li class="instagram"><a href="" target="_blank">{$social_link.label}</a></li>
        <li class="googleplus"><a href="" target="_blank">{$social_link.label}</a></li>
        <li class="pinterest"><a href="" target="_blank">{$social_link.label}</a></li>



{elseif $language.language_code == 'ko'}
        <li class="facebook"><a href="" target="_blank">{$social_link.label}</a></li>
        <li class="twitter"><a href="" target="_blank">{$social_link.label}</a></li>
        <li class="instagram"><a href="" target="_blank">{$social_link.label}</a></li>
        <li class="googleplus"><a href="" target="_blank">{$social_link.label}</a></li>
        <li class="pinterest"><a href="" target="_blank">{$social_link.label}</a></li>

{/if}

{/foreach}
 </ul>
  </div>
{/block}

这不会触发智能错误,而是仅显示所有图标。立即考虑所选语言。

1 个答案:

答案 0 :(得分:0)

在您的示例中,循环具有默认版本,但在修改后的版本中没有。这意味着您不能使用$social_link,因为它在那里不存在,而是在循环内创建的。您只能使用传入的$social_links并指出要使用的数组元素,例如$social_links.twitter.label等。