template engine rework

- use smarty3 as default engine
- new pluggable template engine system
This commit is contained in:
Fabrixxm 2013-03-27 10:37:59 -04:00
commit ddf1caf0fd
5 changed files with 136 additions and 34 deletions

View file

@ -1,9 +1,9 @@
<?php
require_once "object/TemplateEngine.php";
require_once("library/Smarty/libs/Smarty.class.php");
class FriendicaSmarty extends Smarty {
public $filename;
function __construct() {
@ -37,7 +37,33 @@ class FriendicaSmarty extends Smarty {
}
return $this->fetch('file:' . $this->filename);
}
}
class FriendicaSmartyEngine implements ITemplateEngine {
static $name ="smarty3";
// ITemplateEngine interface
public function replace_macros($s, $r) {
$template = '';
if(gettype($s) === 'string') {
$template = $s;
$s = new FriendicaSmarty();
}
foreach($r as $key=>$value) {
if($key[0] === '$') {
$key = substr($key, 1);
}
$s->assign($key, $value);
}
return $s->parsed($template);
}
public function get_template_file($file, $root=''){
$a = get_app();
$template_file = get_template_file($a, 'smarty3/' . $file, $root);
$template = new FriendicaSmarty();
$template->filename = $template_file;
return $template;
}
}

View file

@ -1,9 +1,11 @@
<?php
require_once 'object/TemplateEngine.php';
define("KEY_NOT_EXISTS", '^R_key_not_Exists^');
class Template {
class Template implements ITemplateEngine {
static $name ="internal";
var $r;
var $search;
var $replace;
@ -256,7 +258,8 @@ class Template {
return $s;
}
public function replace($s, $r) {
// TemplateEngine interface
public function replace_macros($s, $r) {
$this->r = $r;
// remove comments block
@ -276,12 +279,18 @@ class Template {
$count++;
$s = $this->var_replace($s);
}
return $s;
return template_unescape($s);
}
public function get_template_file($file, $root='') {
$a = get_app();
$template_file = get_template_file($a, $file, $root);
$content = file_get_contents($template_file);
return $content;
}
}
$t = new Template;
function template_escape($s) {

View file

@ -15,39 +15,20 @@ if(! function_exists('replace_macros')) {
/**
* This is our template processor
*
* @global Template $t
* @param string|FriendicaSmarty $s the string requiring macro substitution,
* or an instance of FriendicaSmarty
* @param array $r key value pairs (search => replace)
* @return string substituted string
*/
function replace_macros($s,$r) {
global $t;
$stamp1 = microtime(true);
$a = get_app();
if($a->theme['template_engine'] === 'smarty3') {
$template = '';
if(gettype($s) === 'string') {
$template = $s;
$s = new FriendicaSmarty();
}
foreach($r as $key=>$value) {
if($key[0] === '$') {
$key = substr($key, 1);
}
$s->assign($key, $value);
}
$output = $s->parsed($template);
}
else {
$r = $t->replace($s,$r);
$t = $a->template_engine();
$output = $t->replace_macros($s,$r);
$output = template_unescape($r);
}
$a = get_app();
$a->save_timestamp($stamp1, "rendering");
return $output;
@ -582,6 +563,14 @@ function get_markup_template($s, $root = '') {
$stamp1 = microtime(true);
$a = get_app();
$t = $a->template_engine();
$template = $t->get_template_file($s, $root);
$a->save_timestamp($stamp1, "file");
return $template;
/*
if($a->theme['template_engine'] === 'smarty3') {
$template_file = get_template_file($a, 'smarty3/' . $s, $root);
@ -602,6 +591,7 @@ function get_markup_template($s, $root = '') {
return $content;
}
*/
}}
if(! function_exists("get_template_file")) {