Cqlctpl PHP template to start providing, best use, most powerful template

Source: Internet
Author: User
Tags array end eval fread functions net php script php template
Template This is a more useful template, than smarty,fasttemplate and phplib template are easy to use

Chinese Text Archive Http://www.cqlc.net/cn_cqlctpl.txt

Here is the demo address: http://www.cqlc.net/web/tml.php

Please respect the author's labor results, the following 3 file download address: Http://www.cqlc.net/cqlctpl.zip

My homepage is: http://www.cqlc.net

1 Tags:

(1), variable: {$key} can be general variables, arrays and members of classes, but characters cannot be used ", only single quotes '

Example: {$value}, {$a [0]}, {$this->a[' color ']} It is wrong to write {$this->["color"}

(2), loop: {loop: Looping statement} other code {/loop} supports for and while loops

Example: {loop:for ($i =10 $i <=20; $i + +)} Test {$i} {/loop} output (test) 20~30 number
{loop:while ($i ++<10)} test {$a [$i]} {/loop} output (test) 20~30 Array

(3), logic: {logi: Conditional statement} other code {/logi} support If,elseif,else

Example: {logi:if ($i ==1)} test {$i} {/logi}
{Logi:elseif ($i ==2)} test {$a [$i]} {/logi}
{Logi:else} test {$a [$i]} {/logi}

(4), program: {EXEC: statement} program section, can be a function, note that characters can only be used ', not used '
Example: {exec: $a = ' hello '. $i} correct {exec: $a = "hello$i"} Error
{Exec:echo $a}
{Exec:echo time ()}
{exec: $this->outpage (1,2, ' index.php ')}

(5), {* Template annotation *} example: {* Here will not output *}

2, Template output:

Output only: eval (cqlctpl (template file)) Example: eval (cqlctpl ("1.tml"))
Set of templates: {Exec:eval (cqlctpl (template file))} Example: {Exec:eval (CQLCTPL ($Templatefile))}
{* Here is the other template file being dropped in the template *}

3, the function of the original code:

function Cqlctpl ($file)/* functions author, CQLC, home: Http://www.cqlc.net,
Chinese file http://www.cqlc.net/cn_cqlctpl.txt * *
{
$FP =fopen ($file, "R");
$msg = "Echo\" "Str_replace (" \ "," \\\ ", Fread ($fp, FileSize ($file))." \";";
Fclose ($FP);
$oldstr =array (0=> "/\{loop: ([^\}]+) \} (. +) \{\/loop\}/si",
1=> "/\{logi: ([^\}]+) \} (. +) \{\/logi\}/si",
2=> "/\{exec: ([^\}]+) \}/si",
3=> "/(\{\*[^\*]+\*\})/si"
);
$newstr =array (0=> "\"; \\1{echo\ "\\2\";} Echo\ "",
1=> "\"; \\1{echo\ "\\2\";} Echo\ "",
2=> "\"; \\1;echo\ "",
3=> ""
);
Return Preg_replace ($oldstr, $newstr, $msg);
}
4, Template examples:

Template file: 1.tml of the original code


{loop:for ($i =0; $i <10; $i + +)} {*loop start*}

<p> {$body} is {$array [$i]} {*out the msg*}

{logi:if ($i >5)} <p> {$i} is biger than 5 {*logic start*}

{/logi} {*login end*}

{/loop} {*loop end*}

{exec: $time =time ()} {*exec start and end*}

{Exec:echo ' <p>time is '. $time} {*exec start and end*}

{Exec:eval (Cqlctpl (' 2.tml ')} {*this is include other tml*}

Template file: 2.tml of the original code
<P> {exec:test ()}

Demo PHP Script
tml.php:
?
function Cqlctpl ($file)/* functions author, CQLC, home: Http://www.cqlc.net,
Chinese text File Http://www.cqlc.net/cn_cqlctpl.txt * *
{
$FP =fopen ($file, "R");
$msg = "Echo\" "Str_replace (" \ "," \\\ ", Fread ($fp, FileSize ($file))." \";";
Fclose ($FP);
$oldstr =array (0=> "/\{loop: ([^\}]+) \} (. +) \{\/loop\}/si",
1=> "/\{logi: ([^\}]+) \} (. +) \{\/logi\}/si",
2=> "/\{exec: ([^\}]+) \}/si",
3=> "/(\{\*[^\*]+\*\})/si"
);
$newstr =array (0=> "\"; \\1{echo\ "\\2\";} Echo\ "",
1=> "\"; \\1{echo\ "\\2\";} Echo\ "",
2=> "\"; \\1;echo\ "",
3=> ""
);
Return Preg_replace ($oldstr, $newstr, $msg);
}

function test () {echo "<p> This is the second template file yo, welcome to your test";}/* Define a function/*

$title = "This is the first template file";

$body = "The output has already begun here";

For ($i =0 $i <10; $i + +) $array [$i]= "Hello World $i";

Eval (CQLCTPL ("1.tml"));


5, template output results, see Demo: http://www.cqlc.net/web/tml.php


This is the first template file (1.tml)
This has already started outputting is Hello World 0

This has already started outputting is Hello World 1

This has already started outputting is Hello World 2

This has already started outputting is Hello World 3

This has already started outputting is Hello World 4

This has already started outputting is Hello World 5

This has already started outputting is Hello World 6

6 is biger than 5

This has already started outputting is Hello World 7

7 is biger than 5

This has already started outputting is Hello World 8

8 is biger than 5

This has already started outputting is Hello World 9

9 is biger than 5

Time is 1059227729


This are other tml (2.tml)
This is the second template file yo, welcome to your test



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.