0

如何在 json 数组中返回 Smarty 模板?
例如:
PHP:

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

1 回答 1

1

Smarty 的display()将渲染的模板回显到输出缓冲区。您可能正在寻找fetch()以将呈现的模板作为字符串获取。

<?php
// …
header('Content-Type: application/javascript');
echo json_encode(array(
  'title' => $title,
  'page' => $smarty->fetch('templatename.tpl'),
));
于 2012-03-02T08:31:16.200 回答