PHP Resource Template--template terminator

Source: Internet
Author: User
Tags functions html page connect variables php file php and php code sin
templates | terminator

"There was a precious template in front of me, I did not cherish, until the others are used, I regret!"
If God can give me another chance, I will say three words to this template--I use you. If you want to know how long I use, I hope so.
--10,000 years. ”
--"Template Terminator" preface

The first time e times crowded, chivalrous tenderness Hero Road

The first contact with the word "PHP template technology" is in a book about Phplib's template class. Because at that time my level is limited,
Unable to understand the code of the template class, so once skipped the chapter, lazy to understand, not the eyes of the mind is not annoying.

Later, a friend of mine asked me about the template, in my mind, it was such an impression, so I have a busy excuse, promised to help him again ... Then I found the book and read it carefully.

It's a php template class, called template, and is an independent part of Phplib. It uses the "assign" method to assign variables, and the core functionality, which is "variable substitution," is done by the PHP function "Preg_replace". I quickly understood the principle of it.

What was my feeling? I feel so excited! Yes, the first time to separate PHP from HTML, this is a novelty feeling! I could not help showing a happy smile, and "hehe" two--at the same time, I found a plmm next to look at me with strange eyes, so I proudly and Tease said: "What to see?" Never seen a handsome man before? haha

But this feeling of happiness, like the sweetness of love, will soon fade away; So the past day, template template class makes me feel uncomfortable.

The most unpleasant thing is to assign this ' action '. Each time to ' Assign variables ', this is not trouble, the trouble is that each time to enter the "So-and-so object->assign" these letters. I hate the monotonous and creative duplication of work, so to describe template template class, it is a word ' annoying ', two word ' annoying ', three word ' annoying annoyance '.

You might ask, if you don't assign assign variables, how do you know which ' variable markers ' need to be replaced? At the time, my intuition told me: "The PHP variable itself is the best ' variable tag '." If we directly assign a value to PHP variables, is not more direct and convenient? Why do you need a few redundant "so-and-so object->assign" boring characters?

So I think, if you can directly use the "to the PHP variable assignment" method to assign variables, that would be good, but also can be beautiful its name yue: "Variable assignment method." At the time, however, I didn't know how to accomplish this little function.

Time is passing day by day. In a rainy morning, in an eye-blind corner, I found a thing-that is, "<<<." Do you know what "<<<" is? -"Only for you, is the best symbol, only you can directly assign value." You are the most capable, just you ... ”

The legendary "<<<" is called the "Heredoc" symbol. This heredoc symbol also exists in other languages, which I will not examine. Let me give you a simple example here:

$title = "This is title";
$text = "This is text";

echo<<< EOT

$text, can use "quote" without "\"


where "EOT" denotes "end of Template", similar to the "EOF" notation method in PHP or C, because EOF represents "end of File". You can choose other symbols instead of EOT, the effect is the same, it only plays a symbolic role, in which any variable, like the variables above this page, directly replace the contents of the variable-this is a very useful feature.

In order to facilitate the narrative, I and you agreed: from echo<<< EOT start to "EOT;" End this paragraph of text, called "Heredoc".

The Heredoc only acts as an output display and belongs to the view layer in MVC. It reminds me of a word that is called "Display logic".
What do you mean "display logic"? The English translation is "View logic". In fact, it is easy to understand, that is, "to see people speak, to hell with the story."

For example:
$title = "This is title";
$text = "This is text";
$who = "Girl";

