PHP outputs intercepted by $ Smarty. capture. name _ PHP Tutorial

Source: Internet
Author: User
PHP gets the output intercepted by $ Smarty. capture. name. To get the output after $ smarty-display and assign it to the php variable as a string, there are two methods: 1. ob_startob_start (); $ smarty-display (StockNewsgetLeft. tpl); $ stringob_get_cont you want to get the output after $ smarty-> display and assign it to the 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 must capture the output using capture in tpl.

// The principle is the same as that of the first method. check the compiled php file:

// Php $ this-> _ smarty_vars ['capture '] ['capturename'] = ob_get_contents (); ob_end_clean ();?>

// It is not hard to see that the smarty capture uses the php ob_start method.

Conclusion: This technique is useful in some static pages. That is to say, when smarty is used, and a page needs some static and some dynamic outputs, the above method can be used.

This method is used when I use static pages in smarty:

--Static.html

-- Index. php

-- IncludeStatic. tpl

-- Index. tpl

-- NeedStatic. tpl

Index. php // home page, which contains the static and dynamic output parts.

If(file_exists('static.html ')){
// Static page output
// Use capture to capture the output containing the static page
$ Smarty-> assign('filename', 'static.html ');
$ Smarty-> display ('your destatic. tpl ');
// Dynamic output part
$ Num = rand (1, 9 );
$ Smarty-> assign ('num', $ num );
// Display the output index again
$ Smarty-> display ('index. tpl ');
} Else {
// The static page does not exist. continue running and generate a static page.
// Use the above method to dynamically obtain the output of the static part. Method 1 can also be used.
Ob_start ();
// Assume that the static array $ array is output 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 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 the static page generated by the static part of the home page.

I am a static page!

IncludeStatic. tpl // if a static page exists, an output is intercepted through the display page (used in index)

{Capture name = staticed}

{Include file = $ filename}

{/Capture}

NeedStatic. tpl // when no static page exists, the static page is dynamically generated. here, the tpl of the static page

{Capture name = staticed}

{Section name = a loop = $ array}

{$ Array [a]}

{/Section}

{/Capture}

Index. tpl // homepage output, including static and dynamic parts. Note: No matter whether static html exists or not, the output is intercepted by capture and used on this page.

I am a homepage

Here is the static part:

{$ Smarty. capture. staticed}

Here is the dynamic part:

{$ Num}

When you do not want to use a separator in php or directly output html tags (it seems that the code is messy =. = !), The preceding two methods can be used to assign the html after display to a php variable for operation.

Values 1, ob_start (); $ smarty-display ("StockNews/getLeft. tpl"); $ string = ob_get_cont...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.