5

I'm designing my html emails, these are to be a block of html containing variables that i can store in a $template variable.

My problem comes with the storing in the variable part. putting all my html into php makes it a pain in the bum to work with.

for example, the below code is fine for a simple email but once i start getting nested tables etc its going to get really confusing...

$template.= 'Welcome ' . $username . '<br /><br /><br />';
$template.= 'Thank-you for creating an account <br /><br />';
$template.= 'Please confirm your account by click the link below! <br /><br />';
$template.= '<a href="' . $sitepath . '?email=' . $email . '&conf_key=' . $key . '" style="color: #03110A;"><font size="5" font-family="Verdana, Geneva, sans-serif" color="#03110A">' . $key . '</font></a>';
$template.='</body></html>';

is there a way i can still store the html in a $var but not have to write it like this?

6 Answers 6

13

For emails, I'm a big fan of file-based templates and a really basic template parser. One advantages of this is that clients and domain experts can read and even edit the text. The other is that you're not relying on variable scope, like with heredoc. I do something like this:

Text file with email template:

Welcome, [Username]

Thank you for creating an account.
Please ... etc.

PHP client code:

$templateData = array ('Username'=>$username...); // get this from a db or something in practice
$emailBody = file_get_contents ($templateFilePath);// read in the template file from above
foreach ($templateData as $key => $value){
  $emailBody = str_replace ("[$key]", $value, $emailBody);
}

Of course, you'll be in trouble if your email needs to contain the text [Username], but you can come up with your own pseudocode convention. For html or more complicated emails with things like loops and conditions, you could extend this idea, but it's easier and safer to use a template engine. I like PHPTAL, but it refuses to do plain text.

EDIT: For emails, you probably want a plain text version as well as an HTML version. Using functions or methods to load the files and do the substitution makes adding the second format pretty painless, though.

Sign up to request clarification or add additional context in comments.

2 Comments

i like this homemade approach, should make for very readable templates i imagine.
Doesn't even have to be a file. You can store them in the DB. Either way, you can give the "client" a simple UI to edit them.
11

Have you tried using the heredoc syntax?

$template = <<<TEMPLATE
Welcome $username <br/><br/>
...
</body></html>
TEMPLATE;

2 Comments

ooh what's that? i'll look it up!
PHP heredoc is great! If you use it to create HTML templates that you want copy and paste, and that are easy to read I suggest to output the string this way: echo "<pre>".htmlentities($template)."</pre>";
7
<?php ob_start(); ?>
Welcome <?= $username ?><br /><br /><br />
Thank-you for creating an account <br /><br />
Please confirm your account by click the link below! <br /><br />
<a href="<?= $sitepath ?>?email=<?= $email ?>&conf_key=<?= $key ?>" style="color: #03110A;"><font size="5" font-family="Verdana, Geneva, sans-serif" color="#03110A"><?= $key ?></font></a>
</body></html>
<?php

$template = ob_get_content();

// to output the data:
ob_end_flush();
// or
echo $template;
// or whatever you want to do else with `$template`

See http://www.php.net/manual/en/ref.outcontrol.php for more information on this matter

Update: (Credits to Ycros)

template_file.php:

<html><head></head><body>
Welcome <?= $username ?><br /><br /><br />
Thank-you for creating an account <br /><br />
Please confirm your account by click the link below! <br /><br />
<a href="<?= $sitepath ?>?email=<?= $email ?>&conf_key=<?= $key ?>" style="color: #03110A;"><font size="5" font-family="Verdana, Geneva, sans-serif" color="#03110A"><?= $key ?></font></a>
</body></html>

some_library_file.php

<?php
function load_template_to_string($file_name) {
  ob_start();
  include $file_name;
  return ob_get_content();
}

in the script where you want to load this template:

$template = load_template_to_string('template_file.php');

2 Comments

With this method, you can also put the template in a separate file. And it'd be a good idea to write a function to do the ob_start/ob_get_content/include if you decide to do it that way.
It is ob_get_content**s**() ;)
2

you can use double quote, Heredoc, or Newdoc.

for example (double quoted strings):

$template.= "Welcome $username <br /><br /><br />
Thank-you for creating an account <br /><br />
Please confirm your account by click the link below! <br /><br />
<a href='${sitepath}?email=${email}&conf_key='$key' style="color: #03110A;"><font size='5' font-family="Verdana, Geneva, sans-serif" color='#03110A'>${key}</font></a>
</body></html>";

or (Heredoc)

$template = <<<EOL
Welcome $username <br /><br /><br />
Thank-you for creating an account <br /><br />
Please confirm your account by click the link below! <br /><br />
<a href='${sitepath}?email=${email}&conf_key='$key' style="color: #03110A;"><font size="5" font-family="Verdana, Geneva, sans-serif" color="#03110A">${key}</font></a>
</body></html>
EOL;

See http://php.net/manual/en/language.types.string.php For different methods of handling variables in strings.

1 Comment

this works perfectly, and seeing as it will only be me using the templates, im pretty happy with heredoc
0

Don't tie your template to PHP. You may need to support the same template on other platforms in the future.

What we do is that we use SSI (Server Side Include) echo command to inject variables into HTML. If you don't have SSI support, you can simply replace the vars in PHP with regular expressions. The template looks like this,

<h1>Welcome <!--#echo var="USERNAME" -->,</h1>

Where USERNAME is the variable name. This looks more verbose than simply putting PHP vars in the template but you will benefit in long run.

Comments

0

If you are not against using a framework piece for this, go for Zend_View.

With Zend_View you can write your templates in pure PHP, and it also comes with lots of helpers to generate HTML output.


You could also use the include() and extract() functions to create your own template mechanism :

$model = array('foo' => 'bar', 'baz' => array('bat', 'qux'));
extract($model);
include('template.php');

<p><?php echo $foo ?></p>
<ul>
    <?php foreach ($baz as $item) : ?>
    <li><?php echo $item ?></li>
    <?php endforeach; ?>
</ul>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.