Here’s how I have been doing conditionals and template single vars (since EE1.x):
// ----------------------------------
// Process conditionals & single vars:
// ----------------------------------
$tagdata = $this->EE->TMPL->tagdata;
if ($tagdata !== FALSE && trim($tagdata) !== '')
{
// ----------------------------------
// Conditionals:
// ----------------------------------
$this->cond['foo'] = $this->foo;
$this->cond['bar'] = $this->bar;
$this->cond['baz'] = $this->baz;
$tagdata = $this->EE->functions->prep_conditionals($tagdata, $this->cond);
// ----------------------------------
// Single variables:
// ----------------------------------
foreach($this->EE->TMPL->var_single as $key => $val)
{
if ($key == 'foo') { $tagdata = $this->EE->TMPL->swap_var_single($val, $this->foo, $tagdata); }
if ($key == 'bar') { $tagdata = $this->EE->TMPL->swap_var_single($val, $this->bar, $tagdata); }
if ($key == 'baz') { $tagdata = $this->EE->TMPL->swap_var_single($val, $this->baz, $tagdata); }
}
}I read through the EE2 docs, and it looks like there are some cool new features of EE2 that might make things easier and more robust… I just don’t understand how to upgrade the above “legacy” approach (or, is the above code still the way to do it?)
Could someone help me modify the above code?
At the very minimum, if someone could point me to a plugin that I could dissect, that would rock!
Thanks so much in advance!!!!
Cheers, Micky
Howdy…
I should clarify…
The docs point out that the way I am doing single vars (code in my first post) is legacy (i.e. the old) way of doing things.
It would be great to see a simple example of a plugin using parse_variables()!
From the docs:
The Template class makes parsing your module or plugin’s variables a snap. Using the parse_variables() method, you supply the tag data, and an array containing all of your variables, organized as “rows”. Your single, pair, and conditional variables will automatically be parsed for you, and your module or plugin will also automatically have {count} and {switch} variables. Additionally, date variables will be parsed, and you can optionally have typography performed automatically for you as well.
It sounds like parse_variables() is way better than how I am doing things currently! I can’t wait to figure this one out. 😊
I just don’t understand how to setup my vars to pass it to parse_variables()?
I think it would be awesome to see a full/simple example of its usage on this docs page. 😊
Any tips would be greatly appreciated!
Thanks! Micky
I think I got it figured… Maybe this info will help others.
“Legacy” code:
// ----------------------------------
// Process conditionals & single vars:
// ----------------------------------
$tagdata = $this->EE->TMPL->tagdata;
if (trim($tagdata) !== '')
{
// ----------------------------------
// Conditionals:
// ----------------------------------
$this->cond['foo'] = $this->foo;
$this->cond['bar'] = $this->bar;
$this->cond['baz'] = $this->baz;
$tagdata = $this->EE->functions->prep_conditionals($tagdata, $this->cond);
// ----------------------------------
// Single variables:
// ----------------------------------
foreach($this->EE->TMPL->var_single as $key => $val)
{
if ($key == 'foo') { $tagdata = $this->EE->TMPL->swap_var_single($val, $this->foo, $tagdata); }
if ($key == 'bar') { $tagdata = $this->EE->TMPL->swap_var_single($val, $this->bar, $tagdata); }
if ($key == 'baz') { $tagdata = $this->EE->TMPL->swap_var_single($val, $this->baz, $tagdata); }
}
}
// ----------------------------------
// Return:
// ----------------------------------
$this->return_data = $tagdata;Upgraded code:
// ----------------------------------
// Process conditionals & single vars:
// ----------------------------------
$tagdata = $this->EE->TMPL->tagdata;
if (trim($tagdata) !== ''):
$variables[] = array(
'foo' => $this->foo,
'bar' => $this->bar,
'baz' => $this->baz
);
$return = $this->EE->TMPL->parse_variables($tagdata, $variables);
endif;
// ----------------------------------
// Return:
// ----------------------------------
$this->return_data = $return;OMG, that is so much better than the “legacy” way of doing things. 😊
Cheers, Micky
Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.