Skip to content

Commit

Permalink
Moving common methods to ViewHelper,
Browse files Browse the repository at this point in the history
Adding a new php view like jinjia2 or django template called EasyView
  • Loading branch information
shaunlee committed Aug 30, 2013
1 parent c9639d0 commit 53ee6da
Show file tree
Hide file tree
Showing 3 changed files with 160 additions and 24 deletions.
36 changes: 12 additions & 24 deletions ajoy/ajoy.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -789,30 +789,6 @@ public function init()
$this->themesPath = app()->get('app root') . '/themes'; $this->themesPath = app()->get('app root') . '/themes';
} }


/**
*
*/
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));
}

/** /**
* *
*/ */
Expand Down Expand Up @@ -911,6 +887,15 @@ public function render($template, array $context = array(), $return = false)


echo $content; 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 . '()');
}
} }


/** /**
Expand Down Expand Up @@ -1321,6 +1306,9 @@ public function run()
spl_autoload_register(array($this, 'loadComponents')); spl_autoload_register(array($this, 'loadComponents'));
spl_autoload_register(array($this, 'loadModels')); spl_autoload_register(array($this, 'loadModels'));


if ($this->get('timezone'))
date_default_timezone_set($this->get('timezone'));

$this->emit('init'); $this->emit('init');


$this->dispatch(); $this->dispatch();
Expand Down
118 changes: 118 additions & 0 deletions ajoy/components/EasyView.php
Original file line number Original file line Diff line number Diff line change
@@ -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 . '()');
}
}
30 changes: 30 additions & 0 deletions ajoy/components/ViewHelper.php
Original file line number Original file line Diff line number Diff line change
@@ -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.