开发者

Can I avoid using a templating engine built on top of PHP for logic inside of an email?

开发者 https://www.devze.com 2023-02-18 15:35 出处:网络
I\'d like to save a template but I need logic inside it. This is an HTML email, btw so that\'s why I\'m using tables.

I'd like to save a template but I need logic inside it. This is an HTML email, btw so that's why I'm using tables.

<p>Dear {{first}} {{last}} </p>

<table>



{{ if something }} 
<tr><td></td></tr>

{{ endif }}

</table>

Essentially I'd like to populate {{variables}} with their respective values coming from a database.

I would like to have an if in order to avoid storing 2 versions of the template. Is there some method of doing this that's easier? I'd like to avoid writing a template processor.

Or should I just store 开发者_开发技巧2 versions of the template since they are distinct?


I think the raw PHP templating approach is suitable here. You can define the email template easily like this:

<p>Dear <?=$first?> <?=$last?> </p>

<table>

<? if ($something): ?> 
<tr><td></td></tr>

<? endif; ?>

</table>

And to use it write a minimal wrapper function:

function email_template($file, $vars) {
    ob_start();
    extract($vars);
    include($file);
    return ob_get_contents() . (ob_end_clean() ? "" : "");
}

You can adapt the parameter passing certainly, but a basic invocation would be:

$text = email_template("greetz.php", array("first"=>"Mr", ...));
mail($to, $subject, $text, ...);

The advantage is that you can use any complex PHP control structures in such templates, without sacrificing readability because you only need few such instances and variable names anyway.


Firstly let me explain a few things, you may already be aware of these but i will explain for the purposes of other users / readers.

Creating template parser is not a bad idea, as its simply replacing instances if {{key}} with a corresponding value, but when you get into logical interpretations such as {{if a > b && c < d}} your parser will suddenly get a lot more resource hungry.

if the it was just value replacements then its a simple str_repalce with arrays, which is pretty efficient at what it does I would say yes go for it... but im not.

PHP Is built as logical interpreter and as this is the lower end of the language it would be faster and less resource hungry then an interpreter built on top of an interpreter..

what you should be doing is looking into sand-boxing your PHP code to effectively become a individual interpreter as such.

so lets begin with some code and templates, lets start with your sample!

<p>Dear {{first}} {{last}} </p>
<table>
    {{ if something }} 
        <tr><td></td></tr>
    {{ endif }}
</table>

the following code has exactly 139 characters including line breaks and tabs, now look at the PHP version

<p>Dear <? echo $this->getFullName() ?> </p>
<table>
    <? if($this->getSomething()): ?>
        <tr><td></td></tr>
    <? endif; ?>
</table>

the following is 166 characters including line breaks and tabs, this is only 27 characters more then a template parser, so effectively your using more resources to save on 27 characters.

is it really worth 27 characters ?

  • my opinion is no, it totally isn't.

so look how this can be accomplished, you say that you do not want to write your own parser, if I have a punt at what the reason is then I would say its due to the complicity that's involved to make the parser work efficiently. Well the above can be done pretty simply with 2 small classes.

class Template
{
     
}

class TemplateRenderer
{
     
}
  • Now the Template class would be used to set / get items you want to pass to the sandbox such as first-name, last name.
  • The TemplateRenderer is the sandbox, its where the code is compiled and returned as a string.

If you wish for me to continue further and fill the code blocks missing above then just drop a comment.


Is using an existing template processor an option?

I would try Dwoo (content copied from their site):

<?php

// Include the main class, the rest will be automatically loaded
include 'path/to/dwooAutoload.php'; 

// Create the controller, it is reusable and can render multiple templates
$dwoo = new Dwoo(); 

// Create some data
$data = array('a'=>5, 'b'=>6);

// Output the result ... 
$dwoo->output('path/to/index.tpl', $data);
// ... or get it to use it somewhere else
$dwoo->get('path/to/index.tpl', $data);

?>

And an index.tpl file could look like that:

<html>
        <body>
                <h1>{$a}</h1>
                <p>{$b}</p>
        </body>
</html>

or Smarty (copied too):

include('Smarty.class.php');

// create object
$smarty = new Smarty;

// assign some content. This would typically come from
// a database or other source, but we'll use static
// values for the purpose of this example.
$smarty->assign('name', 'george smith');
$smarty->assign('address', '45th & Harris');

// display it
$smarty->display('index.tpl');

index.tpl:

<html>
<head>
<title>Info</title>
</head>
<body>

<pre>
User Information:

Name: {$name}
Address: {$address}
</pre>

</body>
</html>
0

精彩评论

暂无评论...
验证码 换一张
取 消