{foreach:variable,key,value}

{foreach:variable,key,value} -- creates a PHP foreach loop

概要

Usage ({foreach:variable,key,value}, {foreach:variable,value})

説明

creates a foreach loop, needs an {end:} tag. note that the engine will add the variable to the scope, so they will not be prefixed with $t-> when used inside the loop.

パラメータ

例 40-1Setting variables for foreach

$this->a = array(
  "dog" => "cat",
  "fire" => "water"
);
$this->b = array('a','b','c');

$template->outputObject($this);

例 40-2Foreach in template

{foreach:a,k,v}
  k is {k}, and v is {v}
{end:}
{foreach:b,v}
  v is {v}
{end:}

例 40-3Compiled template

<?php if (is_array($t->a)) foreach($t->a as $k => $v) { ?>
  k is <?php echo htmlspecialchars($k); ?>, and v is <?php echo htmlspecialchars($v); ?>
<?php } ?>
<?php if (is_array($t->a)) foreach($t->b as $v) { ?>
  v is <?php echo htmlspecialchars($v); ?>
<?php } ?>

例 40-4example output

k is dog, v is cat
k is fire, v is water
v is a
v is b
v is c