Max F 5 rokov pred
rodič
commit
6144b680d7

+ 2 - 2
example/templates/layout_base.tpl.php

@@ -12,7 +12,7 @@
     <link rel="stylesheet" type="text/css" href="/assets/vendor/alertify-1.11.1/css/alertify.css">
     <link rel="stylesheet" type="text/css" href="/assets/vendor/tui.chart-3.11.2/tui-chart.min.css">
     <link rel="stylesheet" type="text/css" href="/assets/css/app.css">
-    {$block_extra_css}
+    {yield block_css}
 
     <script type="text/javascript">var onload_actions = []; function registerOnloadAction(func) { onload_actions.push(func); }</script>
   </head>
@@ -28,7 +28,7 @@
     <script type="text/javascript" src="/assets/vendor/tui.chart-3.11.2/tui-chart.min.js"></script>
     <script type="text/javascript" src="/assets/vendor/jQuery-Autocomplete-1.4.11/jquery.autocomplete.min.js"></script>
     <script type="text/javascript" src="/assets/js/app.js"></script>
-    {$block_extra_js}
+    {yield block_js}
 
     <script type="text/javascript">executeOnloadActions();</script>
   </body>

+ 1 - 0
src/App/Middlewares/DebugBar.php

@@ -21,6 +21,7 @@ class DebugBar
 	public function __invoke(Request $request, Response $response, callable $next)
 	{
 		$load_debugbar = ( class_exists('\\DebugBar\\StandardDebugBar') && ((defined('ENV') && ENV == 'dev') || defined('FORCE_DEBUGBAR') && FORCE_DEBUGBAR)  );
+		$load_debugbar = $load_debugbar && $request->isGet() && ! $request->isAjax();
 
 		if ($load_debugbar) {
 			$debugbar = new StandardDebugBar();

+ 15 - 0
src/Http/Request.php

@@ -117,6 +117,16 @@ class Request
 		return $this->method;
 	}
 
+	public function isGet()
+	{
+		return ($this->method == 'GET');
+	}
+
+	public function isPost()
+	{
+		return ($this->method == 'POST');
+	}
+
 	public function getClientIp()
 	{
 		return $this->client_ip;
@@ -160,6 +170,11 @@ class Request
 		return UserAgent::isBot($this->user_agent);
 	}
 
+	public function isAjax()
+	{
+		return (! empty($request->SERVER['HTTP_X_REQUESTED_WITH']) && $request->SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest');
+	}
+
 	/*
 
 	public function setUrl($url)

+ 2 - 2
src/Kernel.php

@@ -187,8 +187,8 @@ class Kernel
 		});
 
 		$this->set('template', function ($tpl=null, $data=[]) {
-			return new \KarmaFW\Templates\PhpTemplate($tpl, $data);
-			//return new \KarmaFW\Templates\LightweightTemplate($tpl, $data);
+			//return new \KarmaFW\Templates\PhpTemplate($tpl, $data);
+			return new \KarmaFW\Templates\LightweightTemplate($tpl, $data);
 		});
 	}
 

+ 52 - 15
src/Templates/LightweightTemplate.php

@@ -11,7 +11,7 @@ class LightweightTemplate {
 	static $blocks = array();
 	static $cache_path = APP_DIR . '/var/cache';
 	static $tpl_path = APP_DIR . '/templates';
-	static $cache_enabled = (ENV == 'dev') && false;
+	static $cache_enabled = (ENV == 'prod');
 
 
 	protected $data = [];
@@ -33,6 +33,16 @@ class LightweightTemplate {
 			$this->data[$k] = $v;
 		}
 	}
+	
+	public function getVariables() 
+	{
+		return $this->data;
+	}
+	
+	public function getVar($var_name, $default_value=null) 
+	{
+		return isset($this->data[$var_name]) ? $this->data[$var_name] : $default_value;
+	}
 
 	public function fetch($tpl=null, $extra_vars=[], $layout=null, $options=[]) 
 	{
@@ -59,7 +69,9 @@ class LightweightTemplate {
 
 	protected static function cache($file) {
 		if (!file_exists(self::$cache_path)) {
-		  	mkdir(self::$cache_path, 0744);
+		  	if (! @mkdir(self::$cache_path, 0744)) {
+		  		throw new \Exception("Cannot create templates cache dir " . self::$cache_path, 1);
+		  	}
 		}
 	    $cached_file = self::$cache_path . '/' . str_replace(array('/', '.html'), array('_', ''), $file . '.php');
 	    if (!self::$cache_enabled || !file_exists($cached_file) || filemtime($cached_file) < filemtime(self::$tpl_path . '/' . $file)) {
@@ -106,6 +118,12 @@ class LightweightTemplate {
 		$code_init = $code;
 		$layout = null;
 
+		static $tpl_idx = null;
+		if (is_null($tpl_idx) || (empty($caller_file) && empty($parent_file))) {
+			$tpl_idx = 0;
+		}
+		$tpl_idx++;
+
 		$debugbar = App::getData('debugbar');
 		if ($debugbar) {
 			if (isset($debugbar['templates'])) {
@@ -118,21 +136,37 @@ class LightweightTemplate {
 		$ts_start = microtime(true);
 
 
+		// Layout (1)
+		preg_match_all('/{layout ?\'?(.*?)\'? ?}/i', $code, $layout_matches, PREG_SET_ORDER);
+		if ($layout_matches) {
+			$value = $layout_matches[0];
+			$layout = $value[1];
+		} else {
+			$layout = null;
+		}
+
+
 		if (defined('ENV') && ENV == 'dev') {
-			$suffix = '';
+			$tpl_infos = '';
 			if ($caller_file) {
-				$suffix .= ' => caller: ' . $caller_file . '';
+				$tpl_infos .= 'layout for ' . $caller_file . '';
 			}
 			if ($parent_file) {
-				$suffix .= ' => parent: ' . $parent_file . '';
+				$tpl_infos .= 'child of ' . $parent_file . '';
+			}
+			if ($layout) {
+				$tpl_infos .= 'with layout ' . $layout . '';
 			}
-			$code = '<!-- TEMPLATE START : ' . $file . $suffix . ' -->' . $code . '<!-- TEMPLATE END : ' . $file . ' => size: ' . formatSize(strlen($code)) . ' -->';
+
+			$begin = '<!-- BEGIN TEMPLATE #' . $tpl_idx . ' : ' . $file . ' (size: ' . formatSize(strlen($code)) . ' - ' . $tpl_infos . ') -->';
+			$end = '<!-- END TEMPLATE #' . $tpl_idx . ' : ' . $file . ' (size: ' . formatSize(strlen($code)) . ' - ' . $tpl_infos . ') -->';
+
+			$code = PHP_EOL . $begin . PHP_EOL . $code . PHP_EOL . $end . PHP_EOL;
 		}
 
-		// Layout
-		preg_match_all('/{layout ?\'?(.*?)\'? ?}/i', $code, $matches, PREG_SET_ORDER);
-		if ($matches) {
-			$value = $matches[0];
+		// Layout (2)
+		if ($layout_matches) {
+			$value = $layout_matches[0];
 			$layout = $value[1];
 
 			$layout_code = self::includeFiles($layout, $file);
@@ -172,25 +206,28 @@ class LightweightTemplate {
 	}
 
 	protected static function compilePHP($code) {
-		return preg_replace('~\{%\s*(.+?)\s*\%}~is', '<?php $1 ?>', $code);
+		/* return preg_replace('~\{%\s*(.+?)\s*\%}~is', '<?php $1 ?>', $code); */
+		return $code;
 	}
 
 	protected static function compileEchos($code, $strict=false) {
+		// compile PHP variables
 		if ($strict) {
 			$code = preg_replace('~\{\$(.+?)}~is', '<?php echo \$$1 ?>', $code);
-
 		} else {
 			$code = preg_replace('~\{\$(.+?)}~is', '<?php echo isset(\$$1) ? (\$$1) : ""; ?>', $code);
 		}
+		// compile PHP
 		return preg_replace('~\{{\s*(.+?)\s*\}}~is', '<?php echo $1 ?>', $code);
 	}
 
 	protected static function compileEscapedEchos($code) {
+		// compile PHP escaped variables
 		return preg_replace('~\{{{\s*(.+?)\s*\}}}~is', '<?php echo htmlentities($1, ENT_QUOTES, \'UTF-8\') ?>', $code);
 	}
 
 	protected static function compileBlock($code) {
-		preg_match_all('/{% ?block ?(.*?) ?%}(.*?){% ?endblock ?%}/is', $code, $matches, PREG_SET_ORDER);
+		preg_match_all('~{block ?(.*?) ?}(.*?){/block}~is', $code, $matches, PREG_SET_ORDER);
 		foreach ($matches as $value) {
 			if (!array_key_exists($value[1], self::$blocks)) self::$blocks[$value[1]] = '';
 			if (strpos($value[2], '@parent') === false) {
@@ -205,9 +242,9 @@ class LightweightTemplate {
 
 	protected static function compileYield($code) {
 		foreach(self::$blocks as $block => $value) {
-			$code = preg_replace('/{% ?yield ?' . $block . ' ?%}/', $value, $code);
+			$code = preg_replace('/{yield ?' . $block . ' ?}/', $value, $code);
 		}
-		$code = preg_replace('/{% ?yield ?(.*?) ?%}/i', '', $code);
+		$code = preg_replace('/{yield ?(.*?) ?}/i', '', $code);
 		return $code;
 	}
 

+ 2 - 0
src/WebApp.php

@@ -53,6 +53,8 @@ class WebApp extends App
 
 	public static function createTemplate($tpl_path=null, $variables=[], $layout=null, $templates_dirs=null)
 	{
+		//return App::getData('app')->get('template')($tpl_path, $variables);
+
 		return new PhpTemplate($tpl_path, $variables, $layout, $templates_dirs);
 	}