开发者

free lightweight template system

开发者 https://www.devze.com 2022-12-16 04:52 出处:网络
Is there any free, lightweight, non-MVC template s开发者_运维技巧ystems made pure with PHP? I\'m not interested in Smarty.Sure:

Is there any free, lightweight, non-MVC template s开发者_运维技巧ystems made pure with PHP? I'm not interested in Smarty.


Sure:

<?php require("Header.php"); ?>

  <h1>Hello World</h1>
  <p>I build sites without "smarty crap"!</p>

<?php require("Footer.php"); ?>


It was the most lightweight one I could find.

include("header.php");


PHP Savant, basically inline PHP code : http://phpsavant.com/

or if you really want to use the {template.syntax} you might look at TinyButStrong : http://tinybutstrong.com/


Try having a look at Twig by Fabien Potencier.


http://www.phpaddiction.com/tags/axial/url-routing-with-php-part-one/

Far and away the best tutorial I've found. I utilized this lesson to switch my small run projects over to OOP and abandon Procedural.

A big caveat here and something SO made me realize - if you need a serious MVC, it's always better to go with tested, stable ones like CodeIgniter. I basically used this tut to build a skeleton of MVC to hang my pure PHP off of (I didn't want to relearn all the framework commands, and I have many classes I've made that I wanted to include and continue to use.)

This tut helped miles.


Here's a tiny class I came up with to do some quick templating for e-mails.

/**
 * 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 '' ;
        }
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消