Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

method for creating PHP templates (ie html with variables)?

Tags:

php

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?

like image 980
Haroldo Avatar asked May 25 '10 14:05

Haroldo


3 Answers

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.

like image 94
grossvogel Avatar answered Oct 27 '22 00:10

grossvogel


Have you tried using the heredoc syntax?

$template = <<<TEMPLATE
Welcome $username <br/><br/>
...
</body></html>
TEMPLATE;
like image 38
Marcus Adams Avatar answered Oct 27 '22 01:10

Marcus Adams


<?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');
like image 28
jigfox Avatar answered Oct 27 '22 00:10

jigfox