There are two ways to get $smarty->display output and assign it as a string to a PHP variable:
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 is name in {capture Name=banner};
Method uses capture capture output in the TPL
And the first principle is the same, look at the compiled PHP to:
PHP $this->_smarty_vars[' capture ' [' capturename '] = ob_get_contents (); Ob_end_clean ();?>
It's not hard to see that Smarty's capture is using the Ob_start method of PHP.
Summary: This technique is useful in partially static pages. That is, when the smarty is used, and a page needs to be partially static and partially dynamically exported, the above method can be used.
When I am in smarty static pages, this method is used:
--static.html
--index.php
--includestatic.tpl
--index.tpl
--needstatic.tpl
index.php//homepage, partial static and dynamic output parts of this page
? Php
if (file_exists (' static.html ')) {
Static page output static page exists
Use capture to intercept output that contains a static page
$smarty->assign (' filename ', ' static.html ');
$smarty->display (' Includestatic.tpl ');
Dynamic output part
$num = rand (1,9);
$smarty->assign (' num ', $num);
Display again, output index
$smarty->display (' Index.tpl ');
}else{
Static pages do not exist to continue running and generate static pages
Here, using the above method, dynamically obtain the output of the static part, the method used here, also can use the method two
Ob_start ();
If you want to $array the output of the static array after display
$smarty->assign (' array ', $array);
$smarty->display ("Needstatic.tpl");
Saving dynamic output to a $string variable
$string = Ob_get_contents ();
Ob_end_clean ();
Generate a static page
$handle = fopen (' static.html ', ' WB ');
Fwrite ($handle, $string);
Fclose ($handle);
Dynamic output part
$num = rand (1,9);
$smarty->assign (' num ', $num);
Output Index
$smarty->display (' Index.tpl ');
}
?>
static.html//This page is a static page generated by the static section of the home page
I am a static page!
INCLUDESTATIC.TPL//If there is a static page, this page intercepts an output via display (used in index)
{Capture name=staticed}
{Include file= $filename}
{/capture}
NEEDSTATIC.TPL//No static pages, dynamic generation of static page, here is the static part of the main page TPL
{Capture name=staticed}
{section name=a loop= $array}
{$array [a]}
{/section}
{/capture}
INDEX.TPL//home output, including static and dynamic parts. Note: Regardless of whether the static HTML exists, it will intercept the output via capture and use this page.
I am the home page
Here is the static section:
{$smarty. capture.staticed}
Here is the dynamic section:
{$num}
When you do not want to use the qualifier in PHP or output HTML tags directly (so that the code is very messy =.=!), you can assign the display HTML to a PHP variable in both ways to manipulate