Home  >  Article  >  Backend Development  >  Obtain the output intercepted by $Smarty.capture.name in PHP_PHP tutorial

Obtain the output intercepted by $Smarty.capture.name in PHP_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 14:53:06870browse

If you want to get the output after $smarty->display and assign it to a php variable as a string, there are two methods:

 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 the name in {capture name=banner};

 //The method needs to be in Use capture in tpl to capture output

//The principle is the same as the first one, check the compiled php:

//php $this->_smarty_vars['capture'] ['captureName'] = ob_get_contents(); ob_end_clean(); ?>

// It is not difficult to see that smarty’s capture uses the ob_start method of php

Summary: This technique is Very useful in some static pages. In other words, when smarty is used and a certain page needs to be partially static and partially dynamically output, the above method can be used.

When I create a static page in smarty, I use this method:

--static.html

--index.php

--includeStatic .tpl

 --index.tpl

 --needStatic.tpl

 index.php //Home page, this page is divided into a static part and a dynamic output part

if(file_exists('static.html')){
//There is a static page and output a static page
//Use capture to intercept the output containing the 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{
//If there is no static page, continue running and generate a static page
//The above method is used here to dynamically obtain the output of the static part. Method 1 used here, You can also use method two
ob_start();
//If you want the output of the static array $array after display
$smarty->assign('array',$array);
$smarty->display("needStatic.tpl");
//Save the dynamic output content to the $string variable
$string = ob_get_contents();
ob_end_clean();
// Generate 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 part of the homepage

I am a static page!

 includeStatic.tpl //If there is a static page, capture an output (used in the index) by displaying this page

 {capture name=staticed}

 {include file=$ filename}

 {/capture}

 needStatic.tpl //When there is no static page, dynamically generate a static page, here is the tpl

of the static part of the homepage {capture name=staticed}

 {section name=a loop=$array}

 {$array[a]}

 {/section}

 {/capture}

 index.tpl //Home page output, including static and dynamic parts. Note: Regardless of whether the static html exists, the output will be captured through capture and used on this page.

I am the homepage

This is the static part:

{$smarty.capture.staticed}

This is the dynamic part:

{$num}

When you don’t want to use delimiters in php or directly output html tags (this makes the code very messy =.=!), you can use the above two methods to assign the displayed html to Give a php variable for manipulation

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/371446.htmlTechArticleThere are two ways to get the output of $smarty-display and assign it to a php variable as a string: 1. ob_start ob_start(); $smarty-display("StockNews/getLeft.tpl"); $string = ob_get_cont...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn