- 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

@@ -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,7 +504,7 @@ 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') {
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;

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