Home > Backend Development > PHP Tutorial > php template_PHP tutorial

php template_PHP tutorial

WBOY
Release: 2016-07-13 10:59:20
Original
858 people have browsed it

Template is actually the separation of php and html. Some people also say mvc or something.
PHP handler saves dynamic content into variables, and then outputs the contents of these variables through templates. This is the template
You can
$rows = $db->getAll($sql);
require "tpl.html";
to output.
You can too
$tpl->setVar("rows",$rows);
$tpl->display("tpl.html");
To output, in short, it is the content in the output variable.

I feel the template should have the following features
1. It is the html page
2. Get it as soon as you see it
3. Simplified writing and easy to expand. (This is very important, actually just change to a simple writing form)
There are other smarty custom functions, and there are other templates with similar functions. For example {article.addtime:date:Y-m-d} {article.content:nl2br:}
If the above three points can be achieved, I feel it will be a good template.
Many people say to use php directly to make html templates, but I am opposed to this form
1. The php code cannot be seen.
2. Writing is too complicated and troublesome.
If it looks troublesome and writing is troublesome, what should I use the template for, to make a vase?

The template of discuz is a good template, but unfortunately it is not very good at points 2 and 3. I will also make a template myself. I still use it now and add whatever is missing. To me, it is a good template.
1. It is the html page
2. You get what you see, what you see is what the output looks like. Unlike many people who create href="$cssPath/css.css", it is definitely difficult to change the template art of a div+css page.
3. Simplified writing is actually the abbreviation of function. Just like the above, for example, if the publishing time needs to be formatted, you can change it arbitrarily, for example, if the content needs to be changed from a carriage return to a line feed, for example, if the content needs to be moved to an editor. {content:htmleditor:}, for example. . . . , he is an abbreviation for doing so, and everything will be done with half the effort. Remember the call to that page? For example, 8 latest products are displayed, the latest. . . Showing 10. (Many so-called cms use tags)
In fact, to put it bluntly, it is a foreach, passing in parameters and returning a two-dimensional array.
So there is

{product.productname:cut:20} ....

One day the customer’s requirements changed and they wanted to use the essence product, so

Became

I think this is simplification. All the artists need to understand is that this should be a loop. So far, none of the artists I work with can change my template, even though he doesn't understand programming.
4. Template it. If you want to pursue speed, cache it.
Does caching really work? Why should I cache?
Many people say that setting the cache time and html caching will do the trick. Can this be called caching? If it is a dynamic web page, it says
Welcome, {username}, can you still cache it? You said no caching, but there are too many calls below, what should I do?
At this time, partial caching is used. What is partial caching is the part of the page that is cached.
In fact, to put it bluntly, they are just those calls.

To call this kind of place, if the home page displays the latest 5 articles in 20 categories, then when you open the page, just calling these articles will require 20 queries. Partial cache

I add a # to loop and let it be cached.
If I add 20 #, the call on the homepage will no longer be able to query the database. It seems that in some places, the latest one must be called every time. What should I do? Just remove the #.

A template is a template. Although it is only responsible for display, it is not necessarily unimportant. Making the template simple is something we should consider.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/631906.htmlTechArticleThe template is actually the separation of php and html. Some people also say mvc or something. The php handler saves dynamic content into variables, and then outputs the contents of these variables through templates. This is the template...
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template