Free lightweight template system

Are there any free, lightweight, non-MVC template systems created using PHP? I'm not interested in Smarty.

+3
source share
6 answers

Of course:

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

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

<?php require("Footer.php"); ?>
+5
source

It was the easiest I could find.

include("header.php");
+2
source

PHP Savant, PHP-: http://phpsavant.com/

{template.syntax}, TinyButStrong: http://tinybutstrong.com/

+1

Twig Fabien Potencier.

+1

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

, . , .

- SO - MVC, , , CodeIgniter. tut MVC PHP ( , , , . )

.

0

, , .

/**
 * 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 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

Source: https://habr.com/ru/post/1728494/


All Articles