We use cookies to improve your experience. No personal information is gathered and we don't serve ads. Cookies Policy.

ExpressionEngine Logo ExpressionEngine
Features Pricing Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University
Log In or Sign Up
Log In Sign Up
ExpressionEngine Logo
Features Pro new Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University Blog
  • Home
  • Forums

Is this considered "legacy" plugin code? If so, could someone show me how to update?

Development and Programming

mhulse's avatar
mhulse
329 posts
15 years ago
mhulse's avatar mhulse

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

       
mhulse's avatar
mhulse
329 posts
15 years ago
mhulse's avatar mhulse

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

       
mhulse's avatar
mhulse
329 posts
15 years ago
mhulse's avatar mhulse

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

       

Reply

Sign In To Reply

ExpressionEngine Home Features Pro Contact Version Support
Learn Docs University Forums
Resources Support Add-Ons Partners Blog
Privacy Terms Trademark Use License

Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.