Permalink
Browse files

Moving common methods to ViewHelper,

Adding a new php view like jinjia2 or django template called EasyView
  • Loading branch information...
1 parent c9639d0 commit 53ee6da419b40bdb87e81f694e39d51a890315e0 @shaunlee committed Aug 30, 2013
Showing with 160 additions and 24 deletions.
  1. +12 −24 ajoy/ajoy.php
  2. +118 −0 ajoy/components/EasyView.php
  3. +30 −0 ajoy/components/ViewHelper.php
View
36 ajoy/ajoy.php
@@ -792,30 +792,6 @@ public function init()
/**
*
*/
- public function encode($value)
- {
- return htmlspecialchars($value);
- }
-
- /**
- *
- */
- public function linebreaksbr($value)
- {
- return preg_replace('/(\r?\n)/', '<br>$1', $value);
- }
-
- /**
- *
- */
- public function datetime($value, $format = 'Y-m-d h:i:s')
- {
- return date($format, strtotime($value));
- }
-
- /**
- *
- */
public function widget($name, array $options = array())
{
$widgetPath = str_replace('.', '/', $name) . '.php';
@@ -911,6 +887,15 @@ public function render($template, array $context = array(), $return = false)
echo $content;
}
+
+ public function __call($method, $arguments)
+ {
+ $helper = ViewHelper::instance();
+ if (method_exists($helper, $method))
+ return call_user_func_array(array($helper, $method), $arguments);
+
+ throw new Exception('Call to undefined method AjoyView::' . $method . '()');
+ }
}
/**
@@ -1321,6 +1306,9 @@ public function run()
spl_autoload_register(array($this, 'loadComponents'));
spl_autoload_register(array($this, 'loadModels'));
+ if ($this->get('timezone'))
+ date_default_timezone_set($this->get('timezone'));
+
$this->emit('init');
$this->dispatch();
View
118 ajoy/components/EasyView.php
@@ -0,0 +1,118 @@
+<?php
+
+class EasyView extends AjoyComponent implements IAjoyView
+{
+
+ /**
+ *
+ */
+ private $context = array();
+
+ /**
+ *
+ */
+ private $layouts = array();
+
+ /**
+ *
+ */
+ private $variables = array();
+
+ /**
+ *
+ */
+ private $viewsPath;
+
+ /**
+ *
+ */
+ private $themesPath;
+
+ /**
+ *
+ */
+ public function init()
+ {
+ $this->viewsPath = app()->get('app root') . '/views';
+ $this->themesPath = app()->get('app root') . '/themes';
+ }
+
+ public function extending($layout)
+ {
+ array_push($this->layouts, $layout);
+ }
+
+ public function block($variable)
+ {
+ array_push($this->variables, $variable);
+ ob_start();
+ }
+
+ public function endblock()
+ {
+ $ctx = ob_get_clean();
+ $variable = array_pop($this->variables);
+ $this->context[$variable] = $ctx;
+ }
+
+ public function render($template, array $context = array(), $return = false)
+ {
+ $filename = $this->themesPath . '/' . app()->get('theme') . '/' . $template . '.php';
+ if (!file_exists($filename))
+ $filename = $this->viewsPath . '/' . $template . '.php';
+ if (!file_exists($filename))
+ app()->raise('Views file with name "' . $this->viewsPath . '/' . $template . '.php" does not exists.');
+
+ extract(app()->locals());
+ extract($this->context);
+ extract($context);
+
+ ob_start();
+ include $filename;
+ $ctx = ob_get_clean();
+
+ if (!empty($this->layouts)) {
+ $layout = array_pop($this->layouts);
+ $ctx = $this->render($layout, array(), true);
+ }
+
+ if ($return)
+ return $ctx;
+
+ echo $ctx;
+ }
+
+ /**
+ *
+ */
+ public function widget($name, array $options = array())
+ {
+ $widgetPath = str_replace('.', '/', $name) . '.php';
+ $filename = $this->themesPath . '/' . app()->get('theme') . '/widgets/' . $widgetPath;
+ if (!file_exists($filename))
+ $filename = app()->get('app root') . '/views/widgets/' . $widgetPath;
+ if (!file_exists($filename))
+ $filename = app()->get('ajoy root') . '/widgets/' . $widgetPath;
+ if (!file_exists($filename))
+ app()->raise('Widget with name "' . $name . '" does not exists.');
+
+ extract($options);
+ ob_start();
+ include $filename;
+ $content = ob_get_clean();
+
+ $content = preg_replace('/\s+(\w+=)/', ' $1', $content);
+ $content = preg_replace('/\s+>/', '>', $content);
+ $content = preg_replace('/(>)\s+|\s+(<)/', '$1$2', $content);
+ return $content;
+ }
+
+ public function __call($method, $arguments)
+ {
+ $helper = ViewHelper::instance();
+ if (method_exists($helper, $method))
+ return call_user_func_array(array($helper, $method), $arguments);
+
+ throw new Exception('Call to undefined method EasyView::' . $method . '()');
+ }
+}
View
30 ajoy/components/ViewHelper.php
@@ -0,0 +1,30 @@
+<?php
+
+class ViewHelper extends AjoyComponent
+{
+
+ /**
+ *
+ */
+ public function encode($value)
+ {
+ return htmlspecialchars($value);
+ }
+
+ /**
+ *
+ */
+ public function linebreaksbr($value)
+ {
+ return preg_replace('/(\r?\n)/', '<br>$1', $value);
+ }
+
+ /**
+ *
+ */
+ public function datetime($value, $format = 'Y-m-d h:i:s')
+ {
+ return date($format, strtotime($value));
+ }
+
+}

0 comments on commit 53ee6da

Please sign in to comment.