TMDPHP template engine tutorial _ php template _ PHP

Source: Internet
Author: User
Tags php foreach php template
Before explaining how to use tmd_tpl, I want to explain why I want to re-invent the wheel. Let's start with the contributions made by the so-called PHP template engine in this world. Speaking of the template engine in the PHP field, it is imperative to take the Smarty tool,
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 $ Item) {?> ?
{If $ name eq "Fred" or $ name eq "Wilma "}
Youbi 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
The code is as follows:
// 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
The code is as follows:
// Normal value 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 (
''=> 'Http: // www.bitsCN.com ',
'Material World' => 'http: // SC .bitsCN.com /',
'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
The code is as follows:
// Call variables in the template


{$ Site_name}

{$ Site_intro}



// Call the array
{$ Blog. title}
// Two-dimensional array
{$ Blog.info. author}
// 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.
Foreach ($ links as $ name => $ url ){
?>
  • {$ Name}

  • }
    ?>
    // For functions, tmd_tpl is a weakness.
    // The format {$ blog. content | nl2br} is not supported yet.
    {: Nl2br ($ blog ['content'])} // only
    // Or
    // Both are automatically converted

    // 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.
    The code is as follows:
    // Insert an image

    // Introduce CSS

    // Call JS

    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.