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 va
<?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');
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.
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.
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.
Have you tried using the heredoc syntax?
$template = <<<TEMPLATE
Welcome $username <br/><br/>
...
</body></html>
TEMPLATE;
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>