【问题标题】:twig extension render html树枝扩展渲染html
【发布时间】:2017-11-28 11:26:51
【问题描述】:

我想创建一个 twig 函数扩展,为菜单呈现一个 twig 模板

/**
 * Return the functions registered as twig extensions
 *
 * @return array
 */
public function getFunctions()
{
    return array(
        new \Twig_SimpleFunction('my_menu', array($this, 'myMenu')),
    );
}

我的功能

/** render a dropdown with link to conversation
 * @return string
 */
public function myMenu(\Twig_Environment $environment, $typeMenu, $menuId)
{

    return $environment->render('@menuDopdown.html.twig', array(
        'typeMenu' => $typeMenu,
    ));
}

但是当我调用该函数时,我对参数 1 有一个例外

{{ my_menu(4,1280) }}

类型错误:参数 1 传递给 MenuBundle\Twig\TwigMenuExtension::myMenu() 必须是 Twig_Environment,给定整数,调用 ...\app\cache\dev\twig\07\07a15176de77b3d862b64747d9728e117749b97e55d1082eb0f0eec4ffaf12d8.php 在第 164 行

【问题讨论】:

    标签: php symfony twig


    【解决方案1】:

    在你的 myMenu 函数之前添加这个函数,这样 twig 会自动传递环境

    public function getFunctions()
    {
    return array(
        new \Twig_SimpleFunction(
            'myMenu',
            array($this, 'myMenu'),
            array('needs_environment' => true)
        ),
    );
    }
    

    或查看此链接了解更多详情Render template from twig extension

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-27
      • 2019-03-06
      • 2016-02-20
      • 1970-01-01
      • 2015-03-25
      相关资源
      最近更新 更多