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.
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…
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
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;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.
I am writing to express my gratitude for the enlightening and informative material that you have generously contributed to your blog. papa’s freezeria
Within Qualys Make that the API box is checked under User Role, for the user you are attempting to connect with. papa’s games
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
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
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.
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.
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!
Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.