if ($who = = "Phper") {

echo<<< EOT

$text, can use "quote" without "\"


else if ($who = = "Girl";) {
echo<<< EOT
Hi, $who, I love you~~


The code from if to end is "view logic", or "display logic". The simple thing to do is to process the display, which is "output", that is, to use "echo". Why, then, is there a "logic" behind "show"? Because we're going to
"Talk to people and talk nonsense"! If "Hell to talk, see people say", then you in front of the Ghost ' show ' as a person, in front of people ' show ' as a ghost.

If you are a complete materialist, you have no ghosts in the world, in other words your "view logic" is very simple, so that the direct use of Heredoc can be, do not have to Heredoc before the if,else,for, foreach, and so on ' logical control symbols '.

For the sake of narration, I use the heredoc instead of using any ' logical control notation ' code, called ' Simple View '. Conversely, if you use any kind of ' logical control notation ' before your heredoc, you use ' complex view '.

"Simple View" can be translated as a "plain views", and the corresponding ' complex view ' can be translated as ' complex views '. It is called "complexity" because I assume that the world's people are not always diligent, not always smart, time is not always abundant, gender is not always the same, so some people seem to think it is very complex.

For art, she prefers ' simple view '. She is not only simple-minded, but also Slim, Bo is no brain, emotional, regardless of logic. Her name is "Mei", Arty developed, versatile, dancing, the design of the Web page beautiful amorous, attracted countless admirers want to spit out their own heart, helpless will not do "message book"-so she found the programmer ' small path '.

For programmers, he likes ' complex view ' best. Only ' complex view ' can arouse the interest of ' Xiao Cheng ', before
Can embody its ' hero ' image. He's a real trinket, he's slick, and he's going to use "if" and "switch" statements, and he surprised, he's going to use "for and while" statements, and so on. In front of the emperor, he displayed as "loyal", in front of friends show "righteousness", in front of the beauty show as "amorous." If he does not use ' complex view ', it is absolutely impossible to meet such a complex demand-so in front of the beauty, he is a hero. Since only heard of the "hero Save the United States", has not heard of "bear Save the United States", so the small path and Amy will naturally meet.

A small name is like a man, so the little way is soon enchanted by Amy. Think of the beginning, small process and when ' programmer ' and when ' art ', although can be fully qualified for the work of programmers, but for ' art ' such things, has been quite inferior. However, the small process is also able to deal with the ' two ' relationship-he asked himself to use the art of simple and lively style, in order to avoid weaknesses, and at the same time they are more comfortable with the program, so ' when dad and when Mom ', although a little tired, but because everything in their own ' control ', so instead feel more ' Conveniently '.

Now I have seen Mei, only to know that the world can be so beautiful. He can not help but sigh: This world not only has the +-*/, originally also has the red flower, the green grass, the bird is singing, the butterfly is dancing ... This is because in the play of her flash works, the eyes of the small look are straight. In an instant, the feeling of admiration arises spontaneously, even if the Mei told him to die, he also never quit ... In the small way, Mei Soft voice said: "Small Cheng elder brother, young woman has long heard your PHP Dafa Megatron, in this crowded e era, can and Jsp, tripod, really not a hero ..." The little way immediately elated, excited words can not speak out. Amy Then
said: "The little woman actually has one thing to ask." I was going to ask Microsoft for help because I heard he was rich. Who knows he is a miser, stingy, said what his things can only be used on his site, not allowed to step out of the backyard of his home. As soon as I heard my lungs explode, I vowed to find a generous friend to help. So I was introduced, I found Java. Java He is indeed a very generous friend, not at all Microsoft's stingy and stingy, which I am more satisfied. But when I looked at his faltering pace and listened to his slow voice, thinking that he was a huge fat man, I felt uncomfortable in my heart. Perhaps Java has seen my mind, so he has the delicacy to say good-bye to me. At parting, he said to me, ' I have a half brother, named PHP, he was 3, 4 years old when very thin, I have a prejudice against him, so I hate him, but recently I received his 5-year-old photos, found that he became strong, which he had a little favor. Amy, you can go and find him and see if it's OK. Oh, by the way, PHP is his nickname, and his real name is Xiao Cheng. Farewell to the Java, so I found you ... "Small Cheng can't help but complacent, elated, then said:" Can get girl appreciate, is privilege. If a girl has anything to say, she must do her best and never let her down. ”

In this way, Amy hope that the small help to make a ' message book '. This message, in addition to the basic features, also provides 10 different display
Style. These different styles are mainly done by Amy, because of the huge workload, Mei put her brother ' Coolie ' also called to help. And the small process is responsible for data processing, writing programs.

There is a problem: the small path wants to achieve mutual independence and separation of business logic and presentation logic:

1.php Business processing and HTML page display independent (not excluding the use of PHP tags in html);
2. Can preview the page effect of HTML directly;
3. Preview, the link between the page can be seen, like a do not understand the program designers do the site, all static pages, but the relationship between these static pages is clear;
4. The speed must be passable.

This is the "four principles of the template Engine" presented by the small process. The thought of these principles and requirements reminded him of the dialogue he had with his brother "the Little preface" That night. Small process clearly remember the small preface once said:
"I have not adopted any template technology, because I have never found a template suitable for me, phplib, smarty, Supertpl and so on, all are not suitable, want to write their own, level is not enough, there is no time and energy."

The reason I hate these templates is that they don't show the prototype or the original! Our company's target customers are small and medium, these customers always require that the program is not implemented before you can see all the Web interface, so the company basically focused on the art, and the program is not very important, there is a big problem, because each designer's style is different, Lead to hand in my hands of the interface style strange, difficult to do "batch" processing, really very tired! To make the worst of it, in addition to the individual designers to understand the program, the other is simply ignorant, almost every time I have to first change their interface to embed the program, I have been looking for a simple and easy to use template technology, in the early preview effect, such as interface color, layout, etc., Do not let designers spend too much time to learn (they do not want to spend time to learn), that is, this template and their usual design of the Web page basically no difference, but I have not seen this template technology, it is disappointing! ”

Indeed, the disappointment of the small preface also makes the small path quite sympathetic. While thinking, suddenly came a voice: "Hey, Big Brother, what is it?" It's not about how to bubble mm, is it? Oh.. "The newcomer is the small three younger brother, named ' small clerk '." "Go away, your eldest brother is not so
Merry, from morning to night thinking of the bubble mm ... "What do you think?" Are you so preoccupied? "So the little way to put things through 1510 of
told the little clerk.

Behold this little member is very angry, unexpectedly in English said: "PHP really need these fucking template engins?" No!
Don't waste your time. Pay your time into your business requirements. ”

"What do you Jabber?" asked the little-Cheng. "I mean, PHP's best template language is PHP itself," said the clerk. You don't have to choose something that is tasteless, such as smarty, that is not a pity. ”

Small way: "As you say, PHP the best template language is PHP itself, then your template to meet my template engine four principles?"

1.php Business processing and HTML page display independent (not excluding the use of PHP tags in html);
2. Can preview the page effect of HTML directly, including picture;
3. Preview, the link between the page can be seen, like a do not understand the program designers do the site, all static pages, but the relationship between these static pages is clear;
4. The speed must be passable.

The little clerk was tongue-tied. He thought: "Remember Smarty just appeared when I was an advocate, the so-called own template class also wrote a lot of, the company is still 2 ways to coexist, the other one with a lot of changes in a lot of smarttemplate, but now has been basically deactivated." When I finished writing a forum with smarty, I swore I would never use this rubbish again. So the second time to write the forum is xml+xslt. I've seen a variety of template engine template schemes that make me firm a correct conclusion that ' PHP's best template language is PHP itself ' but when it comes to fully satisfying the four principles of the template engine above, few people have done it. This is true even with PHP itself as the template engine. ”

Then the phone rang ... The little one said to the small way: "The company has something I need to deal with, we have time to talk about this topic." Oh, yes, you can go to talk to a friend of mine, Axgle, he is said to be interested in this topic and have a lot of experience, you may wish to look for him. Bye. "Hey, don't run, his QQ number is AH ... "" "380139552--"

To know the funeral, and listen to let's--

Second resource template turned out, Huashan on the sword and the Clouds

Words at both ends. But said this axgle is going to write a template about the article, suddenly see someone add their own QQ. I saw an additional message that said
"Friend of the small," so accept the request, start chatting.

"Hello Ah axgle, hear your name, Thunderclap piercing Oh." ”
"What is Your Excellency?" ”
"In the next is a small brother, named Xiao Cheng." Heard that you have a lot of research on PHP templates, so special to learn from, but also ask you to advise "
"Polite." Research is not, but a little experience. Everybody talk to each other and learn the best from each other.
"So, what do you think of the four principles of the template engine?" ”
"Haha, what a hero sees is the same!" Oh, I'm sorry--it's great minds.
"So axgle you have any ideas about this, so you can achieve this goal?" ”
"I am writing an article, The Terminator of the template, to discuss the issue in detail," he said. ”
"I do not know how much you have done now, really want to sneak peek ..." ”
"Oh, is now written to the second ' resource template turned out, Huashan on the sword again," until you see this sentence, including periods and quotes. Of course, behind the quotation marks you do not see, because QQ generally do not make quotes. ”
"Yes," he said. Then why is this title the second time? ”
"Because the second time I will introduce the" resource template "of the ins and outs, in addition, my web name is ' Huashan Jian ', and you call ' the clouds ', so it can give readers a perfect account. ”
's So You are so funny, you're turning everyone around. ”
"Sin sin, South no Amitabha ... The family does not play lies. Please let us have a detailed explanation and please be attentive. ”
What is a--1.php resource template?

The ' resources ' here refers to the various auxiliary elements used to create Web pages, such as various images, JS scripts, CSS style documents, etc., which can also include SWF files in Flash, and so on, and so on, ' non-HTML elements ' that you can connect to.

' Resources ' are usually ' generic ' and can recur. For example, the same image can appear on a variety of different pages. So
All of the resource files can be placed in a folder. We might as well name this folder "assets".

Under the Assets folder, to make it easier to differentiate and manage, you can create new folders, such as images, that are different in a variety of ways,
Javascript,css,flash and so on. Its directory structure is as follows:


Such a directory structure is clear and orderly, so that everything becomes messy.

We put any HTML file in the same directory as assets, and you must agree that this is a matter of course:
For example:


Because of this, all paths can use a relative path and can be directly connected to the resource file in assets.

For example index.htm need a logo picture, its path is ' assets/images/logo.gif '.

We stipulate that these HTML files, together with assets, are collectively referred to as ' view files ', or ' template files '. In order to facilitate
To differentiate and manage, we give it a name, such as "Default", and create a new default folder where all of these "template files" are placed.

Because the same data can have different display styles, there can be a variety of views, in other words, there can be many different styles. We can do the same thing, create multiple ' template files ' and name them separately, and deal with the ' default ' folder.

Because there are many different views, the directory structure we see is as follows:


In order to make it easy to differentiate with PHP files, we can put ' default ', ' Other_style ' etc into the same folder, usually named ' template ', but I do not follow this point, but the name is called ' view '. Because I think ' view ' Template "More able to express my thoughts better."
Create a new folder named "View":

And the structure becomes:
So far, the view directory is done.

Then there is the PHP directory problem. This is the core and key, please understand carefully.

We assume that there is a index.php file and put it in a directory at the same level as the ' View ' folder. Now index.php get the data from somewhere, you need to call the Index.htm file for display. We assume that it chooses the ' default ' style, and the corresponding index.htm path to the index.php file is ' view/default/index.htm '.

No matter what template engine you use to handle the current problem, you will find that the ' resource file cannot be shared '.
For example, in the above case, the Logo.gif picture cannot be displayed properly in index.php. Because index.php
And index.htm are not in the same sibling directory, and the index.htm file uses a relative path to connect to the picture.

So what do you do? You may have the following options to solve this problem:

Programme I:

You might say, "Axlge, you're a pig." Don't you know it's okay to put index.php files and index.htm files in a sibling directory? ”

If there is only one style, the Tao is able to do so, but even so, the HTML file and PHP file in the same directory, there is a slight confusion. I think this is not good, because it is inconvenient to modify. After all, we want programmers and artists to have their own ' turf ', and the less they have to relate to each other, the better.

So one of the options can be vetoed.

Programme II:

You might say, "Axlge, you're a pig." You do not know that the corresponding ' resource files ', such as a picture copy to the corresponding directory, is it OK? ”

Yes, it's doable. But not ' graceful '. Because if a picture is modified, the corresponding picture needs to be copied again. Besides, sometimes the number of resource documents is quite large. And I have a way to share the same ' resource file ' without copying.

So scheme two can be vetoed.

Programme III:

You might say, "Axlge, you're a pig." Don't you know to use Str_replace or Preg_replace functions for path conversion? ”

Yes, I know. But I don't think it's worth doing to read a file without saying, "pattern matching." To put it simply, I'm against using any of the ' replace ' functions in PHP to apply to the template engine.

So scheme three can be vetoed.

Finally, you finally unbearable, the angry: "I, said half a day I say is to ask:" Why do you ask to be in PHP and HTML can preview to the picture? ”

That's because the template files in the known template engine are pretty ugly when they're open in the browser. Either filthy, full of pock-like, or tattered, or poorly dressed (for example, pictures cannot be displayed).

In fact, this problem has baffled countless heroes. I have written an article before to solve this problem. The title of the article is called "add $ symbol to the folder". To apply the idea of this article, we can solve this problem, which is to give the ' Assets ' folder front
Plus a "$" symbol.

In other words, use "$assets" instead of the original "assets" as the name of the folder.

Now, in order to understand the role of this, let's temporarily focus on the art of attention and sight. Suppose that Amy has designed a index.htm page that contains a logo picture. So the path to this logo image is "$assets/images/logo.gif"
The corresponding HTML is: . It is obvious that the picture can be displayed normally in index.htm.

The next line of sight to the PHP programmer. Let's say the index.htm file is called in index.php, and before include, define a PHP variable with the name ' $assets '. The code is basically as follows:

$assets = ' view/default/$assets ';//Use single quotes here to prevent the latter $assets from being used as a variable by PHP.

Include ' view/default/index.htm ';

When the above code is executed, the HTML generated by index.php becomes this:


Because the equivalent PHP code is as follows:

$assets = ' view/default/$assets ';

echo<<< EOT


The third $assets above has the same name as the first $assets, so it is replaced directly.

The premise is of course the use of ' Heredoc ' in index.htm.

In this way, the picture display problem has been cleverly solved. Whether it's in the index.php or in the index.htm, it's going to be fine.
Displays a picture.

Similarly, other javascript,js,css,swf files can be run or displayed correctly. All it takes is "one $ sign + one Heredoc"

Another problem is the "dry clean" problem when HTML files are displayed. Because Heredoc is used in index.htm, so there is a PHP symbol "Echo<<<eot", you can hide it using the <!----> annotation symbol. So it looks clean. In the preview, HTML displays the ' original '.

The final question is: "The preview, the requirements of the link between the page can be seen, like a programmer do not understand the Web site, are all static pages, but the relationship between these static pages is clear"

For example: There is a super connection in index.htm that connects to register.htm. Its HTML code is as follows:
<a href= "register.htm" > Registration </a>. This will be able to meet the above requirements.

But what about the PHP side? Suppose there is a PHP file that handles registration, called "register.php". So how index.php Super connect to register.php?

This is actually very simple, you can output a section of JavaScript in PHP, let the browser automatically modify the super connection can be.

That's all. If the text narrative does not make you understand, you can refer to the following additional demo block, then everything will look very simple.

The third time to be simple or complicated, that's not a problem.

It is said that embedding the PHP code in the template file is ' too complicated ' for the artist. Even though the resource templates I described above are thoughtful for the artists, some people will feel uncomfortable.

Although I am merciful, but ' reality ' is cruel, the meaning of this sentence is that: do not use the loop statement, you will not be able to bulk output. You will certainly agree with that. But you will still argue: "Why not use a variable in the HTML file for the bulk output, and then assign a value to the variable in PHP?" For example, a table fill, in the php file to generate a table.

You can certainly do that, but you are violating the "preview-ready" principle. The field name of a table is best shown directly in HTML to make it more intuitive to reflect the output goals and intentions of the Web page.

So please don't mind inserting the PHP code into the HTML file with a little control flow.

However, based on MVC requirements, there should be no code for data analysis and processing in HTML files (for example, connecting to a database), in other words, HTML template files are only responsible for "display logic", not for anything else. Of course, in a simple application, you can use ' Simplicity View ', where the PHP code is the least, often only a ' heredoc '.

As the ' little clerk ' said: "PHP's best template language is PHP itself." And the ' PHP resource template ' can be said to be a good implementation of this idea. Not only that, but also clever to achieve the "template engine four principles", can be said to be ' unprecedented, after no one. Nian of heaven and earth, but weep under the tears '-don't misunderstand, that is ' Happy tear '.

So whether it's simple or complex, PHP resource templates are the best, it is the legendary ' template Terminator ', "as long as you have it, your life will change, before you use it, I would like to remind you that any other template on Earth, you need not have any more nostalgia." ”

"There was a precious template in front of me, I did not cherish, until the others are used, I regret!"
If God can give me another chance, I will say three words to this template--I use you. If you want to know how long I use, I hope so.
--10,000 years. ”

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: 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.