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

时间:2015-02-13 17:47:56

标签: symfony twig yaml

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

我构建了一个menus.yml配置文件,用于将html与菜单结构分开。 Basicaly我在config.yml文件中导入menus.yml,它被添加到twig的全局变量中。这是我的menus.yml(Abridged版本)

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%

但它没有用,说价值不存在(还有?)

任何人都知道如何解决这个问题?

1 个答案:

答案 0 :(得分:2)

我在Twig中寻找了等效的eval() PHPJavascript函数,并发现了这个问题:Twig variables in twig variable

以下是定义Twig过滤器的answer by Berry Langerak代码:

<?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}}