Okay, so…
I’m trying to update an addon from EE 2 (Parse Mail) to work properly in EE 3. I’m running into a problem with template parsing. The idea behind the addon is that template tags in system emails EE sends out (like account registration confirmation etc) can have any template tags they want. For instance, the site I’m upgrading embeds a template in the account registration template.
This all worked great on EE 2 but I’m having some trouble getting the parse method updated for EE 3. Granted this looks kind of hacky but I didn’t write it and it was working in EE 2:
public function parse($template_body, $embed_vars = array(), $sub_template = false)
{
// Parse a template
// Template lib required for the embed/parse functions.
ee()->load->library('template');
// Temporarily replace the TMPL, because addons rely on it.
$tmpl = new EE_Template();
$orig_tmpl =& ee()->TMPL;
ee()->TMPL =& $tmpl;
$vars = array_merge(
/* (array)$orig_global_vars, */
$this->prefix_array((array)$embed_vars, 'embed:'),
$this->prefix_array((array)$embed_vars, ':'),
$this->prefix_array((array)$embed_vars, ''));
foreach ($vars as $k => $v)
{
$v = strval($v);
$v = ee()->functions->encode_ee_tags($v, true);
ee()->config->_global_vars[$k] = $v;
}
$vars = ee()->config->_global_vars;
$template_body = $tmpl->parse_variables_row($template_body, $vars, false);
$tmpl->parse($template_body, $sub_template);
$final_template = $tmpl->final_template;
// Global variables.
$final_template = $tmpl->parse_globals($final_template);
ee()->TMPL =& $orig_tmpl;
return $this->cleanup_embed_vars($final_template);
}
I updated to remove the assigning by reference to an overloaded object and EE/PHP throws an error on line 83 of Legacy/Facade.php “Cannot overwrite TMPL on the loader.”
If I comment out that check on 83 in Facade.php everything works (but does throw other notices and warnings). But obviously that relies on a core hack. So I tried removing the ee()->TMPL stuff but then my email template gets the template from the current route of the site (account/forgot-password) rendered into it at the top. So then I tried stuff like:
ee()->TMPL->template = '';
ee()->TMPL->final_template = '';
To no avail. Is there still a way this can be accomplished?
This should get you moving, but would not be officially supported, i.e. I’d consider it private API, even though the method signatures have to be public.
$origTMPL = clone ee()->TMPL;
ee()->remove('TMPL');
ee()->set('TMPL', new EE_Template());
// do some stuff
ee()->remove('TMPL');
ee()->set('TMPL', $origTMPL);
Perfect. We’ll punt the pain down the road when the API really becomes private.
On the other hand, EE 4 or 5 is going to have a nice abstracted template parser so that you can give it a string and tell it to render it and it’s not tied to the request or a bunch of other conditional stuff, right? Right? 😉
Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.