2010-05-25 75 views
2

我正在設計我的html電子郵件,這些都是包含變量的html塊,我可以將其存儲在$ template變量中。創建PHP模板的方法(即帶變量的html)?

我的問題隨存儲在變量部分。把我所有的html加入到php中,這使得它在工作中變得非常痛苦。

例如,下面的代碼是罰款,一個簡單的電子郵件,但一旦我開始變得嵌套表等它要得到真正的混亂...

$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>'; 

有沒有辦法我仍然可以保存HTML中的$ VAR,但不必寫這樣的?

回答

4

您是否嘗試過使用heredoc語法?

$template = <<<TEMPLATE 
Welcome $username <br/><br/> 
... 
</body></html> 
TEMPLATE; 
+0

哦那是什麼?我會查找它! – Haroldo 2010-05-25 14:46:37

+0

是什麼? http://www.phpf1.com/tutorial/php-heredoc-syntax.html – Haroldo 2010-05-25 14:48:10

+0

@Haroldo,正好! – 2010-05-25 16:57:37

2

您可以使用雙引號,Heredoc或Newdoc。

例如(雙引號字符串):

$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>"; 

或(定界符)

$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; 

參見http://php.net/manual/en/language.types.string.php爲了在字符串處理變量的不同的方法。

+0

這是完美的作品,看到因爲它只會是我使用的模板,我很高興與heredoc – Haroldo 2010-05-25 15:07:02

8

對於電子郵件,我很喜歡基於文件的模板和一個非常基本的模板解析器。其中一個優點是客戶和領域專家可以閱讀甚至編輯文本。另一個是你不依賴變量作用域,就像heredoc一樣。我做這樣的事情:

文本文件,電子郵件模板:

Welcome, [Username] 

Thank you for creating an account. 
Please ... etc. 

PHP客戶端代碼:

$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); 
} 

當然,你就會有麻煩,如果您的電子郵件需要包含文本[用戶名],但你可以想出你自己的僞代碼約定。對於HTML或更復雜的電子郵件,例如循環和條件,您可以擴展這個想法,但使用模板引擎更簡單更安全。我喜歡PHPTAL,但它拒絕做純文本。

編輯:對於電子郵件,您可能需要純文本版本以及HTML版本。儘管使用函數或方法加載文件並進行替換,但很容易添加第二種格式。

+0

我喜歡這種自制的方法,應該使我想象的非常可讀的模板。 – Haroldo 2010-05-25 15:05:10

+1

甚至不一定是一個文件。您可以將它們存儲在數據庫中。無論哪種方式,您都可以給「客戶端」一個簡單的用戶界面來編輯它們。 – 2010-05-25 16:58:46

4
<?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` 

有關更多信息,請參見http://www.php.net/manual/en/ref.outcontrol.php對此事

更新:(貸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(); 
} 
在要加載此模板腳本

$template = load_template_to_string('template_file.php'); 
+1

使用此方法,您還可以將模板放入單獨的文件中。如果你決定這樣做,編寫一個函數來執行ob_start/ob_get_content/include將是一個好主意。 – Ycros 2010-05-25 15:01:32

+1

它是ob_get_content ** s **();) – Jos 2017-04-14 06:35:16

0

不要把你的模板PHP。將來您可能需要在其他平臺上支持相同的模板。

我們所做的是我們使用SSI(服務器端包含)echo命令向HTML中注入變量。如果您沒有SSI支持,您可以簡單地用正則表達式替換PHP中的變量。模板看起來像這樣,

<h1>Welcome <!--#echo var="USERNAME" -->,</h1> 

其中USERNAME是變量名稱。這看起來比簡單地將PHP變量放在模板中更加冗長,但是您將長期受益。

0

如果您不反對使用框架件,請轉至Zend_View

使用Zend_View,你可以在純PHP中編寫你的模板,並且它還帶有很多幫助器來生成HTML輸出。


您也可以使用包括()提取物()函數來創建自己的模板機制:

$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>