Home  >  Article  >  Backend Development  >  TMDPHP template engine usage tutorial_PHP tutorial

TMDPHP template engine usage tutorial_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:19:52751browse

When talking about template engines in the PHP world, it is inevitable to start with Smarty.
This is an extremely silly but slightly official template engine.
If there were not people like me who have a sense of justice and innovative spirit, Passionate young people have come forward,
I don’t know how many young people it will continue to poison in their prime and who are full of beautiful fantasies about PHP.
1. Grammar
Do you really think that artists can learn {foreach key=key item=item from=$contact} such syntax
But they can’t learn$item) { ?>?
And {if $name eq "Fred" or $name eq "Wilma"}
is better thanFirst of all, I am always skeptical about artists learning Smarty syntax. At least I have never met anyone who knows Smarty syntax in so many years of working.
And even if an artist is willing to learn, why don’t you teach him authentic PHP? Grammar, but you have to teach him a "Smarty language" that you can't even understand yourself
2. Visualization
When the page is handed over from the artist to you, then you add that disgusting look to those perfect web pages Smarty code,
Then in Dreamweaver, have you seriously looked at how ugly those pages have become?
Can the pictures still be visible? Is CSS still around? Not to mention include. And what about when it’s time to make changes? Can you still recognize it at a glance?
If these cannot be solved, how can those so-called template engines be worthy of the word "powerful"?
3....
I won’t go into too much detail. I’m just taking Smarty as an example. It should not be difficult to find that other template engines are similar.
They are all busy inventing their own template languages. The problems that really need to be solved are avoided.
Do you understand now that the so-called template engine and the so-called power are all liars?
In the dead of night, I woke up countless times, I feel like my burden is heavier just because I can't tell you this cruel fact.
So I was heartbroken and heartbroken, and took time out of my busy schedule to write this real template engine named tmd_tpl.
Although it may not be powerful now, being powerful is a necessity in the future.
·Introductory tutorial on using tmd_tpl:
Next, let’s learn how to use tmd_tpl. The process is not much different from other template engines.
1. Initialize the template engine

Copy the code The code is as follows:

//Include tmd_tpl
require '../tmdphp/tmd_tpl.php'; // Please change to the path where your tmd_tpl is located
// Instantiate tmd_tpl
$TPL = new tmd_tpl ();
// The following is the configuration of tmd_tpl
// Specify the template directory, ending with a slash
$TPL->tpl_dir = './tpl/';
// Specify the template file Extension
// It is recommended to use php as the suffix, because this will have a highlighting effect on the php code in Dreamweaver.
// In addition, you can open it directly in the Chrome browser for preview, and you will be prompted to download when opening it in IE.
$TPL->tpl_ext = '.tpl.php';
// Specify the directory where the compiled template is saved
$TPL->cache_dir = './tpl_c/';
/ / Set the validity period of the compiled file, unit: seconds
$TPL->cache_time = 0; // 0 means recompile every time, -1 means never expires,
// Custom regular replacement
$TPL->my_rep = array(
'~(../)+static/~' => '/proj-1/static/',
// ↑If the access address of the project It is http://localhost/proj-1/
// There are many techniques for custom replacement. I won’t write them down for the entry-level period
);

2. Assignment And display the page

Copy the code The code is as follows:

// Ordinary assignment
$TPL->assign('site_name', '王道中强流');
$TPL->assign('site_intro', 'I am a PHP programmer, author of tmd_tpl. ');
// Supports arrays
$blog = array(
'title' => 'Go to TMD's Smarty',
'content' => ; 'Before explaining how to use tmd_tpl, I want to first talk about why we need to reinvent the wheel.
Then let’s start with what contributions the so-called PHP template engines in the world have made for everyone.
When talking about template engines in the PHP world, it is inevitable to start with Smarty.
This is an extremely silly but slightly official template engine.
If there were not people like me who have a sense of justice and innovative spirit, Passionate young people have come forward,
I don’t know how many young people in their prime and full of beautiful fantasies about PHP will continue to be poisoned by it. ',
// Currently, it only supports two-dimensional arrays, generally speaking, two-dimensional arrays. It's enough
'info' => array(
'addtime' => '2012.3.11',
'author' => 'Wang Zhongqiang',
'weibo' => ; 'http://t.qq.com/teeband',
),
);
$TPL->assign('blog', $blog);
// in template Loop the demonstration to output this array
$links = array(
'Script Home' => 'http://www.jb51.net',
'Material World' => 'http: //sc.jb51.net/',
'Baidu' => 'http://www.baidu.com/',
'Website navigation' => 'http://www.hao123 .com',
'Can't afford to be hurt' => 'http://www.3buqi.com/',
'Hey! ' => 'http://www.hei123.net/' ,
);
$TPL->assign('links', $links);
$TPL->display('index');

3. Directory structure of template static files

TMDPHP template engine usage tutorial_PHP tutorial
4. Template

Copy code The code is as follows:

// Call variables in the template

// Call array

{$blog.title}


// Two-dimensional array
{$blog.info.author}
// The basic functions of these template engines are also the same in tmd_tpl.
// But for looping and judgment, the tmd_tpl view is to use PHP code directly. There is no need to create a template language to do this.
foreach ($links as $name => $url) {
?>
  • {$name}

  • }
    ?>
    // In terms of functions, for the current tmd_tpl , is a weakness.
    // The format {$blog.content|nl2br} is not supported yet.
    {:nl2br( $blog['content'] )} // This is the only way
    // Or this
    // will be automatically converted to

    // If some functions without return values ​​are called
    {~print_r( $blog )}

    The real innovation of tmd_tpl lies in the conversion of paths.
    You can insert images directly in Dreamweaver, introduce CSS, call JS, and include another page.
    Copy code The code is as follows:

    // Insert picture

    // Introduce CSS

    // Call JS

    // Include a page


    ·After using tmd_tpl template engine

    View the effect of access via http >Click here<

    In Dreamweaver (this is how the front-end staff can do it~)

    TMDPHP template engine usage tutorial_PHP tutorial

    In Chrome (only included pages cannot be displayed)

    TMDPHP template engine usage tutorial_PHP tutorial

    ·If tmd_tpl is not used, let’s take a look at the templates of Discuz! and DedeCMS.

    Discuz!

    TMDPHP template engine usage tutorial_PHP tutorial

    DedeCms

    TMDPHP template engine usage tutorial_PHP tutorial

    Now you know how ridiculous those so-called powerful template engines are, right?

    What are you waiting for? The time to change history has arrived, click the mouse in your hand and download the second-generation PHP template engine tmd_tpl!

    www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/325247.htmlTechArticleWhen talking about template engines in the PHP community, it is inevitable to use Smarty. This extremely stupid person has a A template engine with a little official color. If there weren’t people like me who have a sense of justice and are rich...
    Statement:
    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