Simple PHP Template Parsing
I want to create a simple PHP Class for parsing basic HTML email templates in PHP. Very basic. Pass a PHP array into a function which has a variable containing the Email template HTML with placeholders <> . The PHP Array’s Key will be the variable name in the template and it;s Value will be the desired output when the email HTML is sent as an email. I thought it might be useful for me to create a simple PHP Class that could do this same thing and speed things up by being flexible. So here is some basic example HTML for the Email body. Variables that will need to be replaced in the template with values from PHP variables are wrapped with <>
Account Details
Thank you for registering on our site, your account details are as follows:
Username: >
Password: >
In the above example there are 2 variables that need to be populated. <> and > I would like to be able to simply pass my Class function a PHP Array where the Array key is the variable name and the value is the value that would be populated in my email template. So something like this would be passed into my method/function that parses the email template.
$emailValues = array( 'username' => 'My username value here', 'password' => 'My password value here' ); $emailHtml = new EmailParser($emailValues); echo $emailHtml;
Account Details
Thank you for registering on our site, your account details are as follows:
Username: My username value here
Password: My password value here
I am curious how I could best achieve this? My main question would be how to pass in the PHP Array and have it map out to a variable name to do the replacements. The PHP Array Key would be the Variable name in the template.
Simple PHP Template Parsing
Question: I’m trying to write a very basic template engine for a project and ended up taking the approach described in this article, which write template variables like and to simply use to parse the templates and put in the variable values. The resulting code was this: Using the following extract: I am able to create an array of elements: What I would actually like to do, though, is to retrieve all text nodes but to allow certain HTML tags to be ‘looked over’ .
Simple PHP Template Parsing
I want to create a simple PHP Class for parsing basic HTML email templates in PHP. Very basic. Pass a PHP Array into a function which has a variable containing the Email template HTML with placeholders > . The PHP Array’s Key will be the variable name in the template and it;s Value will be the desired output when the email HTML is sent as an email.
I thought it might be useful for me to create a simple PHP Class that could do this same thing and speed things up by being flexible.
So here is some basic example HTML for the Email body. Variables that will need to be replaced in the template with values from PHP variables are wrapped with >
Account Details
Thank you for registering on our site, your account details are as follows:
Username: >
Password: >
In the above example there are 2 variables that need to be populated. > and >
I would like to be able to simply pass my Class function a PHP array where the Array key is the variable name and the value is the value that would be populated in my email template.
So something like this would be passed into my method/function that parses the email template.
$emailValues = array( 'username' => 'My username value here', 'password' => 'My password value here' ); $emailHtml = new EmailParser($emailValues); echo $emailHtml;
Account Details
Thank you for registering on our site, your account details are as follows:
Username: My username value here
Password: My password value here
I am curious how I could best achieve this? My main question would be how to pass in the PHP Array and have it map out to a variable name to do the replacements. The PHP Array Key would be the Variable name in the template.
It should just be a case of looping through the values and using str_replace on them.
'My username value here', 'password' => 'My password value here' ); $emailHtml = new EmailParser($emailValues); echo $emailHtml->output(); class EmailParser < protected $_openingTag = '>'; protected $_emailValues; protected $_emailHtml = Account Details
Thank you for registering on our site, your account details are as follows:
Username: >
Password: >