7. Syntax for conditional

7.1. Content of element

If a part of model data for content of element is null or false, the XML element for the part will be deleted. Using this, you can select the part of template to be displayed.

If true was given to a part of model data, the content of the tag will be displayed without modified.

template

<html>
  <body>
    <div var:cont="{groups}">
      <h1 var:cont="{title}"></h1>
      <div var:cont="{no_data}">
        <em>This group has no data.</em>
      </div>
      <div var:cont="{one_data}">
        This group has only one data: 
        "<span var:cont="{data}" var:omit-tag="true"></span>".
      </div>
      <div var:cont="{many_data}">
        Here's the list of this group's data.
        <ul>
          <li var:cont="{list}"></li>
        </ul>
      </div>
    </div>
  </body>
</html>
   

code

require_once 'XML/Template/zogan.php';
$tmpl = new zogan;
$tmpl->setTemplateFile('./conditional.html');
$tmpl->setCompileDir('.');
$data = array(
  array('Group A', array('only_one')),
  array('Group B', array('one', 'two', 'three')),
  array('Group C', array())
);
$model_data = array();
foreach ($data as $datum) {
  $model_datum = array();
  $model_datum['title'] = $datum[0]; 
  switch(count($datum[1])){
    case 0: 
        $model_datum['no_data'] = true;
        break;
    case 1: 
        $model_datum['one_data'] = array(
          'data' => $datum[1][0]
        );
        break;
    default:
        $model_datum['many_data'] = array(
          'list' => $datum[1]
        );
  }
  $model_data[] = $model_datum;
}
$tmpl->display(array('groups' => $model_data));
   

output

<html>
  <body>
    <div>
      <h1>Group A</h1>
      <div>
        This group has only one data: 
        "only_one".
      </div>
    </div>
    <div>
      <h1>Group B</h1>
      <div>
        Here's the list of this group's data.
        <ul>
          <li>one</li>
          <li>two</li>
          <li>three</li>
        </ul>
      </div>
    </div>
    <div>
      <h1>Group C</h1>
      <div>
        <em>This group has no data.</em>
      </div>
    </div>
  </body>
</html>
   

7.2. Attribute

If a part of model data for attribute is null or false, the attribute for the part will be deleted. If true was given, for example, the attribute "foo" will be foo="foo". Sample of this feature is shown below.

<select name="lang">
    <option var:cont="{lang}" value="{iso}" selected="{selected}">
        <span var:cont="{name}">language</span>
    </option>
</select>
   
$tmpl = new zogan;
// snip
$model_data = array(
    'lang' => array(
        array(
            'name' => 'english',
            'iso'  => 'en'
        ),
        array(
            'name' => 'japanese',
            'iso'  => 'ja',
            'selected' => true
        )
    )
);
$tmpl->display($model_data);
   
<select name="lang">
    <option value="en">
        <span>english</span>
    </option>
    <option value="ja" selected="selected">
        <span>japanese</span>
    </option>
</select>
   

When you want to make the attribute "foo" foo="other_default_value" if true was given to the data, Use attr attribute like below.

<a href="http://planewave.org" var:attr="href={url}">there</a>
   

When variable 'url' is true, the output will be

<a href="http://planewave.org">there</a>
   

When variable 'url' is 'http://www.php.net', the output will be

<a href="http://www.php.net">there</a>