2017-03-17 59 views
0

我從數據庫獲取並顯示數據,但意味着「NO」的數據由「(」表示,而表示「YES」的數據表示當我在樹枝上顯示數據時,如何將「(」轉換爲「否」和「X」轉換爲「是」?)Symfony2:如果值等於某事然後將其改爲別的

這裏是我的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-> createDeleteForm($ shrub);

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

}

+1

通常你會爲這種事情寫一個樹枝過濾器:http://symfony.com/doc/current/templating/twig_extension.html – Cerad

+0

這是一段代碼嗎?我在這裏看到了一些錯誤 - 一旦你使用灌木作爲字符串'如果灌木==「(」'並且一旦你使用灌木作爲對象'灌木.phpreference'。什麼是'srub'的內容? –

+0

枝條延伸選項實際上由於工作CERAD – bigmammoo

回答

0

我使用的樹枝延伸,就像有人上面建議:

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

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

    return $x; 
} 

然後,變量:{{shrub.wetsoil | x}}

+0

公共職能booleanFilter($值){ 如果 ($價值== 「(」){$ 布爾= 「無」;! } else { $ boolean =「yes」; } return $ boolean; } – bigmammoo

0

要設置基於某種邏輯枝杈變量,你需要使用這樣的事情。

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

,只是讓整個表消失感謝您嘗試反正 – bigmammoo

相關問題