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

Force EE to Parse Tags in output from Plugin

Development and Programming

blendimc's avatar
blendimc
150 posts
about 15 years ago
blendimc's avatar blendimc

Im curious, is it possible for a plugin to output EE tags and then have the EE parse the output of that plugin? ie, (simple example, not practical) If I output {exp:channel:entries}{title}{/exp:channel:entries}, could EE then parse that out?

       
blendimc's avatar
blendimc
150 posts
about 15 years ago
blendimc's avatar blendimc

Nevermind, that works, It doesn’t work for things like {current_time format="%D, %F %d, %Y - %g:%i:%s"}. Must be a parse order issue. I wonder if there is any way to force things from earlier in the parse order to reparse. Is there a function to call to have ee parse a string of data?

       
liaorui's avatar
liaorui
5 posts
14 years ago
liaorui's avatar liaorui

Why this kind of question always get no response?

I have looked some posts already. People are looking for this kind of function: in stead of parsing EE tags at user-viewing-the-webpage time, we want to parse EE tags at back-end during editing-an-entry time.

Please help!

       
blendimc's avatar
blendimc
150 posts
14 years ago
blendimc's avatar blendimc

Yeah, I’m running into this same issue again. It would be really nice to be able to have access to the template parser from a plugin.

       
Wouter Vervloet's avatar
Wouter Vervloet
758 posts
14 years ago
Wouter Vervloet's avatar Wouter Vervloet

Hi guys,

People probably didn’t answer your questions, because it’s not really clear what you’re trying to do…

It would be really nice to be able to have access to the template parser from a plugin.

A plugin is only run during template parsing, so by definition it has access to the template parser. Do you perhaps mean any of the other add-on types (module/extension/fieldtype/accessory)? what are you trying to accomplish exactly?

Wouter

       
blendimc's avatar
blendimc
150 posts
14 years ago
blendimc's avatar blendimc

Ok, I guess I was using Plugin to refer to any type of addon. Sorry for the nomenclature mixup. Either way, it would be nice to have access to at least basic template functionality, that could be called from a module, like this (very, very simple example, but gives the idea anyway.

$string = "{if field == '1'}yes{if:else} No{/if}";
$return = $this->EE->tmpl->parseString($string);
       
Wouter Vervloet's avatar
Wouter Vervloet
758 posts
14 years ago
Wouter Vervloet's avatar Wouter Vervloet

That is already possible:

$vars = array(
  'field' => 1,
  'other_var' => 'lorem ispum'
);

$tagdata = "{if field == '1'}yes{if:else} No{/if}";

$return = $this->EE->TMPL->parse_variables_row($tagdata, $vars);

or if you have a set of data you want to loop through:

$vars = array(
  array(
    'field' => 1,
    'other_var' => 'lorem ispum'
  ),
  array(
    'field' => 0,
    'other_var' => 'dolor sit'
  ),
  array(
    'field' => 1,
    'other_var' => 'amet consectetur'
  ),
  array(
    'field' => 0,
    'other_var' => 'adipiscing'
  )
);

$tagdata = "{if field == '1'}{other_var}{if:else} No{/if}";

$return = $this->EE->TMPL->parse_variables($tagdata, $vars);

This will loop though the multi-dimensional array and return the output as one string.

Wouter

       
blendimc's avatar
blendimc
150 posts
14 years ago
blendimc's avatar blendimc

Thank you very much. I’ve seen that before but didn’t make the connection that that could be used in this situation. Sweet!

       
Wouter Vervloet's avatar
Wouter Vervloet
758 posts
14 years ago
Wouter Vervloet's avatar Wouter Vervloet

The parse_variables method is actually pretty powerful and will let you parse anything.

$vars = array(
  array(
    'single_var_1' => TRUE,
    'single_var_2' => 'This is a string 1',
    'var_pair_1' => array(
      array(
        'sub_var_1' => 'something fun blabla',
        'sub_var_2' => FALSE
      )
    )
  ),
  array(
    'single_var_1' => TRUE,
    'single_var_2' => 'This is a string 2',
    'var_pair_1' => array(
      array(
        'sub_var_1' => 'something fun 1',
        'sub_var_2' => FALSE
      ),
      array(
        'sub_var_1' => 'something fun 2',
        'sub_var_2' => FALSE
      ),
      array(
        'sub_var_1' => 'something fun 3',
        'sub_var_2' => FALSE
      )
    )
  )
);

$template = "<h4>{single_var_2}</h4>
<ul>
  {var_pair_1}
    <li>{sub_var_1}</li>
  {/var_par_1}
</ul>";

$output = $this->EE->TMPL->parse_variables($template, $vars);

Will return the following:

<h4>This is a string 1</h4>
<ul>
  <li>something fun blabla</li>
</ul>
<h4>This is a string 2</h4>
<ul>
  <li>something fun 1</li>
  <li>something fun 2</li>
  <li>something fun 3</li>
</ul>

As you can see it can also parse variable pairs very easily.

Wouter

       
blendimc's avatar
blendimc
150 posts
14 years ago
blendimc's avatar blendimc

Wow! you just made my day a lot easier

       
blendimc's avatar
blendimc
150 posts
14 years ago
blendimc's avatar blendimc

This has worked really well, however, I would like to access this function from an extension that is called in the cp. According to the documentation, the TMPL class is only accessible when a module is called from a tag in the template. Is there any way to access the TMPL->parse_variables from an extension?

       
Wouter Vervloet's avatar
Wouter Vervloet
758 posts
14 years ago
Wouter Vervloet's avatar Wouter Vervloet

You can create an instance of the template class.

if ( ! class_exists('EE_Template'))
{
  require APPPATH.'libraries/Template.php';
}

$TMPL = new EE_Template();
$output = $TMPL->parse_variables($tagdata, $vars);

Wouter

       
blendimc's avatar
blendimc
150 posts
14 years ago
blendimc's avatar blendimc

Sweet. That helps, however, now

if ( ! class_exists('EE_Template'))
            {
              require APPPATH.'libraries/Template.php';
            }

            $vars = array(
              array(
                'field' => 1,
                'other_var' => 'lorem ispum'
              ),
              array(
                'field' => 0,
                'other_var' => 'dolor sit'
              ),
              array(
                'field' => 1,
                'other_var' => 'amet consectetur'
              ),
              array(
                'field' => 0,
                'other_var' => 'adipiscing'
              )
            );

            $tagdata = "{if field == '1'}{other_var}{if:else} No{/if}";

            $return = $TMPL->parse_variables($tagdata, $vars);

However, its returning

{if "1" == '1'}lorem ispum{if:else} No{/if}{if 0 == '1'}dolor sit{if:else} No{/if}{if "1" == '1'}amet consectetur{if:else} No{/if}{if 0 == '1'}adipiscing{if:else} No{/if}

is there any way to actually return the parsed values, ie (lorem ispum Noamet consectetur NO)

Thanks again for your help!

       
blendimc's avatar
blendimc
150 posts
14 years ago
blendimc's avatar blendimc

I found out I can just use $return = $TMPL->advanced_conditionals($return); to parse that out. Thanks!

       

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.