Common Learning notes for php mvc framework view

Source: Internet
Author: User

Let's talk about view and view today... Before you understand it, you may think that the template engine is amazing. After you understand it, you will think that it is so simple...

For the template engine, the general principle is to replace the language of the template engine with an executable language, and then output the variables...

Because PHP itself is a good template engine, it is very convenient and easy to use the native template engine...


Index.html

The code is as follows: Copy code
 
<Html>
<Head>
<Title> <? Php echo $ title?> </Title>
</Head>
<Body>
<H1> <? Php echo $ content?> </H1>
 
<? Php if (! Empty ($ arr) {?>
<Ul>
<? Php foreach ($ arr as $ value) {?>
<Li> <? Php echo $ value?> </Li>
<? Php }?>
</Ul>
<? Php }?>
 
</Body>
</Html>

The above html page uses the native php template engine. When you include this page, the server will process the PHP code in html.

You may ask, can I customize the template language? The answer is yes of course. As I mentioned above, the template engine language only needs to be replaced with the standard PHP language...

Index_2.html

 

The code is as follows: Copy code
<Html>
<Head>
<Title> <? Php echo $ title?> </Title>
</Head>
<Body>
<H1> <? Php echo $ content?> </H1>
 
<Ifexist arr>
<Ul>
<For value = arr>
<Li> <echo value> </li>
<Endfor>
</Ul>
<Endexist>
 
</Body>
</Html>

Looking at this page, what is the difference between index.html and index.html? That's right. In the loop, we are not using the standard php language, but defining several template languages according to our preferences, one is <ifexist xxx>. This is used to determine whether the xxx variable exists and is not empty. The variable <for xx1 = xx2> that follows is the xx2 variable of loop iteration, <echo xx> in the middle is the output xx variable.

How can we replace it with a standard language? This can be replaced with a regular expression, because string replacement cannot meet our needs.

The code is as follows: Copy code
 
$ Pattern = array (
'/<Ifexists + (w +)>/I ',
'/<Fors + (w +) s = s (w +)> /',
'/<Echos + (w +)> /'
);
 
$ Replacement = array (
'<? Php if (! Empty ($ \ 1) {?> ',
'<? Php foreach ($ \ 2 as $ \ 1) {?> ',
'<? Php echo $ \ 1?> '
);
 
$ Content = preg_replace ($ pattern, $ replacement, $ content );
 
$ Search = array ('<endexist>', '<endfor> ');
 
 
$ Content = str_replace ($ search, '<? Php }?> ', $ Content );

 

The above regular expression can help us convert the label we just defined into the standard p hp language...

This is the case after conversion. See the figure.

 

Can I output the converted data? The answer is no, because when you get the content of the html file, php has already explained it. At that time, you haven't replaced it with the standard php language, so what you output now, the php code is not executed... However, we can use the php eval function to forcibly execute the code.

The code is as follows: Copy code

$ Content = eval ('?> '. $ Content );

Note: Yes?>. $ Content or not, php regards the html as the execution of php code, and an error will occur...

Refresh it. OK. You can execute it ~~~

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.