Php Symfony2:如果值等于某个值,则将其更改为其他值

Php Symfony2:如果值等于某个值,则将其更改为其他值,php,database,symfony,switch-statement,Php,Database,Symfony,Switch Statement,我从数据库中获取和显示数据,但表示“否”的数据由“(”表示,而表示“是”的数据由“X”表示。当我在细枝中显示数据时,如何将“(”转换为“否”,将“X”转换为“是” 这是我的twig.html文件: {% extends 'base.html.twig' %} {% block body %} <div class="container"> <div class="starter-template"> <h1><strong>{{

我从数据库中获取和显示数据,但表示“否”的数据由“(”表示,而表示“是”的数据由“X”表示。当我在细枝中显示数据时,如何将“(”转换为“否”,将“X”转换为“是”

这是我的twig.html文件:

{% extends 'base.html.twig' %}

{% block body %}
<div class="container">
    <div class="starter-template">
    <h1><strong>{{ shrub.commonname }}</strong></h1>
        <p>THE FOLLOWING IS DETAILED information about the species <strong>{{ shrub.botanicalname }}</strong> (common name <strong>{{ shrub.commonname }})</strong>.</p>

        {% if shrub == "(" %}
            value=="YES"

        <table class="table table-striped">
                    <hr>
        <tbody>
            <tr>
                <th>ph Preference</th>
                <td>{{ shrub.phpreference }}</td>
            </tr>
            <tr>
                <th>Borderline Hardy</th>
                <td>{{ shrub.borderlinehardy }}</td>
            </tr>
            <tr>
                <th>Tolerates Wet Soil</th>
                <td>{{ shrub.wetsoil }}</td>
            </tr>
            <tr>
                <th>Tolerates Moist Soil</th>
                <td>{{ shrub.moistsoil }}</td>
            </tr>
            <tr>
                <th>Prefers Peaty Soil</th>
                <td>{{ shrub.peatysoil }}</td>
            </tr>
            <tr>
                <th>Prefers Well-drained Soil</th>
                <td>{{ shrub.welldrainedsoil }}</td>
            </tr>
            <tr>
                <th>Tolerates Drought</th>
                <td>{{ shrub.drought }}</td>
            </tr>
            <tr>
                <th>Tolerates Clay Soil</th>
                <td>{{ shrub.claysoil }}</td>
            </tr>
            <tr>
                <th>Prefers Sandy Soil</th>
                <td>{{ shrub.sandysoil }}</td>
            </tr>
            <tr>
                <th>Prefers Loam Soil</th>
                <td>{{ shrub.loamsoil }}</td>
            </tr>
            <tr>
                <th>Tolerates Infertile Soil</th>
                <td>{{ shrub.infertilesoil }}</td>
            </tr>
            <tr>
                <th>Prefers Rich Soil</th>
                <td>{{ shrub.richsoil }}</td>
            </tr>
            <tr>
                <th>Tolerates Compacted Soil</th>
                <td>{{ shrub.compactedsoil }}</td>
            </tr>
            <tr>
                <th>Tolerates City Conditions</th>
                <td>{{ shrub.cityconditions }}</td>
            </tr>
            <tr>
                <th>Tolerates Pollution</th>
                <td>{{ shrub.pollution }}</td>
            </tr>
            <tr>
                <th>Tolerates Salt Conditions</th>
                <td>{{ shrub.salt }}</td>
            </tr>
            <tr>
                <th>Tolerates Windy Conditions</th>
                <td>{{ shrub.windy }}</td>
            </tr>
            <tr>
                <th>Prefers Shade</th>
                <td>{{ shrub.shade }}</td>
            </tr>
            <tr>
                <th>Prefers Part Shade</th>
                <td>{{ shrub.partshade }}</td>
            </tr>
            <tr>
                <th>Prefers Full Sun</th>
                <td>{{ shrub.fullsun }}</td>
            </tr>
        </tbody>
    </table>

        {% endif %}

    <ul>
        <li>
            <a href="{{ path('shrubs_index') }}">Back to the list</a>
        </li>
        <li>
            <a href="{{ path('shrubs_edit', { 'id': shrub.number }) }}">Edit</a>
        </li>
        <li>
            {{ form_start(delete_form) }}
                <input type="submit" value="Delete">
            {{ form_end(delete_form) }}
        </li>
    </ul>
    </div>
        </div>
{% endblock %}

}

要根据某些逻辑设置细枝变量,您需要使用如下内容

{% set value = '' %}
{% if '(' == shrub %}
    {% set value = 'YES' %}
{% elseif 'X' == shrub %}
    {% set value = 'NO' %}
{% endif %}

我使用了一根树枝,就像上面有人建议的那样:

public function getFilters()
{
    return array(
        new \Twig_SimpleFilter('x', array($this, 'booleanFilter')),
    );
}

public function booleanFilter()
{
    $x = "yes";

    return $x;
}

然后变量:{shuble.wetsoil | x}

通常您会为这类事情编写一个细枝过滤器:这是一段工作代码吗?我在这里看到了一些错误-一旦您将shuble用作字符串
如果shuble==“(”
一旦您将灌木用作对象
灌木.phpreference
srub
的内容是什么?细枝扩展选项实际上起了作用。感谢Cerad!这只会使整个表消失。感谢您尝试任何方式公共函数booleanFilter($value){if($value==“(”{$boolean=“no”}else{$boolean=“yes”}返回$boolean;}
public function getFilters()
{
    return array(
        new \Twig_SimpleFilter('x', array($this, 'booleanFilter')),
    );
}

public function booleanFilter()
{
    $x = "yes";

    return $x;
}