From 31a21ac24cd5cbe19e40ab3838fcc179d812da13 Mon Sep 17 00:00:00 2001 From: fabrixxm Date: Wed, 8 May 2013 03:51:38 -0400 Subject: use smarty3 as default template engine. add pluggable template system --- include/ITemplateEngine.php | 11 ++++++++ include/friendica_smarty.php | 39 +++++++++++++++++++++++++- include/plugin.php | 21 ++------------ include/template_processor.php | 25 +++++++++++------ include/text.php | 63 +++++++++++++++++++++--------------------- 5 files changed, 100 insertions(+), 59 deletions(-) create mode 100755 include/ITemplateEngine.php mode change 100644 => 100755 include/friendica_smarty.php mode change 100644 => 100755 include/plugin.php mode change 100644 => 100755 include/template_processor.php mode change 100644 => 100755 include/text.php (limited to 'include') diff --git a/include/ITemplateEngine.php b/include/ITemplateEngine.php new file mode 100755 index 000000000..53c1845f4 --- /dev/null +++ b/include/ITemplateEngine.php @@ -0,0 +1,11 @@ +ERROR: folder view/tpl/smarty3/ 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 ""; + } +} \ No newline at end of file diff --git a/include/plugin.php b/include/plugin.php old mode 100644 new mode 100755 index 31427e117..3ef97fba9 --- a/include/plugin.php +++ b/include/plugin.php @@ -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; } diff --git a/include/template_processor.php b/include/template_processor.php old mode 100644 new mode 100755 index 0b4b4142f..794155f84 --- a/include/template_processor.php +++ b/include/template_processor.php @@ -1,7 +1,11 @@ -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; diff --git a/include/text.php b/include/text.php old mode 100644 new mode 100755 index c7210010c..3b3620f33 --- a/include/text.php +++ b/include/text.php @@ -1,44 +1,23 @@ 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
\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 = ''; -- cgit v1.2.3 From c753fa19d289e936131aad706cf31bcfe111b4c1 Mon Sep 17 00:00:00 2001 From: fabrixxm Date: Wed, 8 May 2013 04:00:05 -0400 Subject: fix internal --- include/template_processor.php | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'include') diff --git a/include/template_processor.php b/include/template_processor.php index 794155f84..74acc9c67 100755 --- a/include/template_processor.php +++ b/include/template_processor.php @@ -248,7 +248,11 @@ return $s; } - + + private function replace($s,$r) { + $this->replace_macros($s, $r); + } + // TemplateEngine interface public function replace_macros($s, $r) { $this->r = $r; @@ -274,7 +278,6 @@ public function get_markup_template($file, $root='') { $template_file = theme_include($file, $root); - $template_file = ""; if ($template_file) { $content = file_get_contents($template_file); } -- cgit v1.2.3 From 5e1980becf69a4c65489bed6f94fa730ec8ef4bf Mon Sep 17 00:00:00 2001 From: fabrixxm Date: Wed, 8 May 2013 04:23:17 -0400 Subject: remove "internal" templates, use "tpl/" folder for smarty3 templates, move smarty3 templates in "tpl/" folder, add util/precompile_smarty.php utility, add precompiled templates --- include/friendica_smarty.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'include') diff --git a/include/friendica_smarty.php b/include/friendica_smarty.php index 5f247a528..a4675acd4 100755 --- a/include/friendica_smarty.php +++ b/include/friendica_smarty.php @@ -15,10 +15,10 @@ class FriendicaSmarty extends Smarty { // setTemplateDir can be set to an array, which Smarty will parse in order. // The order is thus very important here - $template_dirs = array('theme' => "view/theme/$theme/tpl/smarty3/"); + $template_dirs = array('theme' => "view/theme/$theme/tpl/"); if( x($a->theme_info,"extends") ) - $template_dirs = $template_dirs + array('extends' => "view/theme/".$a->theme_info["extends"]."/tpl/smarty3/"); - $template_dirs = $template_dirs + array('base' => 'view/tpl/smarty3/'); + $template_dirs = $template_dirs + array('extends' => "view/theme/".$a->theme_info["extends"]."/tpl/"); + $template_dirs = $template_dirs + array('base' => 'view/tpl/'); $this->setTemplateDir($template_dirs); $this->setCompileDir('view/tpl/smarty3/compiled/'); @@ -68,7 +68,7 @@ class FriendicaSmartyEngine implements ITemplateEngine { } public function get_markup_template($file, $root=''){ - $template_file = theme_include('smarty3/'.$file, $root); + $template_file = theme_include($file, $root); if($template_file) { $template = new FriendicaSmarty(); $template->filename = $template_file; -- cgit v1.2.3