开发者

How to set ${var_name}

We are developing a mail system, and we would like to allow user to ad开发者_开发技巧d custom greeting while creating message.. for example, check following...

we will set this variable(var_name) in php.

Hello ${var_name},
This is test message.

we are not using any framework.


What about str_replace?

$text = str_replace('${var_name}', $var_name, $text);


I'd use PHP as the templating language it is:

Hello <?php echo $name; ?>,
This is a test message.

Then you can replace them like this:

function render($template, $vars = array()) {
    extract($vars, EXTR_SKIP);
    ob_start();
    include $template;
    return ob_get_clean();
}

echo render('email.tmpl', array('name' => 'Foo'));


One way is to do the following:

Create a separate .php file for each email template, like so:

//email_text.php
Hello <?php echo $name ?>, <br/>
How are you doing?<br/>
Your truly,<br/>
<?php echo $author ?>

In the page which is sending the emails, you do something like this -

$name = 'Kevin';
$author = 'Freddy';  
ob_start();
include('email_text.php');
$output = ob_get_clean();
//$output now contains your email message with $name and $author substituted


$var_name = 'Powtac';

// ...

$template = "Hello ${var_name},
This is test message.";

echo $template;
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