2013-07-03 97 views
0

我使用的是symfony 1.4,smarty和LAMP 我想知道什麼可能是我的最佳優化方法。用於發送電子郵件的Smarty優化

我的電子郵件模板分爲三個部分:

  1. 頭(靜態)
  2. 機構(可以是靜態的或變量)
  3. 頁腳(非-static其有一個退訂鏈接和我知道通過一個唯一的ID)郵件模板的

代碼

~$commonheader 
~$body 
~footer 

我使用頁眉頁眉和正文部分,這意味着永遠郵件所有這些將被解析。

我應該怎麼做靜態部分應該不會再次解析和再次

+0

如果你使用Symfony,你有沒有考慮Twig爲你的模板? –

+0

我不能使用Twig,我現在不能更改代碼庫 – chicharito

回答

2

您應該啓用Smarty的緩存(見http://www.smarty.net/docsv2/en/caching),並用刀片,而不是包括。正如Smarty文檔所說:

模板的某些部分可能未被高速緩存。如果您打開了緩存,{insert}標籤將不會被緩存。每次創建頁面時,它們都會動態運行,即使在緩存頁面中也是如此。這個工程很好的東西像橫幅,民意調查,天氣實況,搜索結果,用戶反饋區域等

所以,一個例子將是:

test.php的

<? 
require_once("lib/smarty/Smarty.class.php"); 
$smarty = new Smarty(); 
$smarty->setTemplateDir('templates'); 
$smarty->setCompileDir('templates_c'); 
$smarty->setCacheDir('templates_cache'); 
$smarty->setConfigDir('templates_config'); 
$smarty->caching = 1; 
$smarty->compile_check = true; 

function insert_Body() 
{ 
    global $smarty; 
    ob_start(); 
    $smarty->display("search.tpl"); 
    $ret = ob_get_contents(); 
    ob_end_clean(); 
    return $ret; 
} 

$smarty->display("test.tpl"); 

test.tpl

<h1>Smarty test</h1> 
{insert name="body"} 
<hr> 

插入標記將看起來是一個名爲「insert_」的函數的php文件,後面跟着名稱傳遞。在這個例子中,它是insert_Body();

您也可以將變量傳遞給函數,例如要包含的文件名。從Smarty的文檔(http://www.smarty.net/docsv2/en/language.function.insert.tpl

{insert name="getBanner" lid=#banner_location_id# sid=#site_id#} 

的Smarty將調用此函數:insert_getBanner(陣列( 「蓋」=> 「12345」, 「SID」=> 「67890」));並在{insert}標記的位置 中顯示返回的結果。

請注意,該值應該由函數返回,但不回顯。這就是爲什麼我使用ob_start()& ob_end_clean()和ob_get_contents()