douyan1927 2015-07-06 06:14 采纳率: 0%
浏览 36
已采纳

将变量传递给twig中的is_granted(Symfony)

I'm having trouble getting the is_granted function in twig to test a variable. In this case, I am testing if the current user is granted the role contained in the form.roles, but unfortunately the is_granted test will ONLY accept a string, and passing a variable is always returning false.

                    <div class="form-group well">
            {{ form_label(form.roles) }}
            {% for role in form.roles %}
                {{ role.vars.value|trim }}{% if is_granted('{{ role.vars.value }}') %}
                <div class="row marketing ">
                    <div class="col-sm-6">
                        {{ form_label(role) }}
                    </div>
                    <div class="col-sm-6">
                        {{ form_widget(role, {"attr": {"class": "form-control"}} ) }}
                    </div>
                </div>
                {% endif %}
            {% endfor %}
        </div>

I have tried to filter the output to ensure that it is a string, without success, and the quotes in {% if is_granted('{{ role.vars.value }}') %} MUST be there as the system errors if they are not included.

Output of this code is:

Allocated Roles ROLE_SYSTEM_ADMIN ROLE_REGISTRAR ROLE_ADMIN ROLE_PRINCIPAL ROLE_HEAD_TEACHER ROLE_TEACHER ROLE_STUDENT ROLE_PARENT ROLE_USER

Any idea on how I can get the is_granted to test from the value given?

  • 写回答

1条回答 默认 最新

  • duanbairan4235 2015-07-06 06:58
    关注

    You shouldn't open new Twig tags. {{ ... }} are for dumping (echoing) and in this case you don't want to echo/dump the content of role.vars.value. So change it to: {% if is_granted(role.vars.value) %}

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突