2011-01-29 8 views
0

外部テンプレート(html + php)ファイルを読み込む必要があり、それを介して印刷する特定の値を提供する必要があるソリューションを開発中です。外部HTMLテンプレートに選択した変数を登録し、結果を変数に取得します。

セキュリティ上の理由から、現在のスコープ内のすべての変数にテンプレートでアクセスさせたくありません。

テンプレートからレンダリングされたすべての結果を、ページ本文に直接印刷するのではなく、変数に格納する文字列として返す必要があります。

答えて

1

output bufferingを使用できます。

// start output buffering, nothing will be printed out to the screen from now. 
ob_start(); 

// this will not print to the screen as usual because of ob_start(). 
include('my/template/file.php'); 

// ob_get_contents() will return everything that should have been sent to the screen since ob_start was called. 
$template_contents = ob_get_contents(); 

$final_contents = doMyStringReplaceFunction($template_contents, $arrayOfSubstitutions); 

これは一度にon_start()にコールバックを提供することもできます。

ob_start('myCallbackFunction'); 
include('my/template/file.php'); 

// this will pass the contents of the include to myCallbackFunction() then echo to the screen. 
ob_end_flush(); 
関連する問題