1

Let's say I want to display a larger data set and it is possible that there are many null / empty values. I don't want to define default filter for every variable. Is there a way to display a default value for all (empty, null) variables in a template (like: "not specified").

EDIT:

twig:

{# src/KuMiV/EmployeeBundle/Resources/views/Other/detailItem.html.twig #}
{% extends "base.html.twig" %}
...
{% block content %}   
    <h3 class="sub-header">Personenbezogene Angaben</h3>
        <div class="row">
            <div class="col-md-3">             
                <label> ID </label><p>{{ employee.id }} </p>
            </div>
            <div class="col-md-3">  
                <label> Titel </label><p>{{ employee.title|default('keine Angabe') }} </p>
            </div>
            <div class="col-md-3">             
                <label> Vorname </label><p>{{ employee.firstName }} </p>
            </div>
            <div class="col-md-3">                
                <label> Nachname </label><p>{{ employee.lastName }} </p>
            </div>
        </div>
        <div class="row">
            <div class="col-md-3"> 
                <label> Geschlecht </label><p>{{ employee.gender }} </p>
            </div>
            <div class="col-md-3">               
                <label> Email </label><p>{{ employee.email|default('keine Angabe') }} </p>
            </div>
            <div class="col-md-3">               
                <label> Telefon 1</label><p>{{ employee.phone1|default('keine Angabe') }} </p>
            </div>
            <div class="col-md-3">              
                <label> Telefon 2 </label><p>{{ employee.phone2|default('keine Angabe') }} </p>
            </div>
        </div>
        ...
{% endblock content %}

Controller:

class DefaultController extends Controller
{
 public function detailAction($id)
    {
        $employee = $this->getDoctrine("Employee")
                ->getRepository("EmployeeBundle:Employee")
                ->find($id);

        return $this->render('EmployeeBundle:Other:detail.html.twig', array(
            'employee' => $employee,
        ));
    }
}
kinske
  • 597
  • 8
  • 24
  • 1
    You can set the "not specified" value in your controller while you are passing the twig params; or you can make it conditional in twig to check if the value is not empty (but default filter is more reliable and readable) – Javad Jul 21 '14 at 15:33
  • Can you give a code example for these two possibilities, please? – kinske Jul 22 '14 at 09:43
  • If you could post current code in your controller and twig file you have I will be able to modify it so you can get the idea – Javad Jul 22 '14 at 14:22
  • @Javad - I edited the the significant twig and controller code. – kinske Jul 23 '14 at 11:20
  • check the answer hopefully it helps – Javad Jul 23 '14 at 15:01

1 Answers1

1

You may need to create a custom twig extension for this.

namespace Employee\EmployeeBundle\Twig;
class EmployeeExtension extends \Twig_Extension
{
   public function getFilters()
   {
      return array(
         new \Twig_SimpleFilter('setDefaults', array($this, 'setDefaultFilter')),
      );
   }

   public function setDefaultFilter($employee)
   {
      $employee.title = !empty($employee.title) ? $employee.title : 'keine Angabe';
      $employee.email = !empty($employee.email) ? $employee.email : 'keine Angabe';
      //... So on; continue for others
      return $employee;
   }

   public function getName()
   {
      return 'employee_extension';
   }
}

Keep in mind to register the extension as a service:

services:
   employee.twig.employee_extension:
      class: Employee\EmployeeBundle\Twig\EmployeeExtension
      tags:
         - { name: twig.extension }

Now you can use it in your twig file as:

{% set employee = employee|setDefaults %}

Still I believe the default filter in twig is better.

Javad
  • 4,339
  • 3
  • 21
  • 36
  • I think you misunderstood me. I don't wanted the default for a specific class. I wanted it to be dynamic for many classes. – kinske Jul 23 '14 at 16:22
  • @kinske Oh I see; I think you can do the same to generate your own filter as created above, but the `$employee` should be `$object` which must be in array (I mean you need to convert the object to array first then apply the filter); now you can check each element of the array if is empty set the default value; and return the array – Javad Jul 23 '14 at 17:03
  • You were right, it's better to use the twig default filter. Your example is working, but I'm getting problems when casting to array and later accessing the array in twig template, because of attribute naming. E.g. employee.id gets to KuMiV\EmployeeBundle\Entity\Employeeid. If I cast the Array into an Object (StdClass) the attribute is casted right and seems just like an original instance: `object(stdClass)#1999 (3) {["id":"KuMiV\EmployeeBundle\Entity\Employee"..` but if I want to access via employee.id I get the following Error: `Method "name" for object "stdClass" does not exist in..` – kinske Jul 24 '14 at 16:14