public function testRenderSimple()
{
- $template = Template::NewWithData('Hello World');
+ $template = Template::NewWithData('test', 'Hello World');
$this->assertEquals('Hello World', $this->_Render($template));
}
public function testRender1Var()
{
- $template = Template::NewWithData('Hello, {% $name | str %}');
+ $template = Template::NewWithData('test', 'Hello, {% $name | str %}');
$template->name = 'Robert';
$this->assertEquals('Hello, Robert', $this->_Render($template));
}
public function testRender2Vars()
{
- $template = Template::NewWithData('Hello, {% $name %}. Today is the {% $date->day | int %} of July.');
+ $template = Template::NewWithData('test', 'Hello, {% $name %}. Today is the {% $date->day | int %} of July.');
$date = new \stdClass();
$date->day = 26;
$template->name = 'Robert';
public function testRenderIf()
{
- $template = Template::NewWithData(
+ $template = Template::NewWithData('test',
'You are {!% if (!$user->logged_in): %}not logged in{!% else: %}{% $user->name %}{!% endif %}');
$template->user = new \stdClass();
$template->user->logged_in = TRUE;
{
try {
$catch = FALSE;
- $template = Template::NewWithData('Hello %}');
+ $template = Template::NewWithData('test', 'Hello %}');
} catch (\hoplite\views\TemplateException $e) {
$message = $e->GetMessage();
// Check that the column number is correct.
try {
$catch = FALSE;
- $template = Template::NewWithData("Salve\n{% {%");
+ $template = Template::NewWithData('test', "Salve\n{% {%");
} catch (\hoplite\views\TemplateException $e) {
$message = $e->GetMessage();
$this->assertTrue(strpos($message, '2:4') !== FALSE);
try {
$catch = FALSE;
- $template = Template::NewWithData("Salve\n\n{% \$name {!%");
+ $template = Template::NewWithData('test', "Salve\n\n{% \$name {!%");
} catch (\hoplite\views\TemplateException $e) {
$message = $e->GetMessage();
$this->assertTrue(strpos($message, '3:10') !== FALSE);
public function testRenderVars()
{
- $template = Template::NewWithData('Some {% $v %}');
+ $template = Template::NewWithData('test', 'Some {% $v %}');
$this->assertEquals('Some value', $template->Render(array('v' => 'value')));
$template->v = 'other';
namespace hoplite\views;
+use \hoplite\base\Profiling;
+
require_once HOPLITE_ROOT . '/base/filter.php';
+require_once HOPLITE_ROOT . '/base/profiling.php';
/*!
Renders a template with additional vars.
$this->template_name = $name;
}
- static public function NewWithData($data)
+ static public function NewWithData($name, $data)
{
- $template = new Template('');
+ $template = new Template($name);
$template->data = $template->_ProcessTemplate($data);
return $template;
}
- static public function NewWithCompiledData($data)
+ static public function NewWithCompiledData($name, $data)
{
- $template = new Template('');
+ $template = new Template($name);
$template->data = $data;
return $template;
}
error_reporting($error);
+ if (Profiling::IsProfilingEnabled())
+ TemplateLoader::GetInstance()->MarkTemplateRendered($this->name);
+
$data = ob_get_contents();
ob_end_clean();
return $data;
namespace hoplite\views;
+use \hoplite\base\Profiling;
+
+require_once HOPLITE_ROOT . '/base/profiling.php';
require_once HOPLITE_ROOT . '/views/template.php';
/*!
/*! @var array An array of Template objects, keyed by the template name. */
protected $cache = array();
+ /*! @var array Array of template usage counts, keyed by name. Only used
+ when profiling.
+ */
+ protected $usage = array();
+
/*! Gets the singleton instance. */
static public function GetInstance()
{
*/
public function Load($name)
{
+ if (Profiling::IsProfilingEnabled() && !isset($this->usage[$name]))
+ $this->usage[$name] = 0;
+
// First check the memory cache.
if (isset($this->cache[$name]))
return clone $this->cache[$name];
return self::GetInstance()->Load($name);
}
+ /*! Marks a template as having been used. */
+ public function MarkTemplateRendered($name)
+ {
+ if (!isset($this->usage[$name]))
+ throw new \InvalidArgumentException("Template $name has not been loaded through this instance");
+
+ $this->usage[$name]++;
+ }
+
/*!
Loads a cached filesystem template if it is up-to-date.
if ($data === FALSE)
return NULL;
- return Template::NewWithCompiledData($data);
+ return Template::NewWithCompiledData($name, $data);
}
/*!
if ($data === FALSE)
throw new TemplateLoaderException('Could not load template ' . $name);
- $template = Template::NewWithData($data);
+ $template = Template::NewWithData($name, $data);
// Cache the file.
if (file_put_contents($cache_path, $this->cache_prefix . $template->template()) === FALSE)