EmailTemplateRendererService
in package
Service for rendering email templates with variable substitution
HTML templates are stored as Markdown and converted to HTML during rendering
Table of Contents
Properties
- $parsedown : Parsedown
Methods
- __construct() : mixed
- Constructor
- extractVariables() : array<string|int, mixed>
- Get list of variables used in a template
- getMissingVariables() : array<string|int, mixed>
- Validate that all required variables are provided
- htmlToText() : string
- Convert HTML to plain text (simple conversion)
- preview() : array<string|int, mixed>
- Preview rendered template with sample data
- renderHtml() : string|null
- Render HTML template
- renderHtmlBody() : string|null
- Render HTML body only (without wrapper)
- renderSubject() : string
- Render subject template
- renderTemplate() : string
- Render a template by replacing variables with values
- renderText() : string|null
- Render text template
- textToHtml() : string
- Convert plain text to HTML (simple conversion)
- evaluateComparison() : bool
- Evaluate a single comparison: varName == "value" or varName != "value"
- evaluateCondition() : bool
- Evaluate a conditional expression safely.
- formatValue() : string
- Format a value for display in email
- processConditionals() : string
- Process conditional blocks in template before variable substitution.
- splitOutsideQuotes() : array<string|int, mixed>
- Split a condition string by a logical operator, but only when the operator appears outside of quoted strings.
- wrapInEmailHtml() : string
- Wrap HTML body content in email-friendly HTML structure
Properties
$parsedown
protected
Parsedown
$parsedown
Methods
__construct()
Constructor
public
__construct() : mixed
extractVariables()
Get list of variables used in a template
public
extractVariables(string $template) : array<string|int, mixed>
Finds {{variable}}, ${variable}, and variable references in {{#if}} conditionals.
Parameters
- $template : string
Return values
array<string|int, mixed> —List of variable names
getMissingVariables()
Validate that all required variables are provided
public
getMissingVariables(string $template, array<string|int, mixed> $vars) : array<string|int, mixed>
Parameters
- $template : string
-
Template string
- $vars : array<string|int, mixed>
-
Variables provided
Return values
array<string|int, mixed> —Missing variable names
htmlToText()
Convert HTML to plain text (simple conversion)
public
htmlToText(string $html) : string
Parameters
- $html : string
-
HTML
Return values
string —Plain text
preview()
Preview rendered template with sample data
public
preview(EmailTemplate $emailTemplate[, array<string|int, mixed> $sampleVars = [] ]) : array<string|int, mixed>
Parameters
- $emailTemplate : EmailTemplate
- $sampleVars : array<string|int, mixed> = []
-
Sample variable values
Return values
array<string|int, mixed> —Preview of subject, html, and text
renderHtml()
Render HTML template
public
renderHtml(EmailTemplate $template[, array<string|int, mixed> $vars = [] ]) : string|null
The html_template field stores Markdown, which is converted to HTML during rendering. Variables are substituted BEFORE markdown conversion to allow variables in links, etc.
Parameters
- $template : EmailTemplate
- $vars : array<string|int, mixed> = []
Return values
string|nullrenderHtmlBody()
Render HTML body only (without wrapper)
public
renderHtmlBody(EmailTemplate $template[, array<string|int, mixed> $vars = [] ]) : string|null
Used when you need just the content without the HTML structure wrapper.
Parameters
- $template : EmailTemplate
- $vars : array<string|int, mixed> = []
Return values
string|nullrenderSubject()
Render subject template
public
renderSubject(EmailTemplate $emailTemplate, array<string|int, mixed> $vars) : string
Parameters
- $emailTemplate : EmailTemplate
- $vars : array<string|int, mixed>
Return values
stringrenderTemplate()
Render a template by replacing variables with values
public
renderTemplate(string $template, array<string|int, mixed> $vars) : string
Processes conditional blocks first, then substitutes variables.
Parameters
- $template : string
-
Template string with {{variable}} placeholders
- $vars : array<string|int, mixed>
-
Array of variable name => value pairs
Return values
string —Rendered template
renderText()
Render text template
public
renderText(EmailTemplate $emailTemplate, array<string|int, mixed> $vars) : string|null
Parameters
- $emailTemplate : EmailTemplate
- $vars : array<string|int, mixed>
Return values
string|nulltextToHtml()
Convert plain text to HTML (simple conversion)
public
textToHtml(string $text) : string
Parameters
- $text : string
-
Plain text
Return values
string —HTML
evaluateComparison()
Evaluate a single comparison: varName == "value" or varName != "value"
protected
evaluateComparison(string $comparison, array<string|int, mixed> $vars) : bool
Supports both == (equality) and != (not-equal) operators. Variable names do not use a $ prefix in the {{#if}} syntax.
Parameters
- $comparison : string
-
Single comparison expression
- $vars : array<string|int, mixed>
-
Available variable values
Return values
boolevaluateCondition()
Evaluate a conditional expression safely.
protected
evaluateCondition(string $condition, array<string|int, mixed> $vars) : bool
Splits by || first (lower precedence), then && (higher precedence), then evaluates individual comparisons.
Parameters
- $condition : string
-
Expression like 'var == "value" || var == "other"'
- $vars : array<string|int, mixed>
-
Available variable values
Return values
boolformatValue()
Format a value for display in email
protected
formatValue(mixed $value) : string
Parameters
- $value : mixed
Return values
stringprocessConditionals()
Process conditional blocks in template before variable substitution.
protected
processConditionals(string $template, array<string|int, mixed> $vars) : string
Parses {{#if condition}}...{{/if}} blocks as a safe DSL. Supports ==, !=, || (OR), and && (AND) operators.
Example: {{#if status == "Approved" || status == "Revoked"}}...{{/if}}
Parameters
- $template : string
-
Template with conditional blocks
- $vars : array<string|int, mixed>
-
Variable values for condition evaluation
Return values
string —Template with conditionals resolved
splitOutsideQuotes()
Split a condition string by a logical operator, but only when the operator appears outside of quoted strings.
protected
splitOutsideQuotes(string $condition, string $operator) : array<string|int, mixed>
Parameters
- $condition : string
-
The condition string to split
- $operator : string
-
The operator to split on ('||' or '&&')
Return values
array<string|int, mixed> —Parts of the condition (single-element array if operator not found outside quotes)
wrapInEmailHtml()
Wrap HTML body content in email-friendly HTML structure
protected
wrapInEmailHtml(string $htmlBody) : string
Parameters
- $htmlBody : string
-
HTML body content
Return values
string —Complete HTML email