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

Conditionals not parsed using fetch_and_parse()

Development and Programming

ChristiaanT's avatar
ChristiaanT
6 posts
14 years ago
ChristiaanT's avatar ChristiaanT

I’m trying to get this email-template up and running but get stuck at conditionals in the email template. It’s an extension on freeform using the freeform_module_insert_end hook, but that’s not the issue because using the code below it never works, even when not used in an hook.

Documentation (http://ellislab.com/expressionengine/user-guide/development/usage/template.html) of parse_variables says the following:

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.

When I build an array like the one in the example and want to check on a value in the template it doesn’t work.

This is the most basic example code I’m using:

$this->EE->load->library('Template', NULL, 'TMPL');
$this->EE->TMPL->fetch_and_parse('site', 'email');
$tagdata = $this->EE->TMPL->final_template;

// Set email data
$t = array('personname' => 'John Doe');
$tagdata = $this->EE->TMPL->parse_variables($tagdata, array($t)); // Put in in array to simulate "rows"

// Also not when using this
// $tagdata = $this->EE->TMPL->parse_variables_row($tagdata, $t);

// Also tried something with prep_conditionals, but no results anyway
// $cond['personname'] = TRUE; 
// $tagdata = $this->EE->functions->prep_conditionals( $tagdata, $cond );
        
return $tagdata;

My template looks like:

I am here: {personname}



But also when using in conditionals?

{if personname != ""}
    Given name: {personname}
{/if}

Not in this way too:
{if "{personname}" != ""}
    Given name: {personname}
{/if}

Can anyone of you help me out with my problem? My installation is EE 2.2.2.

       
ChristiaanT's avatar
ChristiaanT
6 posts
14 years ago
ChristiaanT's avatar ChristiaanT

Finaly found a solution! The problem is that the function fetch_and_parse - as the function’s name says - parses the complete template. Inside that function are the conditionals parsed. So my solution was to split those two actions apart. Resulted in the following code:

$email_data = array('foo' => 'bar');
$this->EE->load->library('Template', NULL, 'TMPL');
$email_text = $this->EE->TMPL->fetch_template('email', 'your_email_template', FALSE, $this->EE->config->item('site_id'));
$email_text = $this->EE->TMPL->parse_variables_row($email_text, $email_data);

// Here where the magic happens :blush:
$this->EE->TMPL->parse($email_text, FALSE, $this->EE->config->item('site_id'));

// Final "conditionable" output is stored in final_template variable.. niiiceee
$email_text = $this->EE->TMPL->final_template;

Maybe there is still an easier way doing this, but for now I’m satisfied and I hope I helped one of you with the same problem…

       
iain's avatar
iain
317 posts
14 years ago
iain's avatar iain

Helped me, thanks!

       
Dominiq's avatar
Dominiq
18 posts
13 years ago
Dominiq's avatar Dominiq

Post was from 2011, but your info did help me a lot! Thanks man :D

       
amityweb's avatar
amityweb
162 posts
12 years ago
amityweb's avatar amityweb

I know its old but this helped me a lot, except for one thing I needed to correct which took me ages to figure out, so hope it can help someone else…

I also needed to parse global variables, like {site_url}. The above code was not parsing it, so instead of this:

$email_text = $this->EE->TMPL->final_template;

Use this:

$email_text = $this->EE->TMPL->parse_globals($this->EE->TMPL->final_template);

And now global variables are also parsed into the template

       
amityweb's avatar
amityweb
162 posts
12 years ago
amityweb's avatar amityweb

Also, another tip… if you want to use hidden templates in emails, then the above returns a 404. You have to set $this->EE->TMPL->depth = 1 before and set it back to 0 after…

$this->EE->TMPL->depth = 1; 
$email_text = $this->EE->TMPL->fetch_template('email', '.your_email_template', FALSE, $this->EE->config->item('site_id'));
$this->EE->TMPL->depth = 0;
       
agesnoso's avatar
agesnoso
3 posts
2 years ago
agesnoso's avatar agesnoso

Perhaps there is a more efficient method out there, but for the time being, I’m content and I hope I was able to assist someone else basketball stars who was going through the same thing.

       
lalisa167's avatar
lalisa167
1 posts
about 2 years ago
lalisa167's avatar lalisa167

I am writing to express my gratitude for the enlightening and informative material that you have generously contributed to your blog. papa’s freezeria

       
agelababypp's avatar
agelababypp
1 posts
one year ago
agelababypp's avatar agelababypp

Within Qualys Make that the API box is checked under User Role, for the user you are attempting to connect with. papa’s games

       
romakaiser's avatar
romakaiser
2 posts
one year ago
romakaiser's avatar romakaiser

Exciting!

       
Raymond521's avatar
Raymond521
2 posts
one year ago
Raymond521's avatar Raymond521

To solve the problem of parsing variables and conditions in template emails with ExpressionEngine, there are a few important points to check and make sure that everything is set up correctly. cookie clicker

       
JavierCowan's avatar
JavierCowan
9 posts
one year ago
JavierCowan's avatar JavierCowan

It looks like you’re trying to use parse_variables to dynamically insert and manage variables in your email templates in ExpressionEngine. For a basic single-row example, parse_variables_row is appropriate, but since you mentioned issues with both methods, you’ll need to check the basic implementation. solar smash

       
Grace Whitmore's avatar
Grace Whitmore
1 posts
one year ago
Grace Whitmore's avatar Grace Whitmore

It looks like the issue lies in how ExpressionEngine 2.2.2 handles conditionals and variable parsing. Ensure you’re using gladihoppers parse_variables_row for single-row data and avoid using curly braces ({}) inside conditionals like {if "{personname}” != “”}. Stick with {if personname != ""} and verify that the tagdata being passed contains the expected variables.

       
samuelodell1's avatar
samuelodell1
1 posts
one year ago
samuelodell1's avatar samuelodell1

To resolve your issue with conditionals in the ExpressionEngine email template, ensure that you’re passing variables correctly as rows. Use the structure like this: $t = array(array(‘personname’ => ‘Stick War Legacy’));. When applying conditionals, ensure you’re checking the value directly with {if personname != ""} instead of wrapping the variable in quotes. Additionally, use prep_conditionals() before parsing the variables to ensure the conditionals are processed first.

       
JacobAkhtar's avatar
JacobAkhtar
1 posts
about 6 months ago
JacobAkhtar's avatar JacobAkhtar

It can be really frustrating when you hit a wall with coding, especially when following documentation closely. I had a similar experience while working on a basketball stats app; I couldn’t get the variables to parse correctly. Taking a step back and trying out platforms like Basketball Stars really helped me understand coding better through hands-on experience. It’s a great way to see things in action. Keep pushing through!

       
1 2

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.