HiveBrain v1.2.0
Get Started
← Back to all entries
patternphpMinor

Email Template Parser PHP Class

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
templateemailparserphpclass

Problem

This is a very basic PHP class that I am working on which will allow me to provide an HTML template in the form of a string saved to a variable or into an actual template file. I can then pass into my PHP class an array where the array KEYS will be used to find and replace variables in the template HTML.

The template will define variables in this format {{first_name}}, so my PHP array would have a key of first_name and its value would be parsed into the final result HTML.

Example PHP array used to set and replace variables in the templates HTML...

$emailValues = array(
    'first_name' => 'Jason',
    'last_name' => 'Davis'
);


This PHP array would then find and replace all occurrences of {{first_name}} with Jason and {{last_name}} with Davis

I have coded this functionality into countless projects over the years but I generally have defined every single variable that get replaced in the template manually.

Purpose

I wanted a reusable method of doing this and even better, the flexibility to not have to define what variable are allowed in a template. With this class I can now use it in any project and use any variable I want in a template with {{varName}} instead of being confined to a list of variables. It can now have unlimited variables and I don't have to manually set each one up. Simply call the variable in my template and assign a matching KEY name in my PHP array of replacement values. I love the concept of it!

PHP Email Template Parsing Class

```
_emailValues = $emailValues;
$this->_isTemplateFile = $isTemplateFile;

if($this->_isTemplateFile){
// Template HTML is stored in a File
try
{
if(file_exists($emailTemplate)){
$this->_emailTemplate = file_get_contents($emailTemplate);
}else{
throw new Exception('ERROR: Invalid Email Template Filepath');
}
}
catch(

Solution

Yes, this is similar to what I do.

I haven't restricted my class to emails, I can use it on any string or template file. You could do that too. There's no good reason for the restriction you impose.

I have noticed that you provide all the arguments when you make the parser object. Is that wise? Think about it:

1: If you were to supply the $emailTemplate to output() you could reuse the class for different template strings.

2: Or if you supplied the $emailValues to output() you could change them up to the point where the output is needed.

In other words, don't supply all arguments when you make the class, supply them when they're really needed. It means you don't need to store them in the object, and you can reuse the object. In this case I would choose to supply the $emailValues to the __construct since they probably won't change, and use one object for all email templates.

Instead of using an array, outside of this class, to collect all the $emailValues, you could create the parser object first, and use a method to attach template variables to it. This means you don't have to store them in two places:

public function setVar($name,$value)


Now you're really starting to use the object and you're compartmentalizing methods and variables in a class.

I like the way you define the opening and closing tags, instead of using '{{' and '}}' every time. You could turn these into real constants, by using define(), or make them programmable. You have a sort of in between solution, they're not constant, nor programmable.

You store $isTemplateFile in the object, but you don't need to.

You could split you long __construct() into two methods: One for reading a file, and one for accepting a template string. Notice that one could be used by the other!

Code Snippets

public function setVar($name,$value)

Context

StackExchange Code Review Q#83624, answer score: 2

Revisions (0)

No revisions yet.