如何在 symfony2 的 yaml 菜单中包含用户信息?

How to include user info in yaml menu in symfony2?

我目前正在使用 symfony2 进行开发,并使用 FOSUserBundle 进行用户管理。

我构建了一个 menus.yml 配置文件来将 html 与菜单结构分开。基本上我在我的 config.yml 文件中导入 menus.yml 并将其添加到 twig 的全局变量中。看看我的menus.yml(精简版)

twig:
globals:
    menus:
        loggedin:
            topleft:
                -
                    path: ~
                    caption: Réseau
                    icon: glyphicon-comment
                    submenu:
                        -
                            path: nouvelles
                            caption: Fil de nouvelles
                            icon: glyphicon-globe
            topright:
                -
                    path: ~
                    caption: "{{ app.user.prenom }} {{ app.user.nom }}"
                    icon: glyphicon-user

然后,在我的模板 html 文件中,我使用此

呈现菜单
{% for m in menus.loggedin.topleft %}
<li class="dropdown">
    <a href="#" class="dropdown-toggle" data-toggle="dropdown">{{ m.caption }}</a>
    <ul class="dropdown-menu">
        {% for item in m.submenu %}
        <li><a href="#">{{item.caption}}</a></li>
        {% if item.seperator is defined and item.seperator == true %}
        <li class="divider"></li>
        {% endif %}    
        {% endfor %}
    </ul>
</li>
{% endfor %}

但是我无法显示用户的名字和姓氏,因为文本值按原样打印到 html 页面中。我试过将 app.user 挂接到这样的标题中

caption: %app.user.prenom% %app.user.nom%

但是它不起作用,说该值不存在(还?)

有人知道我该如何解决这个问题吗?

我寻找了 eval() PHP or Javascript function in Twig and found this SO question: Twig variables in twig variable.

的等价物

这是来自 answer by Berry Langerak 的代码,它定义了一个 Twig 过滤器:

<?php

/**
* A twig extension that will add an "evaluate" filter, for dynamic evaluation.
*/
class EvaluateExtension extends \Twig_Extension {
    /**
    * Attaches the innervars filter to the Twig Environment.
    * 
    * @return array
    */
    public function getFilters( ) {
        return array(
            'evaluate' => new \Twig_Filter_Method( $this, 'evaluate', array(
                'needs_environment' => true,
                'needs_context' => true,
                'is_safe' => array(
                    'evaluate' => true
                )
            ))
        );
    }

    /**
     * This function will evaluate $string through the $environment, and return its results.
     * 
     * @param array $context
     * @param string $string 
     */
    public function evaluate( \Twig_Environment $environment, $context, $string ) {
        $loader = $environment->getLoader( );

        $parsed = $this->parseString( $environment, $context, $string );

        $environment->setLoader( $loader );
        return $parsed;
    }

    /**
     * Sets the parser for the environment to Twig_Loader_String, and parsed the string $string.
     * 
     * @param \Twig_Environment $environment
     * @param array $context
     * @param string $string
     * @return string 
     */
    protected function parseString( \Twig_Environment $environment, $context, $string ) {
        $environment->setLoader( new \Twig_Loader_String( ) );
        return $environment->render( $string, $context );
    }

    /**
     * Returns the name of this extension.
     * 
     * @return string
     */
    public function getName( ) {
        return 'evaluate';
    }
}

用法示例:

$twig_environment->addExtension( new EvaluateExtension( ) );

在模板中使用:

{% set var = 'inner variable' %}
{{'this is a string with an {{var}}'|evaluate}}