User tips: Generate smarttemplate Template handler framework

Source: Internet
Author: User
Tags foreach array chr functions include ord variables vars

<?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";
}
}

Else

if (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";
}
}

Else
if (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
* Using the following variables, assign values to

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, "<?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.