Symfony2:如果值等于某个东西,那么将其改为其他东西

时间:2017-03-17 16:44:25

标签: php database symfony switch-statement

我正在从数据库中获取和显示数据,但是表示“NO”的数据用“(”表示,而表示“YES”的数据用“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 %}

如果重要的话我的控制器:

公共功能showAction(灌木$灌木) {     $ deleteForm = $ this-&gt; createDeleteForm($ shrub);

return $this->render('shrubs/show.html.twig', array(
    'shrub' => $shrub,
    'delete_form' => $deleteForm->createView(),
));

}

2 个答案:

答案 0 :(得分:0)

要根据某些逻辑设置树枝变量,您需要使用类似的东西。

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

答案 1 :(得分:0)

我使用了枝条扩展,就像上面建议的那样:

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

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

    return $x;
}

然后变量:{{shrub.wetsoil | X}}