TMDPHP template engine tutorial

Source: Internet
Author: User
Tags php foreach php template dreamweaver

When talking about the template engine in the PHP field, you must take the Smarty to open the knife,
This silly template engine with a little bit of official colors,
Without a young man like me with a sense of justice and innovative spirit,
I don't know how much it will continue to poison teenagers who are in the flower season and are full of beautiful fantasies about PHP.
1. Syntax
You really think that the {foreach key = key item = item from = $ contact} syntax is beautiful.
But cannot learn <? Php foreach ($ contact as $ key = >$ item) {?> ?
{If $ name eq "Fred" or $ name eq "Wilma "}
What's worse than <? Php if ($ name = 'fred 'or $ name = 'wilm') {?> Where can I find excellence?
First of all, I have always been skeptical about the Smarty syntax I will learn. At least I have never met a Smarty syntax for so many years of work,
Even if the artist is willing to learn, why don't you teach him authentic PHP syntax? Instead, you need to teach him a "Smarty language" that you don't know"
2. Visualization
When the page is handed over from the artist to your hand, then you give the perfect web pages and put the disgusting Smarty code on them,
Then, in Dreamweaver, do you seriously read how ugly those pages have become,
Is the image still visible? Is CSS still there? Not to mention include. But what about modification? Can you still recognize it at a glance?
These cannot be solved. How can these so-called template engines be "powerful?
3 ....
If there are too many templates, I will not mention them. Here I just use Smarty as an example. It is not difficult to find that other template engines are similar,
All of them are busy inventing their own template language, while the problems that really need to be solved are avoided,
Do you understand that the so-called template engine is powerful and all are TMD scammers,
In the dark night, I woke up countless times and felt that my burden was heavier, just because I could not tell you this cruel fact.
So I was so worried that I took the time to write the real template engine named tmd_tpl,
Although it may not be powerful yet, it is a necessity for the future.
· Tmd _ tpl getting started Tutorial:
Next we will learn how to use tmd_tpl. The process is no different from other template engines.
I. initialize the template engine Copy codeThe Code is as follows: <? Php
// Include tmd_tpl
Require '../tmdphp/tmd_tpl.php'; // change it to the path of your tmd_tpl.
// Instantiate tmd_tpl
$ TPL = new tmd_tpl ();
// Configure tmd_tpl
// Specify the template directory, ending with a slash
$ TPL-> tpl_dir = './tpl /';
// Specify the template file extension.
// We recommend that you use php as the suffix, because php code is highlighted in Dreamweaver.
// In addition, you can directly open it in the Chrome browser for preview. If you open it with IE, a prompt is displayed for download.
$ TPL-> tpl_ext = '. tpl. php ';
// Specify the directory for saving the compiled Template
$ TPL-> cache_dir = './tpl_c /';
// Set the validity period of the compiled file, in seconds
$ TPL-> cache_time = 0; // 0 is re-compiled every time, and-1 never expires,
// Custom Regular Expression replacement
$ TPL-> my_rep = array (
'~ (\. \./) + Static /~ '=>'/Proj-1/static /',
// Configure if the project's access address is http: // localhost/proj-1/
// There are a lot of tips for custom replacement, which will not be written during the getting-started period.
);

2. assign values and display the page Copy codeThe Code is as follows: // normal assignment
$ TPL-> assign ('site _ name', 'wangdao zhongqiang stream ');
$ TPL-> assign ('site _ intro', 'I am a PHP programmer and the author of tmd_tpl. ');
// Supports Arrays
$ Blog = array (
'Title' => 'smarty to TMD ',
'Content' => 'Before explaining how to use tmd_tpl, I want to explain why we need to re-invent the wheel.
Let's start with what contributions the so-called PHP template engine has made to everyone in this world.
When talking about the template engine in the PHP field, you must take the Smarty to open the knife,
This silly template engine with a little bit of official colors,
Without a young man like me with a sense of justice and innovative spirit,
I don't know how much it will continue to poison teenagers who are in the flower season and are full of beautiful fantasies about PHP. ',
// Currently, only two-dimensional arrays are supported. Generally, two-dimensional arrays are sufficient.
'Info' => array (
'Addtime' => '2017. 123 ',
'Author' => 'wang zhongqiang ',
'Weibo '=> 'HTTP: // t.qq.com/teeband ',
),
);
$ TPL-> assign ('blog ', $ blog );
// This array is output cyclically in the template.
$ Links = array (
'Feet home' => 'HTTP: // www.jb51.net ',
'Material world' => 'HTTP: // SC .jb51.net /',
'Baidu '=> 'HTTP: // www.baidu.com /',
'Url navigation '=> 'HTTP: // www.hao123.com ',
'Injury incom' => 'HTTP: // www.3buqi.com /',
'Hey! '=> 'HTTP: // www.hei123.net /',
);
$ TPL-> assign ('links ', $ links );
$ TPL-> display ('index ');

Iii. directory structure of static template files


Iv. templates

Copy codeThe Code is as follows: // call the variable in the template
<Div id = "logo">
<H1> <a href = "#" >{$ site_name} </a> <P >{$ site_intro} </p>
</Div>
// Call the Array
<H2 class = "title"> <a href = "#" >{$ blog. title} </a> // Two-dimensional array
<A href = "{$ blog.info. weibo}" target = "_ blank" >{$ blog.info. author} </a>
// The basic functions of these template engines are the same in tmd_tpl.
// But for loop and judgment, tmd_tpl uses php Code directly. There is no need to create a template language for implementation.
<? Php
Foreach ($ links as $ name => $ url ){
?>
<Li> <a href = "{$ url}" target = "_ blank" >{$ name} </a> </li>
<? Php
}
?>
// For functions, tmd_tpl is a weakness.
// The format {$ blog. content | nl2br} is not supported yet.
{: Nl2br ($ blog ['content'])} // only
<? = Nl2br ($ blog ['content'])?> // Or
// Both are automatically converted
<? Php echo nl2br ($ blog ['content'])?>
// If you call a function that does not return a value
{~ Print_r ($ blog )}

The true innovation of tmd_tpl lies in path conversion.
You can insert images directly in Dreamweaver, introduce CSS, call JS, and include another page.Copy codeThe Code is as follows: // insert an image

// Introduce CSS
<Link href = "../static/style.css" rel = "stylesheet" type = "text/css" media = "screen"/>
// Call JS
<Script type = "text/javascript" src = "../static/test. js"> </script>
// Contains a page
<? Php include ('inc/footer. tpl. php');?>

· After using the tmd_tpl template engine

View http access results> Click here <

In Dreamweaver (in this way, front-end personnel can find a solution ~)

In Chrome (only include pages cannot be displayed)

· If you don't need tmd_tpl, let's take a look at Discuz! And DedeCMS templates.

Discuz!

DedeCms

Now, do you know how ridiculous these powerful template engines are?

What are you waiting? It's time to change history. Click your mouse to download the second generation PHP template engine tmd_tpl!

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.