【问题标题】:How to include user info in yaml menu in symfony2?如何在 symfony2 的 yaml 菜单中包含用户信息?
【发布时间】:2015-02-13 17:47:56
【问题描述】:

我目前正在使用 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%

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

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

【问题讨论】:

  • 为什么不在 Twig 模板中添加{{ app.user.prenom }} {{ app.user.nom }}?您可以添加一个条件来测试它是左侧还是右侧。
  • 可以,但我不会将菜单结构与模板分开。我的 topright 属性中包含更多项目,但我已截断以简化 yaml 示例。
  • 你可以试试这个:Twig variables in twig variable.
  • 哇好!我使用了答案中的评估过滤器,它就像一个魅力!把它写成答案,这样我就可以相信你了。

标签: symfony twig yaml


【解决方案1】:

我在 Twig 中寻找了与 eval() PHPJavascript 函数等效的函数,并发现了这个 SO 问题: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}}

【讨论】:

猜你喜欢
  • 2011-01-06
  • 2013-08-12
  • 1970-01-01
  • 1970-01-01
  • 2014-05-31
  • 1970-01-01
  • 2018-03-17
  • 1970-01-01
  • 2015-03-01
相关资源
最近更新 更多