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

Template parsing in extension on email hook

Developer Preview

TJ Draper's avatar
TJ Draper
222 posts
8 years ago
TJ Draper's avatar TJ Draper

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?

       
Derek Jones's avatar
Derek Jones
7,561 posts
8 years ago
Derek Jones's avatar Derek Jones

Which hook is this method being used on? Or is this method a tag on a template that is being accessed?

       
TJ Draper's avatar
TJ Draper
222 posts
8 years ago
TJ Draper's avatar TJ Draper

email_send hook.

       
Derek Jones's avatar
Derek Jones
7,561 posts
8 years ago
Derek Jones's avatar Derek Jones

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);
       
TJ Draper's avatar
TJ Draper
222 posts
8 years ago
TJ Draper's avatar TJ Draper

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? 😉

       

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.