Php 自由轻量级模板系统

Php 自由轻量级模板系统,php,Php,有没有纯用PHP制作的免费、轻量级、非MVC模板系统?我对Smarty不感兴趣。这是我能找到的最轻的一款 include("header.php"); 当然可以: 你好,世界 我建立的网站没有“聪明的垃圾” PHP专家,基本上是内联PHP代码: 或者,如果你真的想使用{template.syntax},你可以看看TinyButStrong:试试看法比恩·普洛蒂埃的作品。 这是我找到的最好的教程。我利用这一课将我的小运行项目切换到OOP,并放弃了过程化 这里有一个很大的警告,这让我意识到——如

有没有纯用PHP制作的免费、轻量级、非MVC模板系统?我对Smarty不感兴趣。

这是我能找到的最轻的一款

include("header.php");
当然可以:


你好,世界
我建立的网站没有“聪明的垃圾”


PHP专家,基本上是内联PHP代码:

或者,如果你真的想使用{template.syntax},你可以看看TinyButStrong:

试试看法比恩·普洛蒂埃的作品。

这是我找到的最好的教程。我利用这一课将我的小运行项目切换到OOP,并放弃了过程化

这里有一个很大的警告,这让我意识到——如果你需要一个严肃的MVC,最好使用经过测试的、稳定的,比如CodeIgniter。我基本上是用这个tut构建了一个MVC框架来挂起我的纯PHP(我不想重新学习所有的框架命令,我已经创建了很多类,我想包括它们并继续使用它们)


这个图坦卡蒙帮助了迈尔斯。

我想出了一个很小的类来快速制作电子邮件模板

/**
 * Parses a php template, does variable substitution, and evaluates php code returning the result
 * sample usage:
 *       == template : /views/email/welcome.php ==
 *            Hello {name}, Good to see you.
 *            <?php if ('{name}' == 'Mike') { ?>
 *                <div>I know you're mike</div>
 *            <?php } ?>
 *       == code ==
 *            require_once("path/to/Microtemplate.php") ;
 *            $data["name"] = 'Mike' ;
 *            $string = LR_Microtemplate::parse_template('email/welcome', $data) ;
 */
class Microtemplate
{

    /**
     * Micro-template: Replaces {variable} with $data['variable'] and evaluates any php code.
     * @param string $view name of view under views/ dir. Must end in .php
     * @param array $data array of data to use for replacement with keys mapping to template variables {}.
     * @return string
     */


    public static function parse_template($view, $data) {
        $template = file_get_contents($view . ".php") ;
        // substitute {x} with actual text value from array
        $content = preg_replace("/\{([^\{]{1,100}?)\}/e", 'self::get_value("${1}", $data)' , $template);

        // evaluate php code in the template such as if statements, for loops, etc...
        ob_start() ;
        eval('?>' . "$content" . '<?php ;') ;
        $c = ob_get_contents() ;
        ob_end_clean() ;
        return $c ;
    }

    /**
     * Return $data[$key] if it's set. Otherwise, empty string.
     * @param string $key
     * @param array $data
     * @return string 
     */
    public static function get_value($key, $data){
        if (isset($data[$key]) && $data[$key]!='~Unknown') { // filter out unknown from legacy system
            return $data[$key] ;   
        } else {
            return '' ;
        }
    }
}
/**
*解析php模板,进行变量替换,并计算返回结果的php代码
*示例用法:
*==模板:/views/email/welcome.php==
*你好{name},很高兴见到你。
*            
*我知道你是迈克
*            
*==代码==
*需要_一次(“path/to/Microtemplate.php”);
*$data[“name”]=“Mike”;
*$string=LR_微模板::解析模板('email/welcome',$data);
*/
类微模板
{
/**
*微模板:用$data['variable']替换{variable},并计算任何php代码。
*@param string$view/dir.下视图的视图名称必须以.php结尾
*@param array$data要使用映射到模板变量{}的键替换的数据数组。
*@返回字符串
*/
公共静态函数parse_模板($view,$data){
$template=file_get_contents($view。“.php”);
//用数组中的实际文本值替换{x}
$content=preg\u replace(“/\{([^\{]{1100}?\})/e”,“self::get\u value(${1},$data)”,$template);
//评估模板中的php代码,如if语句、for循环等。。。
ob_start();

eval(“?>”.“$content”。“哎呀,我做了一个编辑来修复你在编辑时修复的东西。我的坏人很难……我尝试了很多次,但3分钟后就退出了,让我汗流浃背……l..mvcSolar,给点时间:)当我第一次开始使用面向对象的框架时,这对我来说也很困难。请耐心:)不幸的是,耐心从来都不是我的强项之一:P但也许有一天……也许。有一个面向对象的傻瓜教程吗?Solar,耐心(和其他东西一样)学习:)随着知识和经验的增长,你将学会更耐心。达斯瓦达尼亚!:)太阳能,欢迎来到StackOverflow!这是我最初的难题。。。
/**
 * Parses a php template, does variable substitution, and evaluates php code returning the result
 * sample usage:
 *       == template : /views/email/welcome.php ==
 *            Hello {name}, Good to see you.
 *            <?php if ('{name}' == 'Mike') { ?>
 *                <div>I know you're mike</div>
 *            <?php } ?>
 *       == code ==
 *            require_once("path/to/Microtemplate.php") ;
 *            $data["name"] = 'Mike' ;
 *            $string = LR_Microtemplate::parse_template('email/welcome', $data) ;
 */
class Microtemplate
{

    /**
     * Micro-template: Replaces {variable} with $data['variable'] and evaluates any php code.
     * @param string $view name of view under views/ dir. Must end in .php
     * @param array $data array of data to use for replacement with keys mapping to template variables {}.
     * @return string
     */


    public static function parse_template($view, $data) {
        $template = file_get_contents($view . ".php") ;
        // substitute {x} with actual text value from array
        $content = preg_replace("/\{([^\{]{1,100}?)\}/e", 'self::get_value("${1}", $data)' , $template);

        // evaluate php code in the template such as if statements, for loops, etc...
        ob_start() ;
        eval('?>' . "$content" . '<?php ;') ;
        $c = ob_get_contents() ;
        ob_end_clean() ;
        return $c ;
    }

    /**
     * Return $data[$key] if it's set. Otherwise, empty string.
     * @param string $key
     * @param array $data
     * @return string 
     */
    public static function get_value($key, $data){
        if (isset($data[$key]) && $data[$key]!='~Unknown') { // filter out unknown from legacy system
            return $data[$key] ;   
        } else {
            return '' ;
        }
    }
}