public function testRender2Vars()
{
- $template = Template::NewWithData('Hello, {% $name %}. Today is the {% $date->day %} of July.');
+ $template = Template::NewWithData('Hello, {% $name %}. Today is the {% $date->day | int %} of July.');
$date = new \stdClass();
$date->day = 26;
$template->name = 'Robert';
$template->user->logged_in = FALSE;
$this->assertEquals('You are not logged in', $this->_Render($template));
}
+
+ public function testExceptions()
+ {
+ try {
+ $catch = FALSE;
+ $template = Template::NewWithData('Hello %}');
+ } catch (\hoplite\views\TemplateException $e) {
+ $message = $e->GetMessage();
+ // Check that the column number is correct.
+ $this->assertTrue(strpos($message, '1:6') !== FALSE);
+ $catch = TRUE;
+ }
+ $this->assertTrue($catch);
+
+ try {
+ $catch = FALSE;
+ $template = Template::NewWithData("Salve\n{% {%");
+ } catch (\hoplite\views\TemplateException $e) {
+ $message = $e->GetMessage();
+ $this->assertTrue(strpos($message, '2:4') !== FALSE);
+ $catch = TRUE;
+ }
+ $this->assertTrue($catch);
+
+ try {
+ $catch = FALSE;
+ $template = Template::NewWithData("Salve\n\n{% \$name {!%");
+ } catch (\hoplite\views\TemplateException $e) {
+ $message = $e->GetMessage();
+ $this->assertTrue(strpos($message, '3:10') !== FALSE);
+ $catch = TRUE;
+ }
+ $this->assertTrue($catch);
+ }
}
$i = 0; // The current position of the iterator.
$looking_for_end = FALSE; // Whehter or not an end tag is expected.
$line_number = 1; // The current line number.
- $column_number = 0; // The current column number.
+ $i_last_line = 0; // The value of |i| at the previous new line, used for column numbering.
while ($i < $length) {
// See how far the current position is from the end of the string.
// When a new line is reached, update the counters.
if ($data[$i] == "\n") {
++$line_number;
- $column_number = 0;
+ $i_last_line = $i;
}
// Check for simple PHP short-tag expansion.
if ($delta >= 3 && substr($data, $i, 3) == '{!%') {
// If an expansion has already been opened, then it's an error to nest.
- if ($looking_for_end)
- throw new TemplateException("Unexpected start of expansion at line $line_number:$column_number");
+ if ($looking_for_end) {
+ $column = $i - $i_last_line;
+ throw new TemplateException("Unexpected start of expansion at line $line_number:$column");
+ }
$looking_for_end = TRUE;
$processed .= '<' . '?php';
// Check for an end tag.
if ($substr == '%}') {
// If an end tag was encountered without an open tag, that's an error.
- if (!$looking_for_end)
- throw new TemplateException("Unexpected end of expansion at line $line_number:$column_number");
+ if (!$looking_for_end) {
+ $column = $i - $i_last_line;
+ throw new TemplateException("Unexpected end of expansion at line $line_number:$column");
+ }
// If this is a macro, it's time to process it.
if ($in_macro)
}
// Check for the beginning of a macro.
else if ($substr == '{%') {
+ // If an expansion has already been opened, then it's an error to nest.
+ if ($looking_for_end) {
+ $column = $i - $i_last_line;
+ throw new TemplateException("Unexpected start of expansion at line $line_number:$column");
+ }
+
$processed .= '<' . '?php echo ';
$macro = '';
$in_macro = TRUE;