This is an automated email from the git hooks/post-receive script. It was
generated because a ref change was pushed to the repository containing
the project "quickfw".
The branch, master has been updated
via 6dfe8794c21d2befbef4f56c99800c4a61f3a9fb (commit)
via 7244fb58d5cf1c1acc4772d8c03d9c3f3feb6e87 (commit)
via eeb3e008bd0de770576593a80908b559ceddc558 (commit)
via df4f34df1412cc6060b14d08262069ca6d426e59 (commit)
via 6a7066be3d91ed12b4e9fd51bbaf8b3cfc153d83 (commit)
via 0769b685b8d16c2b67bdc8cf4d1d0a1e36e8b39c (commit)
from a64d640d1933b3d598afff24fac0c2a8e8f85016 (commit)
Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.
- Log -----------------------------------------------------------------
commit 6dfe8794c21d2befbef4f56c99800c4a61f3a9fb
Merge: a64d640 7244fb5
Author: Ivan Borzenkov <iva...@li...>
Date: Sun Jul 24 14:07:04 2011 +0400
Merge branch 'View'
commit 7244fb58d5cf1c1acc4772d8c03d9c3f3feb6e87
Author: Ivan Borzenkov <iva...@li...>
Date: Sun Jul 24 13:37:49 2011 +0400
Документация по View
diff --git a/doc/asciidoc/templates.txt b/doc/asciidoc/templates.txt
index 23e354c..e10a0fa 100644
--- a/doc/asciidoc/templates.txt
+++ b/doc/asciidoc/templates.txt
@@ -24,9 +24,26 @@ $config['templater'] = array(
include::../../QFW/Templater/Templater.php[]
---------------------------------------------
-Основные функции это +assign+ и +fetch+, причем вторую обязательно нужно переопредетить - она возвращает обработанный шаблон.
+Класс View
+~~~~~~~~~~
-На данный момент есть обертки для шаблонизаторов *Smarty* и *Twig* Проксирующий шаблонизатор и PlainPHP.
+Объект класса +View+ представляет собой переменную, хранящую в себе имя шаблона и все переменные.
+Для объекта переопределено преобразование в строку, конструктор и фабрика.
+
+Общий функционал
+~~~~~~~~~~~~~~~~
+
+Областей видимости переменных шаблона всего три:
+
+. Глобальная область видимости
+. Область видимости только этого объекта
+. Область видимости только этого вызова
+
+Значения в глобальной области задаются через функцию +assign+, оставленную для совместимости с предыдущим вариантов и статическую функцию +set_global+.
+
+Через __set метод задаются переменные объекта. Если использовать только объект +QFW::$view+ и не создавать дополнительные объекты шаблонизатора, то области видимости глобальных значений и значений объекта совпадают, причем значения объекта перекрывают глобальные.
+
+Локальные значения при вызове передаются вторым параметром в функцию +fetch+ и влияют только на этот вызов. Их можно использовать в каких-либо циклах.
Smarty, Twig и остальные
~~~~~~~~~~~~~~~~~~~~~~~~
commit eeb3e008bd0de770576593a80908b559ceddc558
Author: Ivan Borzenkov <iva...@li...>
Date: Thu Jul 21 22:37:18 2011 +0400
Расширение php в список прокси
diff --git a/application/default.php b/application/default.php
index 63c8d19..c222309 100644
--- a/application/default.php
+++ b/application/default.php
@@ -99,6 +99,7 @@ $config['templater']= array(
'exts' => array(
'tpl' => 'Smarty',
'html' => 'PlainView',
+ 'php' => 'PlainView',
),
);
commit df4f34df1412cc6060b14d08262069ca6d426e59
Author: Ivan Borzenkov <iva...@li...>
Date: Thu Jul 21 22:20:56 2011 +0400
Перенесены функции в общий класс
diff --git a/QFW/Templater/Templater.php b/QFW/Templater/Templater.php
index 34024fd..17bc247 100644
--- a/QFW/Templater/Templater.php
+++ b/QFW/Templater/Templater.php
@@ -27,9 +27,23 @@ abstract class Templater
$this->mainTemplate = $mainTpl;
}
- public function __get($name)
+ /**
+ * Возвращает значения переменных в шаблоне
+ * Используется для изменения массивов
+ *
+ * @param string $var имя переменной
+ * @return mixed значение
+ */
+ public function &__get($key)
{
- return $this->getTemplateVars($name);
+ if (isset($this->_vars[$key]))
+ return $this->_vars[$key];
+
+ if (isset(static::$global_vars[$key]))
+ return static::$global_vars[$key];
+
+ $this->_vars[$key] = false;
+ return $this->_vars[$key];
}
public function __set($name, $value)
@@ -37,6 +51,11 @@ abstract class Templater
$this->_vars[$name] = $value;
}
+ public function __isset($name)
+ {
+ return isset($this->_vars[$name]) || isset(static::$global_vars[$name]);
+ }
+
/**
* Sets a view variable.
*
@@ -104,12 +123,18 @@ abstract class Templater
* @param string|array имя переменной или массив имен
*/
public function delete($spec)
- { //TODO: уделание из всех
+ {
if (is_array($spec))
foreach ($spec as $item)
+ {
unset($this->_vars[$item]);
+ unset(static::$global_vars[$item]);
+ }
else
+ {
unset($this->_vars[$spec]);
+ unset(static::$global_vars[$spec]);
+ }
}
/**
@@ -117,11 +142,15 @@ abstract class Templater
*
* @param string name of variable
* @param mixed variable to assign by reference
+ * @param bool variable bind to global data
* @return object
*/
- public function bind($name, & $var)
+ public function bind($name, & $var, $global = false)
{
- $this->_vars[$name] =& $var;
+ if ($global)
+ static::$global_vars[$name] =& $var;
+ else
+ $this->_vars[$name] =& $var;
return $this;
}
@@ -141,7 +170,7 @@ abstract class Templater
* @return mixed значение
*/
public function getTemplateVars($var = null)
- { //TODO: получение из всех
+ {
if ($var === null)
return $this->_vars;
elseif (isset($this->_vars[$var]))
diff --git a/lib/View.php b/lib/View.php
index 495b064..6264fc9 100644
--- a/lib/View.php
+++ b/lib/View.php
@@ -1,23 +1,18 @@
<?php
/**
- * Loads and displays Kohana view files. Can also handle output of some binary
- * files, such as image, Javascript, and CSS files.
- *
- * $Id: View.php 4072 2009-03-13 17:20:38Z jheathco $
+ * Класс шаблона, представляющего собой переменную,
+ * основан на шаблоне из Kohana
*
* @author Kohana Team
* @copyright (c) 2007-2008 Kohana Team
- * @license http://kohanaphp.com/license.html
+ * @author Ivan Borzenkov <iva...@li...>
*/
-class View extends Templater {
+class View extends Templater
+{
// The view file name and type
protected $filename = FALSE;
- // View variable storage
- protected $kohana_local_data = array();
- protected static $kohana_global_data = array();
-
/**
* Creates a new View using the given parameters.
*
@@ -43,36 +38,17 @@ class View extends Templater {
public function __construct($name = NULL, $data = NULL)
{
if (is_string($name) AND $name !== '')
- {
- // Set the filename
$this->set_filename($name);
- }
-
- if (is_array($data) AND ! empty($data))
- {
- // Preload data using array_merge, to allow user extensions
+ if (is_array($data) AND !empty($data))
$this->_vars = array_merge($this->_vars, $data);
- }
$this->_tmplPath = QFW::$view->getScriptPath();
}
-
- /**
- * Magic method access to test for view property
- *
- * @param string View property to test for
- * @return boolean
- */
- public function __isset($key = NULL)
- {
- return $this->is_set($key);
- }
/**
* Sets the view filename.
*
* @chainable
* @param string view filename
- * @param string view file type
* @return object
*/
public function set_filename($name)
@@ -83,86 +59,6 @@ class View extends Templater {
}
/**
- * Sets a view variable.
- *
- * @param string|array name of variable or an array of variables
- * @param mixed value when using a named variable
- * @return object
- */
- public function set($name, $value = NULL)
- {
- if (is_array($name))
- {
- foreach ($name as $key => $value)
- {
- $this->__set($key, $value);
- }
- }
- else
- {
- $this->__set($name, $value);
- }
-
- return $this;
- }
-
- /**
- * Checks for a property existence in the view locally or globally. Unlike the built in __isset(),
- * this method can take an array of properties to test simultaneously.
- *
- * @param string $key property name to test for
- * @param array $key array of property names to test for
- * @return boolean property test result
- * @return array associative array of keys and boolean test result
- */
- public function is_set( $key = FALSE )
- { //TODO: посмотреть
- // Setup result;
- $result = FALSE;
-
- // If key is an array
- if (is_array($key))
- {
- // Set the result to an array
- $result = array();
-
- // Foreach key
- foreach ($key as $property)
- {
- // Set the result to an associative array
- $result[$property] = (array_key_exists($property, $this->kohana_local_data) OR array_key_exists($property, View::$kohana_global_data)) ? TRUE : FALSE;
- }
- }
- else
- {
- // Otherwise just check one property
- $result = (array_key_exists($key, $this->kohana_local_data) OR array_key_exists($key, View::$kohana_global_data)) ? TRUE : FALSE;
- }
-
- // Return the result
- return $result;
- }
-
- /**
- * Magically gets a view variable.
- *
- * @param string variable key
- * @return mixed variable value if the key is found
- * @return void if the key is not found
- */
- public function &__get($key)
- { //TODO: посмотреть
- if (isset($this->kohana_local_data[$key]))
- return $this->kohana_local_data[$key];
-
- if (isset(View::$kohana_global_data[$key]))
- return View::$kohana_global_data[$key];
-
- if (isset($this->$key))
- return $this->$key;
- }
-
- /**
* Renders a view.
*
* @param boolean set to TRUE to echo the output instead of returning it
commit 6a7066be3d91ed12b4e9fd51bbaf8b3cfc153d83
Author: Ivan Borzenkov <iva...@li...>
Date: Thu Jul 21 21:48:04 2011 +0400
Правка View и добавление трех уровней переменных
diff --git a/QFW/Templater/PlainView.php b/QFW/Templater/PlainView.php
index 037780d..a96ee90 100644
--- a/QFW/Templater/PlainView.php
+++ b/QFW/Templater/PlainView.php
@@ -71,6 +71,7 @@ class Templater_PlainView extends Templater
public function fetch($tmpl, $vars=array())
{
+ extract(static::$global_vars, EXTR_OVERWRITE);
extract($this->_vars, EXTR_OVERWRITE);
extract($vars, EXTR_OVERWRITE);
$P=&$this->P;
diff --git a/QFW/Templater/Proxy.php b/QFW/Templater/Proxy.php
index 1e8a641..b92712e 100644
--- a/QFW/Templater/Proxy.php
+++ b/QFW/Templater/Proxy.php
@@ -21,18 +21,8 @@ class Templater_Proxy extends Templater
}
/**
- * Assign variables to the template
- *
- * Allows setting a specific key to the specified value, OR passing an array
- * of key => value pairs to set en masse.
- *
- * @see __set()
- * @param string|array $spec The assignment strategy to use (key or array of key
- * => value pairs)
- * @param mixed $value (Optional) If assigning a named variable, use this
- * as the value.
- * @return void
- */
+ * добавляем unsyncronize
+ */
public function assign($spec, $value = null)
{
$this->unsyncronize();
@@ -40,6 +30,12 @@ class Templater_Proxy extends Templater
return $this;
}
+ public function __set($name, $value)
+ {
+ $this->unsyncronize();
+ parent::__set($name, $value);
+ }
+
/**
* Clear assigned variable
*
diff --git a/QFW/Templater/Smarty.php b/QFW/Templater/Smarty.php
index 7a85aad..423684d 100644
--- a/QFW/Templater/Smarty.php
+++ b/QFW/Templater/Smarty.php
@@ -113,15 +113,8 @@ class Templater_Smarty extends Templater
public function fetch($name, $vars=array())
{
- if (!$vars)
- return $this->getEngine()->fetch($name);
- //если есть локальные переменные - сохраняем старые
- //Smarty не поддерживает локальные переменные
- $old = $this->getEngine()->get_template_vars();
- $this->getEngine()->assign($vars);
+ $this->getEngine()->assign($vars + $this->_vars + static::$global_vars);
$data = $this->getEngine()->fetch($name);
- $this->getEngine()->clear_all_assign();
- $this->getEngine()->assign($old);
return $data;
}
diff --git a/QFW/Templater/Templater.php b/QFW/Templater/Templater.php
index a2da74b..34024fd 100644
--- a/QFW/Templater/Templater.php
+++ b/QFW/Templater/Templater.php
@@ -5,8 +5,9 @@
*/
abstract class Templater
{
+ protected static $global_vars = array();
/** @var array переменные, установленные в шаблоне */
- protected $_vars;
+ protected $_vars = array();
/** @var string путь к шаблонам */
protected $_tmplPath;
@@ -33,10 +34,41 @@ abstract class Templater
public function __set($name, $value)
{
- $this->assign($name, $value);
+ $this->_vars[$name] = $value;
}
/**
+ * Sets a view variable.
+ *
+ * @param string|array name of variable or an array of variables
+ * @param mixed value when using a named variable
+ * @return object
+ */
+ public function set($name, $value = NULL)
+ {
+ if (is_array($name))
+ $this->_vars = array_merge($this->_vars, $name);
+ else
+ $this->__set($name, $value);
+ return $this;
+ }
+
+ /**
+ * Функция для совместимости с kohana View
+ *
+ * @static
+ * @param $name
+ * @param null $value
+ * @return void
+ */
+ public static function set_global($name, $value = NULL)
+ {
+ if (is_array($name))
+ static::$global_vars = array_merge(static::$global_vars, $name);
+ else
+ static::$global_vars[$name] = $value;
+ }
+ /**
* Присваение значения переменной шаблона
*
* Allows setting a specific key to the specified value, OR passing an array
@@ -48,10 +80,7 @@ abstract class Templater
*/
public function assign($name, $value = null)
{
- if (is_array($name))
- $this->_vars = array_merge($this->_vars, $name);
- else
- $this->_vars[$name] = $value;
+ self::set_global($name, $value);
return $this;
}
@@ -63,9 +92,9 @@ abstract class Templater
*/
public function append($name, $value)
{
- if (empty($this->_vars[$name]))
- $this->_vars[$name] = array();
- $this->_vars[$name][] = $value;
+ if (empty(static::$global_vars[$name]))
+ static::$global_vars[$name] = array();
+ static::$global_vars[$name][] = $value;
return $this;
}
@@ -75,7 +104,7 @@ abstract class Templater
* @param string|array имя переменной или массив имен
*/
public function delete($spec)
- {
+ { //TODO: уделание из всех
if (is_array($spec))
foreach ($spec as $item)
unset($this->_vars[$item]);
@@ -84,6 +113,19 @@ abstract class Templater
}
/**
+ * Sets a bound variable by reference.
+ *
+ * @param string name of variable
+ * @param mixed variable to assign by reference
+ * @return object
+ */
+ public function bind($name, & $var)
+ {
+ $this->_vars[$name] =& $var;
+ return $this;
+ }
+
+ /**
* Очистка всех установленных переменных
*
*/
@@ -99,7 +141,7 @@ abstract class Templater
* @return mixed значение
*/
public function getTemplateVars($var = null)
- {
+ { //TODO: получение из всех
if ($var === null)
return $this->_vars;
elseif (isset($this->_vars[$var]))
diff --git a/QFW/Templater/Twig.php b/QFW/Templater/Twig.php
index 619e246..f857609 100644
--- a/QFW/Templater/Twig.php
+++ b/QFW/Templater/Twig.php
@@ -66,7 +66,7 @@ class Templater_Twig extends Templater
{
$template = $this->getEngine()->loadTemplate($name);
$this->assign('P', $this->P);
- return $template->render($vars + $this->_vars);
+ return $template->render($vars + $this->_vars + static::$global_vars);
}
/**
diff --git a/lib/View.php b/lib/View.php
old mode 100755
new mode 100644
index 2b8471c..495b064
--- a/lib/View.php
+++ b/lib/View.php
@@ -1,20 +1,18 @@
-<?php defined('SYSPATH') OR die('No direct access allowed.');
+<?php
/**
* Loads and displays Kohana view files. Can also handle output of some binary
* files, such as image, Javascript, and CSS files.
*
* $Id: View.php 4072 2009-03-13 17:20:38Z jheathco $
*
- * @package Core
* @author Kohana Team
* @copyright (c) 2007-2008 Kohana Team
* @license http://kohanaphp.com/license.html
*/
-class View_Core {
+class View extends Templater {
// The view file name and type
- protected $kohana_filename = FALSE;
- protected $kohana_filetype = FALSE;
+ protected $filename = FALSE;
// View variable storage
protected $kohana_local_data = array();
@@ -28,9 +26,9 @@ class View_Core {
* @param string type of file: html, css, js, etc.
* @return object
*/
- public static function factory($name = NULL, $data = NULL, $type = NULL)
+ public static function factory($name = NULL, $data = NULL)
{
- return new View($name, $data, $type);
+ return new View($name, $data);
}
/**
@@ -42,21 +40,22 @@ class View_Core {
* @param string type of file: html, css, js, etc.
* @return void
*/
- public function __construct($name = NULL, $data = NULL, $type = NULL)
+ public function __construct($name = NULL, $data = NULL)
{
if (is_string($name) AND $name !== '')
{
// Set the filename
- $this->set_filename($name, $type);
+ $this->set_filename($name);
}
if (is_array($data) AND ! empty($data))
{
// Preload data using array_merge, to allow user extensions
- $this->kohana_local_data = array_merge($this->kohana_local_data, $data);
+ $this->_vars = array_merge($this->_vars, $data);
}
+ $this->_tmplPath = QFW::$view->getScriptPath();
}
-
+
/**
* Magic method access to test for view property
*
@@ -76,31 +75,10 @@ class View_Core {
* @param string view file type
* @return object
*/
- public function set_filename($name, $type = NULL)
+ public function set_filename($name)
{
- if ($type == NULL)
- {
- // Load the filename and set the content type
- $this->kohana_filename = Kohana::find_file('views', $name, TRUE);
- $this->kohana_filetype = EXT;
- }
- else
- {
- // Check if the filetype is allowed by the configuration
- if ( ! in_array($type, Kohana::config('view.allowed_filetypes')))
- throw new Kohana_Exception('core.invalid_filetype', $type);
-
- // Load the filename and set the content type
- $this->kohana_filename = Kohana::find_file('views', $name, TRUE, $type);
- $this->kohana_filetype = Kohana::config('mimes.'.$type);
-
- if ($this->kohana_filetype == NULL)
- {
- // Use the specified type
- $this->kohana_filetype = $type;
- }
- }
-
+ // Load the filename and set the content type
+ $this->filename = $name;
return $this;
}
@@ -129,7 +107,7 @@ class View_Core {
}
/**
- * Checks for a property existence in the view locally or globally. Unlike the built in __isset(),
+ * Checks for a property existence in the view locally or globally. Unlike the built in __isset(),
* this method can take an array of properties to test simultaneously.
*
* @param string $key property name to test for
@@ -138,7 +116,7 @@ class View_Core {
* @return array associative array of keys and boolean test result
*/
public function is_set( $key = FALSE )
- {
+ { //TODO: посмотреть
// Setup result;
$result = FALSE;
@@ -147,7 +125,7 @@ class View_Core {
{
// Set the result to an array
$result = array();
-
+
// Foreach key
foreach ($key as $property)
{
@@ -166,54 +144,6 @@ class View_Core {
}
/**
- * Sets a bound variable by reference.
- *
- * @param string name of variable
- * @param mixed variable to assign by reference
- * @return object
- */
- public function bind($name, & $var)
- {
- $this->kohana_local_data[$name] =& $var;
-
- return $this;
- }
-
- /**
- * Sets a view global variable.
- *
- * @param string|array name of variable or an array of variables
- * @param mixed value when using a named variable
- * @return void
- */
- public static function set_global($name, $value = NULL)
- {
- if (is_array($name))
- {
- foreach ($name as $key => $value)
- {
- View::$kohana_global_data[$key] = $value;
- }
- }
- else
- {
- View::$kohana_global_data[$name] = $value;
- }
- }
-
- /**
- * Magically sets a view variable.
- *
- * @param string variable key
- * @param string variable value
- * @return void
- */
- public function __set($key, $value)
- {
- $this->kohana_local_data[$key] = $value;
- }
-
- /**
* Magically gets a view variable.
*
* @param string variable key
@@ -221,7 +151,7 @@ class View_Core {
* @return void if the key is not found
*/
public function &__get($key)
- {
+ { //TODO: посмотреть
if (isset($this->kohana_local_data[$key]))
return $this->kohana_local_data[$key];
@@ -233,6 +163,21 @@ class View_Core {
}
/**
+ * Renders a view.
+ *
+ * @param boolean set to TRUE to echo the output instead of returning it
+ * @return string if print is FALSE
+ * @return void if print is TRUE
+ */
+ public function fetch($tmpl=false, $vars=array())
+ {
+ $tmpl = $tmpl ? $tmpl : $this->filename;
+ // Merge global and local data, local overrides global with the same name
+ $data = array_merge($this->_vars, $vars);
+ return QFW::$view->fetch($tmpl, $data);
+ }
+
+ /**
* Magically converts view object to string.
*
* @return string
@@ -241,7 +186,7 @@ class View_Core {
{
try
{
- return $this->render();
+ return $this->fetch('', array());
}
catch (Exception $e)
{
@@ -249,61 +194,4 @@ class View_Core {
return (string) $e;
}
}
-
- /**
- * Renders a view.
- *
- * @param boolean set to TRUE to echo the output instead of returning it
- * @param callback special renderer to pass the output through
- * @return string if print is FALSE
- * @return void if print is TRUE
- */
- public function render($print = FALSE, $renderer = FALSE)
- {
- if (empty($this->kohana_filename))
- throw new Kohana_Exception('core.view_set_filename');
-
- if (is_string($this->kohana_filetype))
- {
- // Merge global and local data, local overrides global with the same name
- $data = array_merge(View::$kohana_global_data, $this->kohana_local_data);
-
- // Load the view in the controller for access to $this
- $output = Kohana::$instance->_kohana_load_view($this->kohana_filename, $data);
-
- if ($renderer !== FALSE AND is_callable($renderer, TRUE))
- {
- // Pass the output through the user defined renderer
- $output = call_user_func($renderer, $output);
- }
-
- if ($print === TRUE)
- {
- // Display the output
- echo $output;
- return;
- }
- }
- else
- {
- // Set the content type and size
- header('Content-Type: '.$this->kohana_filetype[0]);
-
- if ($print === TRUE)
- {
- if ($file = fopen($this->kohana_filename, 'rb'))
- {
- // Display the output
- fpassthru($file);
- fclose($file);
- }
- return;
- }
-
- // Fetch the file contents
- $output = file_get_contents($this->kohana_filename);
- }
-
- return $output;
- }
-} // End View
\ No newline at end of file
+}
commit 0769b685b8d16c2b67bdc8cf4d1d0a1e36e8b39c
Author: Ivan Borzenkov <iva...@li...>
Date: Thu Jul 21 21:35:03 2011 +0400
View из Коханы
diff --git a/lib/View.php b/lib/View.php
new file mode 100755
index 0000000..2b8471c
--- /dev/null
+++ b/lib/View.php
@@ -0,0 +1,309 @@
+<?php defined('SYSPATH') OR die('No direct access allowed.');
+/**
+ * Loads and displays Kohana view files. Can also handle output of some binary
+ * files, such as image, Javascript, and CSS files.
+ *
+ * $Id: View.php 4072 2009-03-13 17:20:38Z jheathco $
+ *
+ * @package Core
+ * @author Kohana Team
+ * @copyright (c) 2007-2008 Kohana Team
+ * @license http://kohanaphp.com/license.html
+ */
+class View_Core {
+
+ // The view file name and type
+ protected $kohana_filename = FALSE;
+ protected $kohana_filetype = FALSE;
+
+ // View variable storage
+ protected $kohana_local_data = array();
+ protected static $kohana_global_data = array();
+
+ /**
+ * Creates a new View using the given parameters.
+ *
+ * @param string view name
+ * @param array pre-load data
+ * @param string type of file: html, css, js, etc.
+ * @return object
+ */
+ public static function factory($name = NULL, $data = NULL, $type = NULL)
+ {
+ return new View($name, $data, $type);
+ }
+
+ /**
+ * Attempts to load a view and pre-load view data.
+ *
+ * @throws Kohana_Exception if the requested view cannot be found
+ * @param string view name
+ * @param array pre-load data
+ * @param string type of file: html, css, js, etc.
+ * @return void
+ */
+ public function __construct($name = NULL, $data = NULL, $type = NULL)
+ {
+ if (is_string($name) AND $name !== '')
+ {
+ // Set the filename
+ $this->set_filename($name, $type);
+ }
+
+ if (is_array($data) AND ! empty($data))
+ {
+ // Preload data using array_merge, to allow user extensions
+ $this->kohana_local_data = array_merge($this->kohana_local_data, $data);
+ }
+ }
+
+ /**
+ * Magic method access to test for view property
+ *
+ * @param string View property to test for
+ * @return boolean
+ */
+ public function __isset($key = NULL)
+ {
+ return $this->is_set($key);
+ }
+
+ /**
+ * Sets the view filename.
+ *
+ * @chainable
+ * @param string view filename
+ * @param string view file type
+ * @return object
+ */
+ public function set_filename($name, $type = NULL)
+ {
+ if ($type == NULL)
+ {
+ // Load the filename and set the content type
+ $this->kohana_filename = Kohana::find_file('views', $name, TRUE);
+ $this->kohana_filetype = EXT;
+ }
+ else
+ {
+ // Check if the filetype is allowed by the configuration
+ if ( ! in_array($type, Kohana::config('view.allowed_filetypes')))
+ throw new Kohana_Exception('core.invalid_filetype', $type);
+
+ // Load the filename and set the content type
+ $this->kohana_filename = Kohana::find_file('views', $name, TRUE, $type);
+ $this->kohana_filetype = Kohana::config('mimes.'.$type);
+
+ if ($this->kohana_filetype == NULL)
+ {
+ // Use the specified type
+ $this->kohana_filetype = $type;
+ }
+ }
+
+ return $this;
+ }
+
+ /**
+ * Sets a view variable.
+ *
+ * @param string|array name of variable or an array of variables
+ * @param mixed value when using a named variable
+ * @return object
+ */
+ public function set($name, $value = NULL)
+ {
+ if (is_array($name))
+ {
+ foreach ($name as $key => $value)
+ {
+ $this->__set($key, $value);
+ }
+ }
+ else
+ {
+ $this->__set($name, $value);
+ }
+
+ return $this;
+ }
+
+ /**
+ * Checks for a property existence in the view locally or globally. Unlike the built in __isset(),
+ * this method can take an array of properties to test simultaneously.
+ *
+ * @param string $key property name to test for
+ * @param array $key array of property names to test for
+ * @return boolean property test result
+ * @return array associative array of keys and boolean test result
+ */
+ public function is_set( $key = FALSE )
+ {
+ // Setup result;
+ $result = FALSE;
+
+ // If key is an array
+ if (is_array($key))
+ {
+ // Set the result to an array
+ $result = array();
+
+ // Foreach key
+ foreach ($key as $property)
+ {
+ // Set the result to an associative array
+ $result[$property] = (array_key_exists($property, $this->kohana_local_data) OR array_key_exists($property, View::$kohana_global_data)) ? TRUE : FALSE;
+ }
+ }
+ else
+ {
+ // Otherwise just check one property
+ $result = (array_key_exists($key, $this->kohana_local_data) OR array_key_exists($key, View::$kohana_global_data)) ? TRUE : FALSE;
+ }
+
+ // Return the result
+ return $result;
+ }
+
+ /**
+ * Sets a bound variable by reference.
+ *
+ * @param string name of variable
+ * @param mixed variable to assign by reference
+ * @return object
+ */
+ public function bind($name, & $var)
+ {
+ $this->kohana_local_data[$name] =& $var;
+
+ return $this;
+ }
+
+ /**
+ * Sets a view global variable.
+ *
+ * @param string|array name of variable or an array of variables
+ * @param mixed value when using a named variable
+ * @return void
+ */
+ public static function set_global($name, $value = NULL)
+ {
+ if (is_array($name))
+ {
+ foreach ($name as $key => $value)
+ {
+ View::$kohana_global_data[$key] = $value;
+ }
+ }
+ else
+ {
+ View::$kohana_global_data[$name] = $value;
+ }
+ }
+
+ /**
+ * Magically sets a view variable.
+ *
+ * @param string variable key
+ * @param string variable value
+ * @return void
+ */
+ public function __set($key, $value)
+ {
+ $this->kohana_local_data[$key] = $value;
+ }
+
+ /**
+ * Magically gets a view variable.
+ *
+ * @param string variable key
+ * @return mixed variable value if the key is found
+ * @return void if the key is not found
+ */
+ public function &__get($key)
+ {
+ if (isset($this->kohana_local_data[$key]))
+ return $this->kohana_local_data[$key];
+
+ if (isset(View::$kohana_global_data[$key]))
+ return View::$kohana_global_data[$key];
+
+ if (isset($this->$key))
+ return $this->$key;
+ }
+
+ /**
+ * Magically converts view object to string.
+ *
+ * @return string
+ */
+ public function __toString()
+ {
+ try
+ {
+ return $this->render();
+ }
+ catch (Exception $e)
+ {
+ // Display the exception using its internal __toString method
+ return (string) $e;
+ }
+ }
+
+ /**
+ * Renders a view.
+ *
+ * @param boolean set to TRUE to echo the output instead of returning it
+ * @param callback special renderer to pass the output through
+ * @return string if print is FALSE
+ * @return void if print is TRUE
+ */
+ public function render($print = FALSE, $renderer = FALSE)
+ {
+ if (empty($this->kohana_filename))
+ throw new Kohana_Exception('core.view_set_filename');
+
+ if (is_string($this->kohana_filetype))
+ {
+ // Merge global and local data, local overrides global with the same name
+ $data = array_merge(View::$kohana_global_data, $this->kohana_local_data);
+
+ // Load the view in the controller for access to $this
+ $output = Kohana::$instance->_kohana_load_view($this->kohana_filename, $data);
+
+ if ($renderer !== FALSE AND is_callable($renderer, TRUE))
+ {
+ // Pass the output through the user defined renderer
+ $output = call_user_func($renderer, $output);
+ }
+
+ if ($print === TRUE)
+ {
+ // Display the output
+ echo $output;
+ return;
+ }
+ }
+ else
+ {
+ // Set the content type and size
+ header('Content-Type: '.$this->kohana_filetype[0]);
+
+ if ($print === TRUE)
+ {
+ if ($file = fopen($this->kohana_filename, 'rb'))
+ {
+ // Display the output
+ fpassthru($file);
+ fclose($file);
+ }
+ return;
+ }
+
+ // Fetch the file contents
+ $output = file_get_contents($this->kohana_filename);
+ }
+
+ return $output;
+ }
+} // End View
\ No newline at end of file
-----------------------------------------------------------------------
Summary of changes:
QFW/Templater/PlainView.php | 1 +
QFW/Templater/Proxy.php | 20 ++++------
QFW/Templater/Smarty.php | 9 +----
QFW/Templater/Templater.php | 93 +++++++++++++++++++++++++++++++++++++-----
QFW/Templater/Twig.php | 2 +-
application/default.php | 1 +
doc/asciidoc/templates.txt | 21 +++++++++-
lib/View.php | 93 +++++++++++++++++++++++++++++++++++++++++++
8 files changed, 206 insertions(+), 34 deletions(-)
create mode 100644 lib/View.php
hooks/post-receive
--
quickfw
|