move template object to own file and rename it to SemanticScuttle_Model_Template
git-svn-id: https://semanticscuttle.svn.sourceforge.net/svnroot/semanticscuttle/trunk@474 b3834d28-1941-0410-a4f8-b48e95affb8f
This commit is contained in:
parent
cc2c8242c7
commit
6b201d4728
2 changed files with 128 additions and 26 deletions
104
src/SemanticScuttle/Model/Template.php
Normal file
104
src/SemanticScuttle/Model/Template.php
Normal file
|
@ -0,0 +1,104 @@
|
||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* SemanticScuttle - your social bookmark manager.
|
||||||
|
*
|
||||||
|
* PHP version 5.
|
||||||
|
*
|
||||||
|
* @category Bookmarking
|
||||||
|
* @package SemanticScuttle
|
||||||
|
* @author Benjamin Huynh-Kim-Bang <mensonge@users.sourceforge.net>
|
||||||
|
* @author Christian Weiske <cweiske@cweiske.de>
|
||||||
|
* @author Eric Dane <ericdane@users.sourceforge.net>
|
||||||
|
* @license GPL http://www.gnu.org/licenses/gpl.html
|
||||||
|
* @link http://sourceforge.net/projects/semanticscuttle
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* SemanticScuttle HTML templating system.
|
||||||
|
* This templating system is really, really simple and based
|
||||||
|
* on including php files while proving a set of
|
||||||
|
* variables in the template scope.
|
||||||
|
* When rendering templates, they are directly echoed to the
|
||||||
|
* browser. There is no in-built way to capture their output.
|
||||||
|
*
|
||||||
|
* @category Bookmarking
|
||||||
|
* @package SemanticScuttle
|
||||||
|
* @author Benjamin Huynh-Kim-Bang <mensonge@users.sourceforge.net>
|
||||||
|
* @author Christian Weiske <cweiske@cweiske.de>
|
||||||
|
* @author Eric Dane <ericdane@users.sourceforge.net>
|
||||||
|
* @license GPL http://www.gnu.org/licenses/gpl.html
|
||||||
|
* @link http://sourceforge.net/projects/semanticscuttle
|
||||||
|
*/
|
||||||
|
class SemanticScuttle_Model_Template
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Array of variables to be available in template
|
||||||
|
* scope.
|
||||||
|
*
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
protected $vars = array();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* File name of template
|
||||||
|
*/
|
||||||
|
protected $file = '';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Template service instance
|
||||||
|
*
|
||||||
|
* @var SemanticScuttle_Service_Template
|
||||||
|
*/
|
||||||
|
protected $ts;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new template instance
|
||||||
|
*
|
||||||
|
* @param string $file Template filename,
|
||||||
|
* full path
|
||||||
|
* @param array $vars Template variables
|
||||||
|
* @param SemanticScuttle_Service_Template $ts Template service
|
||||||
|
*/
|
||||||
|
public function __construct(
|
||||||
|
$file, $vars = null,
|
||||||
|
SemanticScuttle_Service_Template $ts = null
|
||||||
|
) {
|
||||||
|
$this->vars = $vars;
|
||||||
|
$this->file = $file;
|
||||||
|
$this->ts = $ts;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets variables and includes the template file,
|
||||||
|
* causing it to be rendered.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function parse()
|
||||||
|
{
|
||||||
|
if (isset($this->vars)) {
|
||||||
|
extract($this->vars);
|
||||||
|
}
|
||||||
|
include $this->file;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Loads another template
|
||||||
|
*
|
||||||
|
* @param string $file Filename of template, relative
|
||||||
|
* to template directory
|
||||||
|
*
|
||||||
|
* @return SemanticScuttle_Service_Template Template object
|
||||||
|
*/
|
||||||
|
public function includeTemplate($file)
|
||||||
|
{
|
||||||
|
return $this->ts->loadTemplate($file, $this->vars);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
?>
|
|
@ -13,6 +13,8 @@
|
||||||
* @link http://sourceforge.net/projects/semanticscuttle
|
* @link http://sourceforge.net/projects/semanticscuttle
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
require_once 'SemanticScuttle/Model/Template.php';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* SemanticScuttle template service.
|
* SemanticScuttle template service.
|
||||||
*
|
*
|
||||||
|
@ -26,8 +28,18 @@
|
||||||
*/
|
*/
|
||||||
class SemanticScuttle_Service_Template extends SemanticScuttle_Service
|
class SemanticScuttle_Service_Template extends SemanticScuttle_Service
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* Full path to template directory.
|
||||||
|
*
|
||||||
|
* Set in constructor to
|
||||||
|
* $GLOBALS['TEMPLATES_DIR']
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
protected $basedir;
|
protected $basedir;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the single service instance
|
* Returns the single service instance
|
||||||
*
|
*
|
||||||
|
@ -44,11 +56,18 @@ class SemanticScuttle_Service_Template extends SemanticScuttle_Service
|
||||||
return $instance;
|
return $instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function __construct()
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new instance
|
||||||
|
*/
|
||||||
|
protected function __construct()
|
||||||
{
|
{
|
||||||
$this->basedir = $GLOBALS['TEMPLATES_DIR'];
|
$this->basedir = $GLOBALS['TEMPLATES_DIR'];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Loads and displays a template file.
|
* Loads and displays a template file.
|
||||||
*
|
*
|
||||||
|
@ -56,41 +75,20 @@ class SemanticScuttle_Service_Template extends SemanticScuttle_Service
|
||||||
* to template dir
|
* to template dir
|
||||||
* @param array $vars Array of template variables.
|
* @param array $vars Array of template variables.
|
||||||
*
|
*
|
||||||
* @return Template Template object
|
* @return SemanticScuttle_Model_Template Template object
|
||||||
*/
|
*/
|
||||||
function loadTemplate($template, $vars = null)
|
function loadTemplate($template, $vars = null)
|
||||||
{
|
{
|
||||||
if (substr($template, -4) != '.php') {
|
if (substr($template, -4) != '.php') {
|
||||||
$template .= '.php';
|
$template .= '.php';
|
||||||
}
|
}
|
||||||
$tpl = new Template($this->basedir .'/'. $template, $vars, $this);
|
$tpl = new SemanticScuttle_Model_Template(
|
||||||
|
$this->basedir .'/'. $template, $vars, $this
|
||||||
|
);
|
||||||
$tpl->parse();
|
$tpl->parse();
|
||||||
|
|
||||||
return $tpl;
|
return $tpl;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class Template
|
|
||||||
{
|
|
||||||
var $vars = array();
|
|
||||||
var $file = '';
|
|
||||||
var $templateservice;
|
|
||||||
|
|
||||||
function Template($file, $vars = null, &$templateservice)
|
|
||||||
{
|
|
||||||
$this->vars = $vars;
|
|
||||||
$this->file = $file;
|
|
||||||
$this->templateservice = $templateservice;
|
|
||||||
}
|
|
||||||
|
|
||||||
function parse() {
|
|
||||||
if (isset($this->vars))
|
|
||||||
extract($this->vars);
|
|
||||||
include($this->file);
|
|
||||||
}
|
|
||||||
|
|
||||||
function includeTemplate($name) {
|
|
||||||
return $this->templateservice->loadTemplate($name, $this->vars);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
?>
|
?>
|
Loading…
Reference in a new issue