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

Smarty error after hardcoding ps_socialfollow.tpl to support multilanguage

我的 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的了解甚至不值一提,因为它更多的是基于我能够做到的一点点逻辑思维,而不是实际知识。因此,我无法自行修复 smarty 错误。

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

我已经根据 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}

这并没有触发聪明的错误,只是显示了所有的图标。一次,不管选择的语言。

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