简体   繁体   English

树枝扩展渲染html

[英]twig extension render html

I want to create a twig function extension that render a twig template for a menu 我想创建一个树枝功能扩展,以呈现菜单的树枝模板

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

my function 我的功能

/** 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,
    ));
}

but when i call the function i have a exeption for parametre 1 但是当我调用函数时,我有一个参数1的例子

{{ my_menu(4,1280) }}

Type error: Argument 1 passed to MenuBundle\\Twig\\TwigMenuExtension::myMenu() must be an instance of Twig_Environment, integer given, called in ...\\app\\cache\\dev\\twig\\07\\07a15176de77b3d862b64747d9728e117749b97e55d1082eb0f0eec4ffaf12d8.php on line 164 类型错误:传递给MenuBundle \\ Twig \\ TwigMenuExtension :: myMenu()的参数1必须是Twig_Environment的实例,给定整数,在... \\ app \\ cache \\ dev \\ twig \\ 07 \\ 07a15176de77b3d862b64747d9728e117749b97e55d1082eb0f0eec4onaf164d8中调用。

add this function, before your myMenu function, so twig will automatically pass the environment 在myMenu函数之前添加此函数,以便树枝会自动通过环境

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

or check this link for more details Render template from twig extension 或检查此链接查看更多细节渲染从树枝扩展模板

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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