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 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?