use smarty3 as default template engine. add pluggable template system
This commit is contained in:
parent
51c27579ba
commit
31a21ac24c
3
.gitignore
vendored
Normal file → Executable file
3
.gitignore
vendored
Normal file → Executable file
@ -29,3 +29,6 @@ report/
|
||||
|
||||
#ignore OSX .DS_Store files
|
||||
.DS_Store
|
||||
|
||||
#netbeans project folder
|
||||
nbproject
|
||||
|
84
boot.php
Normal file → Executable file
84
boot.php
Normal file → Executable file
@ -546,9 +546,14 @@ class App {
|
||||
'force_max_items' => 0,
|
||||
'thread_allow' => true,
|
||||
'stylesheet' => '',
|
||||
'template_engine' => 'internal',
|
||||
'template_engine' => 'smarty3',
|
||||
);
|
||||
|
||||
// array of registered template engines ('name'=>'class name')
|
||||
public $template_engines = array();
|
||||
// array of instanced template engines ('name'=>'instance')
|
||||
public $template_engine_instance = array();
|
||||
|
||||
private $ldelim = array(
|
||||
'internal' => '',
|
||||
'smarty3' => '{{'
|
||||
@ -680,6 +685,16 @@ class App {
|
||||
$this->is_tablet = $mobile_detect->isTablet();
|
||||
|
||||
BaseObject::set_app($this);
|
||||
|
||||
/**
|
||||
* register template engines
|
||||
*/
|
||||
$dc = get_declared_classes();
|
||||
foreach ($dc as $k) {
|
||||
if (in_array("ITemplateEngine", class_implements($k))){
|
||||
$this->register_template_engine($k);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function get_baseurl($ssl = false) {
|
||||
@ -897,28 +912,79 @@ class App {
|
||||
return $this->curl_headers;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* register template engine class
|
||||
* if $name is "", is used class static property $class::$name
|
||||
* @param string $class
|
||||
* @param string $name
|
||||
*/
|
||||
function register_template_engine($class, $name = '') {
|
||||
if ($name===""){
|
||||
$v = get_class_vars( $class );
|
||||
if(x($v,"name")) $name = $v['name'];
|
||||
}
|
||||
if ($name===""){
|
||||
echo "template engine <tt>$class</tt> cannot be registered without a name.\n";
|
||||
killme();
|
||||
}
|
||||
$this->template_engines[$name] = $class;
|
||||
}
|
||||
|
||||
/**
|
||||
* return template engine instance. If $name is not defined,
|
||||
* return engine defined by theme, or default
|
||||
*
|
||||
* @param strin $name Template engine name
|
||||
* @return object Template Engine instance
|
||||
*/
|
||||
function template_engine($name = ''){
|
||||
if ($name!=="") {
|
||||
$template_engine = $name;
|
||||
} else {
|
||||
$template_engine = 'smarty3';
|
||||
if (x($this->theme, 'template_engine')) {
|
||||
$template_engine = $this->theme['template_engine'];
|
||||
}
|
||||
}
|
||||
|
||||
if (isset($this->template_engines[$template_engine])){
|
||||
if(isset($this->template_engine_instance[$template_engine])){
|
||||
return $this->template_engine_instance[$template_engine];
|
||||
} else {
|
||||
$class = $this->template_engines[$template_engine];
|
||||
$obj = new $class;
|
||||
$this->template_engine_instance[$template_engine] = $obj;
|
||||
return $obj;
|
||||
}
|
||||
}
|
||||
|
||||
echo "template engine <tt>$template_engine</tt> is not registered!\n"; killme();
|
||||
}
|
||||
|
||||
function get_template_engine() {
|
||||
return $this->theme['template_engine'];
|
||||
}
|
||||
|
||||
function set_template_engine($engine = 'internal') {
|
||||
function set_template_engine($engine = 'smarty3') {
|
||||
|
||||
$this->theme['template_engine'] = 'internal';
|
||||
$this->theme['template_engine'] = $engine;
|
||||
|
||||
switch($engine) {
|
||||
/*if ($engine) {
|
||||
case 'smarty3':
|
||||
if(is_writable('view/tpl/smarty3/'))
|
||||
$this->theme['template_engine'] = 'smarty3';
|
||||
if(!is_writable('view/tpl/smarty3/'))
|
||||
echo "<b>ERROR</b> folder <tt>view/tpl/smarty3/</tt> must be writable by webserver."; killme();
|
||||
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}*/
|
||||
}
|
||||
function get_template_ldelim($engine = 'internal') {
|
||||
function get_template_ldelim($engine = 'smarty3') {
|
||||
return $this->ldelim[$engine];
|
||||
}
|
||||
|
||||
function get_template_rdelim($engine = 'internal') {
|
||||
function get_template_rdelim($engine = 'smarty3') {
|
||||
return $this->rdelim[$engine];
|
||||
}
|
||||
|
||||
|
11
include/ITemplateEngine.php
Executable file
11
include/ITemplateEngine.php
Executable file
@ -0,0 +1,11 @@
|
||||
<?php
|
||||
require_once 'boot.php';
|
||||
|
||||
|
||||
/**
|
||||
* Interface for template engines
|
||||
*/
|
||||
interface ITemplateEngine {
|
||||
public function replace_macros($s,$v);
|
||||
public function get_markup_template($file, $root='');
|
||||
}
|
39
include/friendica_smarty.php
Normal file → Executable file
39
include/friendica_smarty.php
Normal file → Executable file
@ -1,7 +1,8 @@
|
||||
<?php /** @file */
|
||||
|
||||
require_once 'include/ITemplateEngine.php';
|
||||
require_once("library/Smarty/libs/Smarty.class.php");
|
||||
|
||||
|
||||
class FriendicaSmarty extends Smarty {
|
||||
|
||||
public $filename;
|
||||
@ -41,3 +42,39 @@ class FriendicaSmarty extends Smarty {
|
||||
|
||||
|
||||
|
||||
class FriendicaSmartyEngine implements ITemplateEngine {
|
||||
static $name ="smarty3";
|
||||
|
||||
public function __construct(){
|
||||
if(!is_writable('view/tpl/smarty3/')){
|
||||
echo "<b>ERROR:</b> folder <tt>view/tpl/smarty3/</tt> must be writable by webserver."; killme();
|
||||
}
|
||||
}
|
||||
|
||||
// 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_markup_template($file, $root=''){
|
||||
$template_file = theme_include('smarty3/'.$file, $root);
|
||||
if($template_file) {
|
||||
$template = new FriendicaSmarty();
|
||||
$template->filename = $template_file;
|
||||
|
||||
return $template;
|
||||
}
|
||||
return "";
|
||||
}
|
||||
}
|
21
include/plugin.php
Normal file → Executable file
21
include/plugin.php
Normal file → Executable file
@ -569,24 +569,9 @@ function get_intltext_template($s) {
|
||||
|
||||
|
||||
function get_markup_template($s, $root = '') {
|
||||
|
||||
$a = get_app();
|
||||
|
||||
$template_eng = $a->get_template_engine();
|
||||
if($template_eng === 'internal') {
|
||||
$template_file = theme_include($s, $root);
|
||||
if($template_file)
|
||||
return file_get_contents($template_file);
|
||||
}
|
||||
else {
|
||||
$template_file = theme_include("$template_eng/$s", $root);
|
||||
|
||||
if($template_file) {
|
||||
$template = new FriendicaSmarty();
|
||||
$template->filename = $template_file;
|
||||
|
||||
return $template;
|
||||
}
|
||||
}
|
||||
$t = $a->template_engine();
|
||||
$template = $t->get_markup_template($s, $root);
|
||||
return $template;
|
||||
}
|
||||
|
||||
|
25
include/template_processor.php
Normal file → Executable file
25
include/template_processor.php
Normal file → Executable file
@ -1,7 +1,11 @@
|
||||
<?php /** @file */
|
||||
<?php
|
||||
require_once 'include/ITemplateEngine.php';
|
||||
|
||||
define ("KEY_NOT_EXISTS", '^R_key_not_Exists^');
|
||||
|
||||
class Template {
|
||||
class Template implements ITemplateEngine {
|
||||
static $name ="internal";
|
||||
|
||||
var $r;
|
||||
var $search;
|
||||
var $replace;
|
||||
@ -245,8 +249,8 @@
|
||||
return $s;
|
||||
}
|
||||
|
||||
public function replace($s, $r) {
|
||||
$t1 = dba_timer();
|
||||
// TemplateEngine interface
|
||||
public function replace_macros($s, $r) {
|
||||
$this->r = $r;
|
||||
|
||||
$s = $this->_build_nodes($s);
|
||||
@ -265,14 +269,19 @@
|
||||
$os=$s; $count++;
|
||||
$s = $this->var_replace($s);
|
||||
}
|
||||
$t3 = dba_timer();
|
||||
// logger('macro timer: ' . sprintf('%01.4f %01.4f',$t3 - $t2, $t2 - $t1));
|
||||
|
||||
return $s;
|
||||
}
|
||||
|
||||
public function get_markup_template($file, $root='') {
|
||||
$template_file = theme_include($file, $root);
|
||||
$template_file = "";
|
||||
if ($template_file) {
|
||||
$content = file_get_contents($template_file);
|
||||
}
|
||||
return $content;
|
||||
}
|
||||
}
|
||||
|
||||
$t = new Template;
|
||||
|
||||
|
||||
|
||||
|
63
include/text.php
Normal file → Executable file
63
include/text.php
Normal file → Executable file
@ -1,44 +1,23 @@
|
||||
<?php /** @file */
|
||||
|
||||
// This is our template processor.
|
||||
// $s is the string requiring macro substitution.
|
||||
// $r is an array of key value pairs (search => replace)
|
||||
// returns substituted string.
|
||||
|
||||
|
||||
require_once("include/template_processor.php");
|
||||
require_once("include/friendica_smarty.php");
|
||||
|
||||
|
||||
/**
|
||||
* This is our template processor
|
||||
*
|
||||
* @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;
|
||||
|
||||
// $ts = microtime();
|
||||
$a = get_app();
|
||||
|
||||
if($a->get_template_engine() === 'smarty3') {
|
||||
$output = '';
|
||||
if(gettype($s) !== 'NULL') {
|
||||
$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);
|
||||
}
|
||||
// $tt = microtime() - $ts;
|
||||
// $a->page['debug'] .= "$tt <br>\n";
|
||||
return $output;
|
||||
}
|
||||
|
||||
@ -71,6 +50,8 @@ function random_string($size = 64,$type = RANDOM_STRING_HEX) {
|
||||
* They will be replaced with safer brackets. This may be filtered further
|
||||
* if these are not allowed either.
|
||||
*
|
||||
* @param string $string Input string
|
||||
* @return string Filtered string
|
||||
*/
|
||||
|
||||
|
||||
@ -86,6 +67,13 @@ function notags($string) {
|
||||
// and allow them to be safely displayed.
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* use this on "body" or "content" input where angle chars shouldn't be removed,
|
||||
* and allow them to be safely displayed.
|
||||
* @param string $string
|
||||
* @return string
|
||||
*/
|
||||
function escape_tags($string) {
|
||||
|
||||
return(htmlspecialchars($string, ENT_COMPAT, 'UTF-8', false));
|
||||
@ -97,6 +85,12 @@ function escape_tags($string) {
|
||||
// used to generate initial passwords
|
||||
|
||||
|
||||
/**
|
||||
* generate a string that's random, but usually pronounceable.
|
||||
* used to generate initial passwords
|
||||
* @param int $len
|
||||
* @return string
|
||||
*/
|
||||
function autoname($len) {
|
||||
|
||||
if($len <= 0)
|
||||
@ -172,6 +166,11 @@ function autoname($len) {
|
||||
// returns escaped text.
|
||||
|
||||
|
||||
/**
|
||||
* escape text ($str) for XML transport
|
||||
* @param string $str
|
||||
* @return string Escaped text.
|
||||
*/
|
||||
function xmlify($str) {
|
||||
$buffer = '';
|
||||
|
||||
|
Reference in New Issue
Block a user