【问题标题】:Shopware 5 call custom function in template fileShopware 5 在模板文件中调用自定义函数
【发布时间】:2021-08-27 18:47:38
【问题描述】:

我使用的是 Shopware 5.6.1 版本。我创建了自定义插件。 我想在模板文件中调用自定义函数,如下所示:-

\Resources\views\frontend\checkout\confirm.tpl

{extends file="parent:frontend/checkout/confirm.tpl"}
{block name='frontend_index_content'}
    {Here I wanted to add custom function result}
    {$smarty.block.parent}
{/block}

谁能确认我怎样才能达到同样的效果。

【问题讨论】:

    标签: shopware5


    【解决方案1】:

    实现这一点的最佳方法是添加 Smarty 插件。 根据你是使用主题还是插件来扩展,有两种方式:

    1. 主题:themes/Frontend/ThemeName/_private/smarty/function.example.php 将 example 替换为您要在模板中调用的实际函数名称。

    2. 插件:

    <?php
    namespace PluginName\Components\CompilerPass;
    
    use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
    use Symfony\Component\DependencyInjection\ContainerBuilder;
    /**
     * Class AddTemplatePluginDirCompilerPass
     */
    class AddTemplatePluginDirCompilerPass implements CompilerPassInterface
    {
        /**
         * You can modify the container here before it is dumped to PHP code.
         *
         * @param ContainerBuilder $container
         */
        public function process(ContainerBuilder $container)
        {
            $template = $container->getDefinition('template');
            $template->addMethodCall('addPluginsDir', ['directory/name']);
        }
    }
    

    在插件boostrap类的build方法中注册CompilerPass类:

    /**
     * @param ContainerBuilder $container
     */
    public function build(ContainerBuilder $container)
    {
        parent::build($container);
    
        $container->addCompilerPass(new AddTemplatePluginDirCompilerPass());
    }
    

    致谢,另请参阅 shopware 5 开发人员文档 https://developers.shopware.com/designers-guide/smarty/#register-custom-smarty-plugins

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-10-13
      • 2013-07-13
      • 2021-09-18
      • 1970-01-01
      • 2017-06-17
      • 2023-03-10
      • 1970-01-01
      • 2019-12-05
      相关资源
      最近更新 更多