bigmammoo bigmammoo -4 years ago 182
PHP Question

Symfony2: If value equals something then change it to something else

I'm fetching and displaying data from a database, but data that means "NO" is represented by a "(", while data that means "YES" is represented by an "X". When I'm displaying the data in the twig, how do I convert the "(" to a "NO" and the "X" to a "YES"?

Here's my twig.html file:

{% 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 %}


And my controller if that matters:

public function showAction(Shrubs $shrub)
{
$deleteForm = $this->createDeleteForm($shrub);

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


}

Answer Source

I used a twig extension, like someone suggested above:

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

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

    return $x;
}

Then the variable: {{ shrub.wetsoil | x}}

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download