简体   繁体   English

在树枝中将变量传递给is_grant(Symfony)

[英]Passing Variable to is_granted in twig (Symfony)

I'm having trouble getting the is_granted function in twig to test a variable. 我在让树枝中的is_granted函数测试变量时遇到麻烦。 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. 在这种情况下,我正在测试是否授予当前用户form.roles中包含的角色,但是不幸的是,is_granted测试将仅接受字符串,并且传递变量始终返回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. 我试图过滤输出以确保它是一个字符串,但没有成功,并且{%if is_granted('{{role.vars.value}}')%}中的引号必须存在,因为它们会引起系统错误不包括在内。

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 分配的角色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? 关于如何从给定的值中获得is_granted进行测试的任何想法吗?

You shouldn't open new Twig tags. 您不应该打开新的Twig标签。 {{ ... }} are for dumping (echoing) and in this case you don't want to echo/dump the content of role.vars.value . {{ ... }}用于转储(回显),在这种情况下,您不想回显/转储role.vars.value的内容。 So change it to: {% if is_granted(role.vars.value) %} 因此将其更改为: {% if is_granted(role.vars.value) %}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM