- added stream resources ($smarty->display('mystream://mytemplate'))

- added stream variables  {$mystream:myvar}
This commit is contained in:
Uwe.Tews
2009-04-18 14:46:29 +00:00
parent 24e3218d28
commit 152cedb75c
6 changed files with 861 additions and 724 deletions

View File

@@ -1,3 +1,7 @@
04/18/2009
- added stream resources ($smarty->display('mystream://mytemplate'))
- added stream variables {$mystream:myvar}
04/14/2009
- fixed compile_id handling on {include} tags
- fixed append/prepend attributes in {block} tag

View File

@@ -111,7 +111,7 @@ class Smarty extends Smarty_Internal_TemplateBase {
// config var settings
public $config_overwrite = true; //Controls whether variables with the same name overwrite each other.
public $config_booleanize = true; //Controls whether config values of on/true/yes and off/false/no get converted to boolean
public $config_read_hidden = true; //Controls whether hidden config sections/vars are read from the file.
public $config_read_hidden = true; //Controls whether hidden config sections/vars are read from the file.
// config vars
public $config_vars = array();
// assigned tpl vars
@@ -173,6 +173,9 @@ class Smarty extends Smarty_Internal_TemplateBase {
*/
public function __construct()
{
// set instance object
self::instance($this);
if (is_callable('mb_internal_encoding')) {
$this->has_mb = true;
mb_internal_encoding($this->resource_char_set);
@@ -185,15 +188,13 @@ class Smarty extends Smarty_Internal_TemplateBase {
if (!empty($this->exception_handler))
set_exception_handler($this->exception_handler);
// set default dirs
$this->template_dir = '.' . DIRECTORY_SEPARATOR . 'templates' . DIRECTORY_SEPARATOR;
$this->template_dir = array('.' . DIRECTORY_SEPARATOR . 'templates' . DIRECTORY_SEPARATOR);
$this->compile_dir = '.' . DIRECTORY_SEPARATOR . 'templates_c' . DIRECTORY_SEPARATOR;
$this->plugins_dir = array(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR);
$this->cache_dir = '.' . DIRECTORY_SEPARATOR . 'cache' . DIRECTORY_SEPARATOR;
$this->config_dir = '.' . DIRECTORY_SEPARATOR . 'configs' . DIRECTORY_SEPARATOR;
$this->sysplugins_dir = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'sysplugins' . DIRECTORY_SEPARATOR;
$this->debug_tpl = SMARTY_DIR . 'debug.tpl';
// set instance object
self::instance($this);
// load base plugins
$this->loadPlugin('Smarty_Internal_Base');
$this->loadPlugin('Smarty_Internal_PluginBase');

View File

@@ -0,0 +1,101 @@
<?php
/**
* Smarty Internal Plugin Resource Stream
*
* Implements the streams as resource for Smarty template
*
* @package Smarty
* @subpackage TemplateResources
* @author Uwe Tews
*/
/**
* Smarty Internal Plugin Resource Stream
*/
class Smarty_Internal_Resource_Stream extends Smarty_Internal_Base {
// classes used for compiling Smarty templates from file resource
public $compiler_class = 'Smarty_Internal_SmartyTemplateCompiler';
public $template_lexer_class = 'Smarty_Internal_Templatelexer';
public $template_parser_class = 'Smarty_Internal_Templateparser';
/**
* Get filepath to template source
*
* @param object $_template template object
* @return string return 'string' as template source is not a file
*/
public function getTemplateFilepath($_template)
{
// no filepath for strings
// return resource name for compiler error messages
return $_template->resource_name;
}
/**
* Get timestamp to template source
*
* @param object $_template template object
* @return boolean false as string resources have no timestamp
*/
public function getTemplateTimestamp($_template)
{
// strings must always be compiled and have no timestamp
return false;
}
/**
* Retuen template source from resource name
*
* @param object $_template template object
* @return string content of template source
*/
public function getTemplateSource($_template)
{
// return template string
$_template->template_source = '';
$fp = fopen($_template->resource_name,'r+');
while (!feof($fp)) {
$_template->template_source .= fgets($fp);
}
fclose($fp);
return true;
}
/**
* Return flag that this resource uses the compiler
*
* @return boolean true
*/
public function usesCompiler()
{
// resource string is template, needs compiler
return true;
}
/**
* Return flag that this resource is evaluated
*
* @return boolean true
*/
public function isEvaluated()
{
// compiled template is evaluated instead of saved to disk
return true;
}
/**
* Get filepath to compiled template
*
* @param object $_template template object
* @return boolean return false as compiled template is not stored
*/
public function getCompiledFilepath($_template)
{
// no filepath for strings
return false;
}
}
?>

View File

@@ -473,7 +473,7 @@ class Smarty_Internal_Template extends Smarty_Internal_TemplateBase {
// no resource given, use default
$this->resource_type = $this->smarty->default_resource_type;
$this->resource_name = $template_resource;
} else {
} elseif (strpos($template_resource, '://') != strpos($template_resource, ':')) {
// get type and name from path
list($this->resource_type, $this->resource_name) = explode(':', $template_resource, 2);
if (strlen($this->resource_type) == 1) {
@@ -483,11 +483,15 @@ class Smarty_Internal_Template extends Smarty_Internal_TemplateBase {
} else {
$this->resource_type = strtolower($this->resource_type);
}
} else {
// stream resource
$this->resource_type = 'stream';
$this->resource_name = $template_resource;
}
// load resource handler if required
if (!isset($this->resource_objects[$this->resource_type])) {
// is this an internal or custom resource?
if (in_array($this->resource_type, array('file', 'php', 'string', 'extend'))) {
if (in_array($this->resource_type, array('file', 'php', 'string', 'extend', 'stream'))) {
// internal, get from sysplugins dir
$_resource_class = "Smarty_Internal_Resource_{$this->resource_type}";
} else {
@@ -500,96 +504,96 @@ class Smarty_Internal_Template extends Smarty_Internal_TemplateBase {
}
// cache template object under a unique ID
// do not cache string resources
if ($this->resource_type != 'string') {
Smarty::$template_objects[$this->buildTemplateId ($this->template_resource, $this->cache_id, $this->compile_id)] = $this;
}
return true;
}
/**
* get system filepath to template
*/
public function buildTemplateFilepath ($file = null)
{
if ($file == null) {
$file = $this->resource_name;
}
foreach((array)$this->smarty->template_dir as $_template_dir) {
if (substr($_template_dir, -1) != DIRECTORY_SEPARATOR) {
$_template_dir .= DIRECTORY_SEPARATOR;
if (!in_array($this->resource_type, array('string', 'stream'))) {
Smarty::$template_objects[$this->buildTemplateId ($this->template_resource, $this->cache_id, $this->compile_id)] = $this;
}
return true;
}
$_filepath = $_template_dir . $file;
if (file_exists($_filepath))
return $_filepath;
}
if (file_exists($file)) return $file;
// no tpl file found
if (!empty($this->smarty->default_template_handler_func)) {
if (!is_callable($this->smarty->default_template_handler_func)) {
throw new Exception("Default template handler not callable");
} else {
$_return = call_user_func_array($this->smarty->default_template_handler_func,
array($this->resource_type, $this->resource_name, &$this->template_source, &$this->template_timestamp, &$this));
if ($_return == true) {
return $_filepath;
/**
* get system filepath to template
*/
public function buildTemplateFilepath ($file = null)
{
if ($file == null) {
$file = $this->resource_name;
}
foreach((array)$this->smarty->template_dir as $_template_dir) {
if (substr($_template_dir, -1) != DIRECTORY_SEPARATOR) {
$_template_dir .= DIRECTORY_SEPARATOR;
}
$_filepath = $_template_dir . $file;
if (file_exists($_filepath))
return $_filepath;
}
if (file_exists($file)) return $file;
// no tpl file found
if (!empty($this->smarty->default_template_handler_func)) {
if (!is_callable($this->smarty->default_template_handler_func)) {
throw new Exception("Default template handler not callable");
} else {
$_return = call_user_func_array($this->smarty->default_template_handler_func,
array($this->resource_type, $this->resource_name, &$this->template_source, &$this->template_timestamp, &$this));
if ($_return == true) {
return $_filepath;
}
}
}
throw new Exception("Unable to load template \"{$file}\"");
return false;
}
/**
* Update Smarty variables in parent variable object
*/
public function updateParentVariables ($scope = SMARTY_LOCAL_SCOPE)
{
foreach ($this->tpl_vars as $_key => $_value) {
// copy global vars back to parent
if (isset($this->parent) && ($scope == SMARTY_PARENT_SCOPE || $this->tpl_vars[$_key]->scope == SMARTY_PARENT_SCOPE)) {
if (isset($this->parent->tpl_vars[$_key])) {
// variable is already defined in parent, copy value
$this->parent->tpl_vars[$_key]->value = $this->tpl_vars[$_key]->value;
} else {
// create variable in parent
$this->parent->tpl_vars[$_key] = clone $_value;
$this->smarty->tpl_vars[$_key]->scope = SMARTY_LOCAL_SCOPE;
}
}
if ($scope == SMARTY_ROOT_SCOPE || $this->tpl_vars[$_key]->scope == SMARTY_ROOT_SCOPE) {
$_ptr = $this;
// find root
while ($_ptr->parent != null) {
$_ptr = $_ptr->parent;
}
if (isset($_ptr->tpl_vars[$_key])) {
// variable is already defined in root, copy value
$_ptr->tpl_vars[$_key]->value = $this->tpl_vars[$_key]->value;
} else {
// create variable in root
$_ptr->tpl_vars[$_key] = clone $_value;
$_ptr->tpl_vars[$_key]->scope = SMARTY_LOCAL_SCOPE;
}
}
if ($scope == SMARTY_GLOBAL_SCOPE || $this->tpl_vars[$_key]->scope == SMARTY_GLOBAL_SCOPE) {
if (isset($this->smarty->global_tpl_vars[$_key])) {
// variable is already defined in root, copy value
$this->smarty->global_tpl_vars[$_key]->value = $this->tpl_vars[$_key]->value;
} else {
// create variable in root
$this->smarty->global_tpl_vars[$_key] = clone $_value;
}
$this->smarty->global_tpl_vars[$_key]->scope = SMARTY_LOCAL_SCOPE;
}
}
}
}
throw new Exception("Unable to load template \"{$file}\"");
return false;
}
/**
* Update Smarty variables in parent variable object
*/
public function updateParentVariables ($scope = SMARTY_LOCAL_SCOPE)
{
foreach ($this->tpl_vars as $_key => $_value) {
// copy global vars back to parent
if (isset($this->parent) && ($scope == SMARTY_PARENT_SCOPE || $this->tpl_vars[$_key]->scope == SMARTY_PARENT_SCOPE)) {
if (isset($this->parent->tpl_vars[$_key])) {
// variable is already defined in parent, copy value
$this->parent->tpl_vars[$_key]->value = $this->tpl_vars[$_key]->value;
} else {
// create variable in parent
$this->parent->tpl_vars[$_key] = clone $_value;
$this->smarty->tpl_vars[$_key]->scope = SMARTY_LOCAL_SCOPE;
}
}
if ($scope == SMARTY_ROOT_SCOPE || $this->tpl_vars[$_key]->scope == SMARTY_ROOT_SCOPE) {
$_ptr = $this;
// find root
while ($_ptr->parent != null) {
$_ptr = $_ptr->parent;
}
if (isset($_ptr->tpl_vars[$_key])) {
// variable is already defined in root, copy value
$_ptr->tpl_vars[$_key]->value = $this->tpl_vars[$_key]->value;
} else {
// create variable in root
$_ptr->tpl_vars[$_key] = clone $_value;
$_ptr->tpl_vars[$_key]->scope = SMARTY_LOCAL_SCOPE;
}
}
if ($scope == SMARTY_GLOBAL_SCOPE || $this->tpl_vars[$_key]->scope == SMARTY_GLOBAL_SCOPE) {
if (isset($this->smarty->global_tpl_vars[$_key])) {
// variable is already defined in root, copy value
$this->smarty->global_tpl_vars[$_key]->value = $this->tpl_vars[$_key]->value;
} else {
// create variable in root
$this->smarty->global_tpl_vars[$_key] = clone $_value;
}
$this->smarty->global_tpl_vars[$_key]->scope = SMARTY_LOCAL_SCOPE;
}
/**
* wrapper for template class
*/
class Smarty_Template extends Smarty_Internal_Template {
}
}
}
/**
* wrapper for template class
*/
class Smarty_Template extends Smarty_Internal_Template {
}
?>
?>

View File

@@ -275,6 +275,29 @@ class Smarty_Internal_TemplateBase {
return '';
}
}
/**
* gets a stream variable
*
* @param string $variable the stream of the variable
* @return mixed the value of the global variable
*/
public function getStreamVariable($variable)
{
$_result = '';
if ($fp = fopen($variable, 'r+')) {
while (!feof($fp)) {
$_result .= fgets($fp);
}
fclose($fp);
return $_result;
}
if (Smarty::$error_unassigned) {
throw new Exception('Undefined global variable "' . $variable . '"');
} else {
return '';
}
}
/**
* creates a template object

File diff suppressed because it is too large Load Diff