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?
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
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);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
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
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?
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!
Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.