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?
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.
Have you tried using the heredoc syntax?
$template = <<<TEMPLATE
Welcome $username <br/><br/>
...
</body></html>
TEMPLATE;
<?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');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With