Code Monkey home page Code Monkey logo

php-email-template-parser-class's Introduction

PHP-Email-Template-Parser-Class

This is a very basic PHP Class to Parse variables in a template for use as an HTML Email.

The file demo.php is commented and pretty self explanatory on how to use the PHP Class however I will also explain it here.

There are 2 sources for your HTML Template.

  1. External Template file which will be included and parsed.

  2. Inline Template saved and stored as a String in a PHP variable.

There are 2 methods for setting Template Variables. 1 by 1 or passing in an Array

  1. Calling setVar($variable_key, $variable_value) where $variable_key = {{VAR_NAME}} in your Template.

  2. Calling setVars($variable_array) and passing in an array of key=template variable name and value=the value to replace in the final HTML output.

I will show the usage for both versions below...


External Template File Method

email-template.tpl

<html>
<body>
<h1>Account Details Using External Template File</h1>
<p>Thank you for registering on our site, your account details are as follows:<br>
Username: {{username}}<br>
Password: {{password}} </p>
</body>
</html>

demo.php

<?php
require('emailTemplateParser.php');

// Set the file path to our external Template File.
$emailTemplate = 'email-template.tpl';

// Assign our replacement VALUES to a matching KEY where the KEY
// is the Variable Placeholder name used in the Template file!
$emailValues = array(
  'username' => 'My username value here',
  'password' => 'My password'
);

// Instantiate our Email Template Parser Class
// Passing in the Template file path
$emailHtml = new EmailTemplateParser($emailTemplate);

// Pass in an ARRAY of Variables to replace in the template file.
$emailHtml->setVars($emailValues);

// Print the Parsed Email Template to the screen.  Our Variable
// placeholders in the template are now parsed with real values!
echo $emailHtml->output();

Inline Template Method

demo.php

<?php
require('emailTemplateParser.php');

// HTML Template saved to the PHP $emailTemplateInline variable
// instead of an external Template File!.
$emailTemplateInline = <<<HTML
<html>
<body>
<h1>Account Details Using Inline Template</h1>
<p>Thank you for registering on our site, your account details are as follows:<br>
Username: {{username}}<br>
Password: {{password}} </p>
</body>
</html>
HTML;

// Instantiate our Email Template Parser Class
// Passing in the Template variable
$emailHtml = new EmailTemplateParser($emailTemplateInline);

// Assign our replacement VALUES 1 by 1
$emailHtml->setVar('username', 'JasonDavis');
$emailHtml->setVar('password', 'hfgjhfghsh');

// Print the Parsed Email Template to the screen.  Our Variable
// placeholders in the template are now parsed with real values!
echo $emailHtml->output();

Other Info

That pretty much sums up the current usage and functionality/features.

Please feel free to send pull request or submit issues if they arise. This was a quick and dirty little project thrown together to fix a need on another project in a night!

Feel free to visit my Web Development Blog - http://www.CodeDevelopr.com

or

Check out my Web Development and Marketing Studio - https://www.ApolloWebStudio.com

Follow me on Twitter @JasonDavisFL

php-email-template-parser-class's People

Contributors

jasondavis avatar

Stargazers

 avatar Bruno Luiz  avatar Eugenio Miró avatar Israel avatar Lajeesh k avatar 陶俊 avatar Vuong Dinh Ngoc avatar Ryan McQuen avatar Alex King avatar Saifullah Khan avatar Nam Nguyen avatar Giovanni avatar Geoff avatar  avatar

Watchers

 avatar James Cloos avatar  avatar

php-email-template-parser-class's Issues

Composer

HELLo,

Care to spend 3 minutes and make this available on packagist so I can include it with composer?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.