Generate Smarttemplate Template Handler framework

Source: Internet
Author: User
Tags foreach define array chr functions ord variables vars
Program | template <?php
/**
* Generate Smarttemplate Template handler framework
*
* The Smarttemplate template class is extremely efficient and has a small code size.
* His extensible template function is unique.
*
* Although there are only a few methods, it is easy to use.
* But after adjusting the template file more, it will take some time to match the handler with the template.

* This procedure is used to analyze the template file and give the framework of the template processing program
**/

Ob_start ();

$Template _name = "include/main.html";
$str = file_get_contents ($Template _name);
Preg_match_all ("/<!--. *-->|{ \w.*}/u ", $str, $regs);

$stack = Array ();
$vars = Array ();
$functions = Array ();
$key = 0; = Chr (ord ("I") +count ($stack));
$LF = "\ r \ n";
$outstr = "";
foreach ($regs [0] as $v) {
if (Preg_match_all ("/<!--BEGIN (\w+)-->/", $v, $r)) {
Array_push ($stack, $r [1][0]);
$k = Chr (ord ("I") + $key + +);
$outstr. = Str_repeat (Chr (9), $key-1). " /* Logarithmic group \${$r [1][0]} assigned value/$LF ";
$outstr. = Str_repeat (Chr (9), $key-1). " For (\$ $k =0;\$ $k <1;\$ $k + +) \{$LF ";
}elseif (Preg_match_all ("/<!--End (\w+)-->/", $v, $r)) {
$v = Array_pop ($stack);
$outstr. = Str_repeat (Chr (9),--$key). "} $LF ";
if ($key) {
$k = Chr (ord ("I") + $key-1);
$p = $stack [$key-1];
$outstr. = Str_repeat (Chr (9), $key). " \${$p}[\$ $k] [$v] = \$ $v; $LF ";
}else
$outstr. = "\ $data [$v] = \$ $v; $LF";
}elseif (Preg_match_all ("/{(\w+)}/", $v, $r)) {
if (count ($stack)) {
$v = $stack [$key-1];
$k = Chr (ord ("I") + $key-1);
$outstr. = Str_repeat (Chr (9), COUNT ($stack). " \${$v}[\$ $k][{$r [1][0]}] = ' {$r [1][0]} test '; $LF ';
$ch = "\ $data [". Join ("][][", $stack). "] [{$r [1][0]}];
if (! In_array ($ch, $vars))
$vars [] = $ch;
}else
if (! In_array ("\ $data [{$r [1][0]}]", $vars)) {
$vars [] = "\ $data [{$r [1][0]}]";
$outstr. = "\ $data [{$r [1][0]}] = ' {$r [1][0]} test '; $LF ';
}
}elseif (Preg_match_all ("/{(\w+):([^}]+)/", $v, $r)) {
$ch = $r [1][0]. ({$r [2][0]}) ";
$define = true;
if (! @include ("smarttemplate_extensions/smarttemplate_extension_serverr.php"))
$define = false;
$functions [] = Array (name=> $ch, define=> $define);
foreach (Split (",", $r [2][0]) as $v)
if ($v [0]!= ' ")
if (! In_array ($v, $vars)) {
$vars [] = "\ $data [$v]";
$outstr. = "\ $data [$v] = ' $v test '; $LF";
}
}elseif (Preg_match_all ("/<!--IF (\w+)/", $v, $r)) {
if (! In_array ("\ $data [{$r [1][0]}]", $vars)) {
$vars [] = "\ $data [{$r [1][0]}]";
$outstr. = "\ $data [{$r [1][0]}] = ' {$r [1][0]} test '; $LF ';
}
}
}

Echo <<< TEXT
<?php
/**
* Template $Template _name
* Use the following variables, assign values to them

TEXT;

foreach ($vars as $v)
echo "* $v $LF";

echo "* $LF";
echo "* uses the following template function $lf";
$ar = Array ();
foreach ($functions as $k => $v) {
echo "* $v [name] $LF";
if (! $v [define]) $ar [] = $k;
}
if (count ($ar)) {
$path = "Smarttemplate_extensions";
if (! Is_dir ($path)) {
mkdir ($path, 0666);
chmod ($path, 0666);
}
echo "* $LF";
echo "* Where the following function is undefined, define $LF";
foreach ($ar as $k) {
$v = $functions [$k][name];
$name = Preg_replace ("/(\w+). *", "\\1", $v);
echo "* $v => $path/{$path}_$name.php$lf";

$fp = fopen ("$path/{$path}_$name.php", "w");
Fwrite ($fp, "&LT;?PHP$LF");
Fwrite ($fp, "function {$path}_$v \{$LF");
Fwrite ($FP, "return" function $v '; $LF ");
Fwrite ($fp, "} $LF?> $LF");
Fclose ($FP);
}
}
Echo <<< TEXT
**/

/** possible forms of assigning value to template variables **/

TEXT;
Echo $outstr;
Echo <<< TEXT

/** load and execute template **/
Require_once "class.smarttemplate.php";
\ $page = new Smarttemplate ("$Template _name");
\ $page->assign (\ $data);
\ $page->output ();
?>

TEXT;

$code = Ob_get_contents ();
Ob_end_clean ();

$fname = basename ($Template _name);
$fname = substr ($fname, 0,strrpos ($fname, ".") +1). " PHP ";

$fp = fopen ($fname, "w");
Fwrite ($fp, $code);
Fclose ($FP);

Include "$fname";

?>



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.