Commit 232df72c authored by Taddes Kroes's avatar Taddes Kroes

Added base of template parser.

parent 2198131d
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
include_once '../debug.php'; include_once '../debug.php';
include_once 'pquery.php'; include_once 'pquery.php';
__p::require_plugins('array', 'sql'); __p::require_plugins('array', 'sql', 'template');
// Array test // Array test
/*$a = _p(range(0, 10)); /*$a = _p(range(0, 10));
...@@ -11,16 +11,17 @@ while( !$a->is_empty() ) { ...@@ -11,16 +11,17 @@ while( !$a->is_empty() ) {
debug($a->pop(), $a->reverse()->pop()); debug($a->pop(), $a->reverse()->pop());
}*/ }*/
echo '<br><br>';
// SQL test // SQL test
$sql = _sql("select * from posts where slug = '[slug]'") /*$sql = _sql("select * from posts where slug = '[slug]'")
->set(array('slug' => 'contact')); ->set(array('slug' => 'contact'));
$results = $sql->fetch_all('object'); $results = $sql->fetch_all('object');
$results = _arr($results); $results = _arr($results);
debug($results); debug($results);*/
__tpl::set_root('templates');
$tpl = _tpl('test.tpl');
debug($tpl->content);
?> ?>
\ No newline at end of file
...@@ -51,7 +51,7 @@ class pQueryArray extends pQuery implements pQueryExtension { ...@@ -51,7 +51,7 @@ class pQueryArray extends pQuery implements pQueryExtension {
* @see pQuerySql::__construct * @see pQuerySql::__construct
*/ */
function _arr($array) { function _arr($array) {
return pQuery::create('pQueryArray', $array); return pQuery::create('array', $array);
} }
pQuery::extend('pQueryArray', 'array'); pQuery::extend('pQueryArray', 'array');
......
...@@ -113,11 +113,11 @@ class pQuery { ...@@ -113,11 +113,11 @@ class pQuery {
* *
* @param string $error The error message to display. * @param string $error The error message to display.
*/ */
static function error($error/*, $arg1, $arg2...*/) { static function error($error /* , $arg1, $arg2... */) {
$args = func_get_args(); $args = func_get_args();
if( DEBUG ) { if( DEBUG ) {
call_user_func_array('printf', $args); echo nl2br(call_user_func_array('sprintf', $args));
//echo debug_backtrace(); //echo debug_backtrace();
} }
...@@ -132,12 +132,20 @@ class pQuery { ...@@ -132,12 +132,20 @@ class pQuery {
*/ */
static function create() { static function create() {
$args = func_get_args(); $args = func_get_args();
$class_name = array_shift($args); $plugin = array_shift($args);
$obj = $class_name === null ? new self() : new $class_name();
$variable = array_shift($args); if( $plugin === null )
$class_name = 'self';
elseif( isset(self::$plugins[$plugin]) )
$class_name = self::$plugins[$plugin];
elseif( in_array($plugin, self::$plugins) )
$class_name = $plugin;
else
return self::error('Plugin "%s" does not exist.', $plugin);
$obj = new $class_name();
$obj->arguments = $args; $obj->arguments = $args;
$obj->set_variable($variable); $obj->set_variable(array_shift($args));
return $obj; return $obj;
} }
......
...@@ -236,13 +236,13 @@ class pQuerySql extends pQuery implements pQueryExtension { ...@@ -236,13 +236,13 @@ class pQuerySql extends pQuery implements pQueryExtension {
/** /**
* Shortcut constructor for {@link pQuerySql}. * Shortcut constructor for {@link pQuerySql}.
* *
* @returns pQuerySql A new pQuerySql instance. * @param string $query A MySQL query to evaluate.
* @see pQuerySql::__construct * @returns pQuerySql A new SQL query instance.
*/ */
function _sql($query /* [ , $arg1, ... ] */) { function _sql($query /* [ , $arg1, ... ] */) {
$args = func_get_args(); $args = func_get_args();
$query = array_shift($args); $query = array_shift($args);
array_unshift($args, 'pQuerySql', $query); array_unshift($args, 'sql', $query);
return call_user_func_array('pQuery::create', $args); return call_user_func_array('pQuery::create', $args);
} }
......
<?php
/**
* pQuery plugin for parsing templates.
*
* @package pQuery
*/
/**
* @todo Documentation
* @property $ Alias for {@link pQuery::variable}.
*/
class pQueryTemplate extends pQuery implements pQueryExtension {
static $accepts = array('string' => 'open_template_file');
/**
* Root folders from which template files will be included.
*
* @var string
*/
static $include_path = array();
/**
* @see pQuery::$variable_alias
* @var string|array
*/
static $variable_alias = 'template';
/**
* Open the given template filename in the current variable.
*/
function open_template_file() {
$found = false;
foreach( self::$include_path as $root ) {
$path = $root.$this->variable;
if( is_file($path) ) {
$found = true;
break;
}
}
if( !$found ) {
return self::error("Could not find template file \"%s\", looked in folders:\n%s",
$this->variable, implode("\n", self::$include_path));
}
$this->content = file_get_contents($path);
}
/**
* Replace all include paths by a single new one.
*
* @param str $path The path to set.
* @param bool $relative Indicates whether the path is relative to the document root.
*/
static function set_root($path, $relative=true) {
self::$include_path = array();
self::add_root($path);
}
/**
* Add a new include path.
*
* @param str $path The path to add.
* @param bool $relative Indicates whether the path is relative to the document root.
*/
static function add_root($path, $relative=true) {
$relative && $path = PQUERY_ROOT.$path;
preg_match('%/$%', $path) || $path .= '/';
if( !is_dir($path) )
return self::error('"%s" is not a directory.', $path);
self::$include_path[] = $path;
}
}
/**
* Shortcut constructor for {@link pQueryTemplate}.
*
* @param string $path The path to a template file.
* @returns pQueryTemplate A new template instance.
*/
function _tpl($path) {
return pQuery::create('tpl', $path);
}
__p::extend('pQueryTemplate', 'tpl');
__tpl::set_root('');
?>
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment