2012-03-02 81 views
0

如何返回json數組中的Smarty模板?
例如:
PHP:返回json數組上的smarty模板

$smarty=new Smarty(); 
.... 
$title='some text'; 
echo json_encode(array('title'=>$title,'page'=>$smarty->display('templatename.tpl'))); 

的jQuery:

$.post('pages.php',{id:id,page:page},function(json){ 
    $('.title').text(json.title); 
    $('.content').html(json.page); 
},'json'); 

回答

1

Smarty的display()回聲渲染模板到outbut緩衝區。您可能正在尋找fetch()以將呈現的模板作爲字符串獲取。

<?php 
// … 
header('Content-Type: application/javascript'); 
echo json_encode(array(
    'title' => $title, 
    'page' => $smarty->fetch('templatename.tpl'), 
)); 
+0

謝謝,我會試試。 :) – Eugene 2012-03-02 08:36:05