简体   繁体   English

Symfony2 Twig更改/替换值

[英]Symfony2 Twig Change / Replace Value

I'm new here. 我是新来的。 I'm also new in working with Symfony2.5. 我也是使用Symfony2.5的新手。

I want to replace a value with another, better would be to change it via array. 我想用另一个替换一个值,最好是通过数组更改它。 It's hard to explain - here are some code parts: 很难解释-以下是一些代码部分:

Recources/Views/register.html.twig: 资源/视图/register.html.twig:

 {% for user in list_user %}
     {{ user.Id }}
     {{ user.isAdmin }}
     {{ user.isActive }}
 {% endfor %}

Here I generate the array to send to register.html.twig which looks like this: 在这里,我生成了要发送到register.html.twig的数组,如下所示:

Controller/AccountController.php Controller / AccountController.php

    $user = $this->getDoctrine()
        ->getRepository('SeotoolMainBundle:User')
        ->findAll();

    return $this->render(
        'SeotoolMainBundle:Account:register.html.twig',
        array('form' => $form->createView(), 'list_user' => $user)
    );

It correctly outputs for isAdmin and isActive 0 or 1. Now I want to replace this output with for example isActive = 1 should Output "Active", isActive = 0 shut Output "Not active". 它正确地为isAdmin和isActive 0或1输出。现在,我想用例如isActive = 1替换此输出,应该输出“ Active”,isActive = 0关闭输出“ Not active”。 I hope you understand what I mean and can help me find the correct way to do this. 希望您能理解我的意思,并可以帮助我找到正确的方法。

Thank you guys, 感谢大伙们,
kind regards, 亲切的问候,

Marvin 马文

Can't you just use an "if"? 您不能只使用“ if”吗?

{% for user in list_user %}
 {{ user.Id }}
 {{ user.isAdmin }}
 {% if user.isActive %}
   Active
 {% else %}
   Inactive
 {% endif %}
{% endfor %}

You can also use the ternary operator, which is more compact. 您还可以使用更紧凑的三元运算符。

{% for user in list_user %}
 {{ user.Id }}
 {{ user.isAdmin }}
 {{ user.isActive ? 'Active' : 'Inactive' }}
{% endfor %}

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

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