Can someone please please please help me with the following..
I have several variable pairs inside a plugin. I have the data prepared but need to use the prep_conditionals function to sort.
Inside my tag I have:
{statuses}
{if status == current_status}{status}{/if}
{statuses}
{fields}
{if field_type = 'textarea'}do this{/if}
{if field_type = 'text'}do this{/if}
{/fields}
in the plugin i have this
... get the query data for statuses
... get the query data for fields
$tagdata = TMPL->tagdata;
foreach ($field_data as $row) {
.. blah blah
$cond = $row;
$tagdata = $FNS->prep_conditionals($tagdata, $cond);
}
foreach ($status_data as $row) {
.. blah blah
$cond = $row;
$tagdata = $FNS->prep_conditionals($tagdata, $cond);
}
then variable pairs…
foreach ($TMPL->var_pair as $key => $val) {
if ($key == 'statuses') {
$statuses = '';
preg_match( "/".LD.$key.RD."(.*?)".LD.SLASH.$key.RD."/s", $tagdata, $match);
foreach ($status_data as $row) {
$str = $match['1'];
foreach ($row as $k => $v ) {
$str = str_replace( LD.$k.RD, $v, $str);
}
$statuses .= $str;
}
$tagdata = str_replace($match['0'], $statuses, $tagdata);
}
if ($key == 'custom_fields') {
$custom_fields = '';
preg_match( "/".LD.$key.RD."(.*?)".LD.SLASH.$key.RD."/s", $tagdata, $match);
foreach ($field_data as $row) {
$str = $match['1'];
foreach ($row as $k => $v ) {
$str = str_replace( LD.$k.RD, $v, $str );
}
$custom_fields .= $str;
}
$tagdata = str_replace($match['0'], $custom_fields, $tagdata);
}
}
return $tagdata;
its not parsing the conditionals properly.
Can someone (i.e one of you EE geniuses) please help?
Mod Edit: Moved to the Plugins: Technical forum.
You need to operate on a copy of $TMPL->tagdata, or more specifically, a copy of the variable pairs’ tagdata, and process the conditionals for each loop within the variable pair. Look at the Weblog module’s {categories} tag pair handling in parse_weblog_entries() for an example.
Derek - its working, and I think its working they way you mentioned… But can you quickly cast your eye over this, Thanks 😊
$w_sql = "SELECT blog_title AS a_weblog_title, weblog_id AS a_weblog_id, blog_name AS a_weblog_name FROM exp_weblogs ";
$w_query = $DB->query($w_sql);
$g_sql = "SELECT group_id AS allowed_group_id, group_title AS allowed_group_title FROM exp_member_groups ";
$g_query = $DB->query($g_sql);
$tagdata = $TMPL->tagdata;s
if (preg_match("/".LD."member_weblogs".RD."(.*?)".LD.SLASH.'member_weblogs'.RD."/s", $TMPL->tagdata, $w_match)) {
$weblog_chunk = $w_match['1'];
}
if (preg_match("/".LD."allowed_groups".RD."(.*?)".LD.SLASH.'allowed_groups'.RD."/s", $TMPL->tagdata, $g_match)) {
$groups_chunk = $g_match['1'];
}
$w = '';
foreach ($w_query->result as $row) {
foreach ($TMPL->var_pair as $key => $val) {
if (ereg("^member_weblogs", $key)) {
$temp = $weblog_chunk;
$cond = $row;
$temp = $FNS->prep_conditionals($temp, $cond);
foreach ($row as $k => $v ) {
$temp = str_replace( LD.$k.RD, $v, $temp );
}
$w .= $temp;
}
}
}
//echo $w;
$g = '';
foreach ($g_query->result as $row) {
foreach ($TMPL->var_pair as $key => $val) {
if (ereg("^allowed_groups", $key)) {
$temp = $groups_chunk;
$cond = $row;
$temp = $FNS->prep_conditionals($temp, $cond);
foreach ($row as $k => $v ) {
$temp = str_replace( LD.$k.RD, $v, $temp );
}
$g .= $temp;
}
}
}
//echo $g;
$tagdata = str_replace($g_match['0'], $g, $tagdata);
$tagdata = str_replace($w_match['0'], $w, $tagdata);
return $tagdata;
Yes, though you don’t appear to be using $key nor $val from your $TMPL->var_pair foreach statements, so you can forego those, and I know in 1.x the weblog module still uses it, but you’d want to use PCRE regex functions instead of POSIX. It won’t matter though since you’ll just be removing it altogether along with the $TMPL->var_pair foreach statements.
Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.