$smarty->display の後の出力を取得し、文字列として php 変数に割り当てる方法は 2 つあります:
1. ob_start
ob_start();
$smarty->display("StockNews /getLeft.tpl");
$string = ob_get_contents();
ob_end_clean();
2. $smarty->_smarty_vars['capture']['captureName'];
$smarty-> display("StockNews/getLeft.tpl");
$string = $smarty->_smarty_vars['capture']['captureName'];
//captureName は {capture name=banner} 内の名前です;
//メソッドは出力をキャプチャするためにtplでキャプチャを使用する必要があります
//原理は最初のものと同じで、コンパイルされたphpを確認してください:
//php $this->_smarty_vars['capture'] ['captureName'] = ob_get_contents(); ob_end_clean(); ?>
// Smarty のキャプチャが php
の ob_start メソッドを使用していることは、難しくありません。 要約: この手法は、一部の静的ページで非常に役立ちます。つまり、smarty を使用していて、特定のページを部分的に静的に、部分的に動的に出力する必要がある場合、上記の方法を使用できます。
smartyで静的ページを作成するときは、
--static.html
--index.php
--includeStatic.tpl
--index.tpl
--needStaticという方法を使います。 tpl
index.php //ホームページ、このページは静的部分と動的出力部分に分かれています
if(file_exists('static.html')){
//静的ページと、静的ページが出力されます
/ /静的ページをインクルードした後、キャプチャを使用して出力をインターセプトします
$smarty->assign('filename','static.html');
$smarty->display('includeStatic.tpl ');
//動的出力部分
$num = rand(1,9);
$smarty->assign('num',$num );
//再度表示、インデックスを出力
$smarty-> display('index.tpl');
}else{
//静的ページがない場合は、実行を継続して静的ページを生成します
//ここでは、静的部分の出力を動的に取得するために上記のメソッドが使用されます。ここでは 1 つを使用していますが、2 つ目の方法も使用できます
ob_start ();
//表示後に静的配列 $array の出力が必要な場合
$smarty->assign('array',$array);
$smarty->display("needStatic.tpl");
/ /動的出力コンテンツを $string 変数に保存します
$string = ob_get_contents();
ob_end_clean();
//静的ページを生成します
$handle = fopen('static.html','wb');
fwrite($ handle,$string);
fclose($handle);
//動的出力部分
$num = rand(1,9);
$ Smarty->assign('num',$num );
//インデックスを出力
$smarty->display('index.tpl');
}
?>
static.html //このページは静的ページはホームページの静的部分から生成されます
私は静的ページです!
includeStatic. tpl //静的ページがある場合、このページを表示することで出力をインターセプトします
{キャプチャ名=staticed}
{include file=$filename}
{/capture}
needStatic. tpl //静的ページがない場合、動的に静的ページを生成するホームページ
のtplです。{capture name=staticed}
{section name=a loop=$array}
{$array[ a]}
{/section}
{/capture}
index.tpl //ホームページの出力、静的部分と動的部分を含みます。注: 静的 HTML が存在するかどうかに関係なく、出力はキャプチャを通じてキャプチャされ、このページで使用されます。
私がホームページです
ここが静的部分です:
{$smarty.capture.staticed}
ここが動的部分です:
{$num}
区切り文字を使いたくないときphpまたはhtmlを直接出力 マークするとき(コードが汚くなってしまいます =.=!)、上記2つの方法を使って表示されているhtmlをphp変数に代入すると操作が楽になります