Hi everyone. I am working on my first module and I do not understand how to loop over the inner tags of template tag pairs. The code below is just a simplified version for clarity.
Here is my template:
{exp:module:items}
{overview}
{details}
{variation}
{size}
{weight}
{/details}
{/exp:module:items}
Here is the PHP:
function items()
{
global $DB, $TMPL, $IN, $FNS;
$tagdata = $TMPL->tagdata;
$itemlist = $DB->query("SELECT * FROM exp_module_items");
foreach ($TMPL->var_pair as $key => $val)
{
if (ereg("^details", $key))
{
foreach($itemlist->result as $row)
{
//$temp = preg_match("/".LD."$key".RD."(.*?)".LD.SLASH.'details'.RD."/s", $tagdata, $matches);
$tagdata = str_replace('{variation}', $row['variation'], $tagdata);
}
}
}
$tagdata = str_replace('{overview}', 'Something', $tagdata);
return $tagdata;
}
The $temp variable just returns the number of matches for ‘details’. How do you parse and loop the inner variables. Right now I only get one row returned for the {variation}. Any ideas. The documentation on this is not very clear to me.
Thanks.
To iterate details do something like this:
function items()
{
global $DB, $TMPL, $IN, $FNS;
$tagdata = $TMPL->tagdata;
$itemlist = $DB->query("SELECT * FROM exp_module_items");
foreach ($TMPL->var_pair as $key => $val)
{
if (ereg("^details", $key))
{
// render to this string
$details = '';
// get just stuff inside {details}{/details}
if (!preg_match("/".LD."$key".RD."(.*?)".LD.SLASH."$key".RD."/s", $tagdata, $matches))
continue;
$details_tagdata = $matches[1];
foreach($itemlist->result as $row)
{
// make a copy of the unprocessed text
$temp = $details_tagdata;
// do normal modifications
$temp = str_replace('{variation}', $row['variation'], $temp);
// render the results of this iteration
$details .= $temp;
}
// replace the {details}{/details} tag with all the results
$tagdata = preg_replace("/".LD."$key".RD."(.*?)".LD.SLASH."$key".RD."/s", $details, $tagdata);
}
}
$tagdata = str_replace('{overview}', 'Something', $tagdata);
return $tagdata;
}
😊
Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.