Skip to content

Commit

Permalink
2.1 global variables
Browse files Browse the repository at this point in the history
  • Loading branch information
Aleksandr-ru committed May 24, 2016
1 parent 15803d5 commit 8c2f105
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 13 deletions.
49 changes: 36 additions & 13 deletions TemplateObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Another simple template parser
* @author Rebel
* @copyright (c) 2016 Aleksandr.ru
* @version 2.0
* @version 2.1
* @link https://github.com/Aleksandr-ru/TemplateObject
*
* Based on features of HTML_Template_IT by Ulf Wendel, Pierre-Alain Joye
Expand All @@ -15,6 +15,7 @@
* 1.2 multiple filter support like {{VAR|html|nl2br}}
* 1.3 ability to get variables and blocks from loaded template
* 2.0 now one template can extend another template by replacing it's blocks with own content
* 2.1 global variables: inherited to all child blocks
*/
class TemplateObject
{
Expand Down Expand Up @@ -92,6 +93,12 @@ class TemplateObject
* containers for variable markup and variable's data
*/
protected $variables, $vardata;

/**
* @var $vardata_global
* container for global variable's data
*/
protected $vardata_global;

/**
* @var $filters
Expand Down Expand Up @@ -127,13 +134,15 @@ static function loadTemplate($file)
* constructor
* @param string $data in case of template from variable or DB
* @param string $base_dir working directory for template
* @param array $global_variables inherited variables array('VARNAME' => 'value', ...)
*/
function __construct($data = '', $base_dir = '')
function __construct($data = '', $base_dir = '', $global_variables = array())
{
$this->__destruct();

$this->tmpl = $data;
$this->base_dir = $base_dir ? $base_dir : getcwd();
$this->vardata_global = is_array($global_variables) ? $global_variables : array();

$this->parseExtend();
$this->parseIncludes();
Expand All @@ -152,7 +161,8 @@ function __destruct()
$this->includes = array();
$this->base_dir = '';
$this->blocks = $this->blockdata = array();
$this->vardata = $this->vardata = array();
$this->variables = $this->vardata = array();
$this->vardata_global = array();
$this->extended = $this->extend_blocks = array();
}

Expand Down Expand Up @@ -191,7 +201,21 @@ function setBlock($blockname)
return FALSE;
}
$this->out = '';
return $this->blockdata[$blockname][] = new self($this->blocks[$blockname]['data'], $this->base_dir);
return $this->blockdata[$blockname][] = new self($this->blocks[$blockname]['data'], $this->base_dir, $this->vardata_global);
}

/**
* Set the variable in global scope
* @param string $var name of the variable
* @param string $val value of the variable
*
* @return bool - variable exists in the template
*/
function setGlobalVariable($var, $val)
{
$this->vardata_global[$var] = $val;
$this->out = '';
return isset($this->variables[$var]);
}

/**
Expand All @@ -212,7 +236,6 @@ function setVariable($var, $val)
return TRUE;
}


/**
* Set variables from array('VAR1' => 'value',
* 'VAR2' => 'another value',
Expand Down Expand Up @@ -266,14 +289,15 @@ function getOutput()
if($this->out) return $this->out;

$this->out = $this->tmpl;
$empty = TRUE;
//$empty = TRUE;
$vardata = array_merge($this->vardata_global, $this->vardata);

if($this->variables) foreach ($this->variables as $var => $vv) {
foreach($vv as $filter) {
$search = sprintf(self::PLACEHOLDER_VAR, $var, $filter);
if(isset($this->vardata[$var])) {
$empty = FALSE;
$replace = $this->applyVarFilter($this->vardata[$var], $filter);
$search = sprintf(self::PLACEHOLDER_VAR, $var, $filter);
if(isset($vardata[$var])) {
//$empty = FALSE;
$replace = $this->applyVarFilter($vardata[$var], $filter);
$this->out = str_replace($search, $replace, $this->out);
}
else {
Expand All @@ -289,16 +313,15 @@ function getOutput()
foreach ($this->blockdata[$blockname] as $b) {
$replace .= $b->getOutput();
}
if($replace) $empty = FALSE;
//if($replace) $empty = FALSE;
}
if(!$replace && isset($block['empty'])) {
$empty = FALSE;
//$empty = FALSE;
$replace = $block['empty'];
}
$this->out = str_replace($search, $replace, $this->out);
}

//if($empty) trigger_error("Template is empty!", E_USER_NOTICE);
return $this->out;
}

Expand Down
35 changes: 35 additions & 0 deletions tests/global.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<!DOCTYPE html>
<html>
<head>
<title>{{TITLE}}</title>
</head>
<body>
<h1>{{TITLE}}</h1>
<hr>
<!-- BEGIN head -->
<p>This is the header</p>
<p>Global var: {{GLOBAL}}</p>
<!-- END head -->
<hr>
<!-- BEGIN content -->
<table border="1">
<caption>This is the content</caption>
<tr>
<th>Column-1</th>
<th>Global</th>
</tr>
<!-- BEGIN row -->
<tr>
<td>{{COL|html}}</td>
<td>{{GLOBAL}}</td>
</tr>
<!-- END row -->
</table>
<!-- END content -->
<hr>
<!-- BEGIN foot -->
<p>This is the footer</p>
<p>Global var: {{GLOBAL}}</p>
<!-- END foot -->
</body>
</html>
23 changes: 23 additions & 0 deletions tests/global.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php
//error_reporting(E_ALL);
//ini_set('display_errors', 'on');
//header("content-type: text/plain");
require('../TemplateObject.php');

$to = TemplateObject::loadTemplate('global.html');

$to->setBlock('head');

$to->setVariable('TITLE', "this is a 'title'");
$to->setGlobalVariable('GLOBAL', "this is a GLOBAL variable");

$to->setBlock('foot');

$b = $to->setBlock('content');
for($i = 1; $i <= 3; $i++) {
$r = $b->setBlock('row');
$r->setVariable('COL', $i);
}

$to->showOutput();
?>

0 comments on commit 8c2f105

Please sign in to comment.