aboutsummaryrefslogtreecommitdiffstats
path: root/include
diff options
context:
space:
mode:
authorfabrixxm <fabrix.xm@gmail.com>2013-05-08 03:51:38 -0400
committerfabrixxm <fabrix.xm@gmail.com>2013-05-08 03:51:38 -0400
commit31a21ac24cd5cbe19e40ab3838fcc179d812da13 (patch)
treed556822e261cb2b8ff10452a7c5b07b0c2c793c7 /include
parent51c27579ba79d32c26052c0d1f1218a16315234b (diff)
downloadvolse-hubzilla-31a21ac24cd5cbe19e40ab3838fcc179d812da13.tar.gz
volse-hubzilla-31a21ac24cd5cbe19e40ab3838fcc179d812da13.tar.bz2
volse-hubzilla-31a21ac24cd5cbe19e40ab3838fcc179d812da13.zip
use smarty3 as default template engine. add pluggable template system
Diffstat (limited to 'include')
-rwxr-xr-xinclude/ITemplateEngine.php11
-rwxr-xr-x[-rw-r--r--]include/friendica_smarty.php39
-rwxr-xr-x[-rw-r--r--]include/plugin.php21
-rwxr-xr-x[-rw-r--r--]include/template_processor.php25
-rwxr-xr-x[-rw-r--r--]include/text.php63
5 files changed, 100 insertions, 59 deletions
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 @@
+<?php
+require_once 'boot.php';
+
+
+/**
+ * Interface for template engines
+ */
+interface ITemplateEngine {
+ public function replace_macros($s,$v);
+ public function get_markup_template($file, $root='');
+}
diff --git a/include/friendica_smarty.php b/include/friendica_smarty.php
index b9ed2e982..5f247a528 100644..100755
--- a/include/friendica_smarty.php
+++ b/include/friendica_smarty.php
@@ -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 "";
+ }
+} \ No newline at end of file
diff --git a/include/plugin.php b/include/plugin.php
index 31427e117..3ef97fba9 100644..100755
--- 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
index 0b4b4142f..794155f84 100644..100755
--- a/include/template_processor.php
+++ b/include/template_processor.php
@@ -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;
diff --git a/include/text.php b/include/text.php
index c7210010c..3b3620f33 100644..100755
--- a/include/text.php
+++ b/include/text.php
@@ -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 = '';