|
@@ -3,7 +3,7 @@
|
|
|
/*
|
|
/*
|
|
|
* This file is part of Mustache.php.
|
|
* This file is part of Mustache.php.
|
|
|
*
|
|
*
|
|
|
- * (c) 2010-2014 Justin Hileman
|
|
|
|
|
|
|
+ * (c) 2010-2015 Justin Hileman
|
|
|
*
|
|
*
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
* file that was distributed with this source code.
|
|
* file that was distributed with this source code.
|
|
@@ -19,6 +19,7 @@ class Mustache_Compiler
|
|
|
private $pragmas;
|
|
private $pragmas;
|
|
|
private $defaultPragmas = array();
|
|
private $defaultPragmas = array();
|
|
|
private $sections;
|
|
private $sections;
|
|
|
|
|
+ private $blocks;
|
|
|
private $source;
|
|
private $source;
|
|
|
private $indentNextLine;
|
|
private $indentNextLine;
|
|
|
private $customEscape;
|
|
private $customEscape;
|
|
@@ -43,6 +44,7 @@ class Mustache_Compiler
|
|
|
{
|
|
{
|
|
|
$this->pragmas = $this->defaultPragmas;
|
|
$this->pragmas = $this->defaultPragmas;
|
|
|
$this->sections = array();
|
|
$this->sections = array();
|
|
|
|
|
+ $this->blocks = array();
|
|
|
$this->source = $source;
|
|
$this->source = $source;
|
|
|
$this->indentNextLine = true;
|
|
$this->indentNextLine = true;
|
|
|
$this->customEscape = $customEscape;
|
|
$this->customEscape = $customEscape;
|
|
@@ -195,6 +197,7 @@ class Mustache_Compiler
|
|
|
return $buffer;
|
|
return $buffer;
|
|
|
}
|
|
}
|
|
|
%s
|
|
%s
|
|
|
|
|
+ %s
|
|
|
}';
|
|
}';
|
|
|
|
|
|
|
|
const KLASS_NO_LAMBDAS = '<?php
|
|
const KLASS_NO_LAMBDAS = '<?php
|
|
@@ -225,19 +228,19 @@ class Mustache_Compiler
|
|
|
{
|
|
{
|
|
|
$code = $this->walk($tree);
|
|
$code = $this->walk($tree);
|
|
|
$sections = implode("\n", $this->sections);
|
|
$sections = implode("\n", $this->sections);
|
|
|
- $klass = empty($this->sections) ? self::KLASS_NO_LAMBDAS : self::KLASS;
|
|
|
|
|
|
|
+ $blocks = implode("\n", $this->blocks);
|
|
|
|
|
+ $klass = empty($this->sections) && empty($this->blocks) ? self::KLASS_NO_LAMBDAS : self::KLASS;
|
|
|
|
|
|
|
|
$callable = $this->strictCallables ? $this->prepare(self::STRICT_CALLABLE) : '';
|
|
$callable = $this->strictCallables ? $this->prepare(self::STRICT_CALLABLE) : '';
|
|
|
|
|
|
|
|
- return sprintf($this->prepare($klass, 0, false, true), $name, $callable, $code, $sections);
|
|
|
|
|
|
|
+ return sprintf($this->prepare($klass, 0, false, true), $name, $callable, $code, $sections, $blocks);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
const BLOCK_VAR = '
|
|
const BLOCK_VAR = '
|
|
|
- $value = $this->resolveValue($context->findInBlock(%s), $context, $indent);
|
|
|
|
|
- if ($value && !is_array($value) && !is_object($value)) {
|
|
|
|
|
- $buffer .= $value;
|
|
|
|
|
- } else {
|
|
|
|
|
- %s
|
|
|
|
|
|
|
+ $blockFunction = $context->findInBlock(%s);
|
|
|
|
|
+ if (is_callable($blockFunction)) {
|
|
|
|
|
+ $buffer .= call_user_func($blockFunction, $context);
|
|
|
|
|
+ } else {%s
|
|
|
}
|
|
}
|
|
|
';
|
|
';
|
|
|
|
|
|
|
@@ -258,14 +261,10 @@ class Mustache_Compiler
|
|
|
{
|
|
{
|
|
|
$id = var_export($id, true);
|
|
$id = var_export($id, true);
|
|
|
|
|
|
|
|
- return sprintf($this->prepare(self::BLOCK_VAR, $level), $id, $this->walk($nodes, 2));
|
|
|
|
|
|
|
+ return sprintf($this->prepare(self::BLOCK_VAR, $level), $id, $this->walk($nodes, $level));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- const BLOCK_ARG = '
|
|
|
|
|
- // %s block_arg
|
|
|
|
|
- $value = $this->section%s($context, \'\', true);
|
|
|
|
|
- $newContext[%s] = %s$value;
|
|
|
|
|
- ';
|
|
|
|
|
|
|
+ const BLOCK_ARG = '$newContext[%s] = array($this, \'block%s\');';
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* Generate Mustache Template inheritance block argument PHP source.
|
|
* Generate Mustache Template inheritance block argument PHP source.
|
|
@@ -282,10 +281,39 @@ class Mustache_Compiler
|
|
|
*/
|
|
*/
|
|
|
private function blockArg($nodes, $id, $start, $end, $otag, $ctag, $level)
|
|
private function blockArg($nodes, $id, $start, $end, $otag, $ctag, $level)
|
|
|
{
|
|
{
|
|
|
- $key = $this->section($nodes, $id, array(), $start, $end, $otag, $ctag, $level, true);
|
|
|
|
|
- $id = var_export($id, true);
|
|
|
|
|
|
|
+ $key = $this->block($nodes);
|
|
|
|
|
+ $keystr = var_export($key, true);
|
|
|
|
|
+ $id = var_export($id, true);
|
|
|
|
|
+
|
|
|
|
|
+ return sprintf($this->prepare(self::BLOCK_ARG, 1), $id, $key);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ const BLOCK_FUNCTION = '
|
|
|
|
|
+ public function block%s($context)
|
|
|
|
|
+ {
|
|
|
|
|
+ $indent = $buffer = \'\';%s
|
|
|
|
|
+
|
|
|
|
|
+ return $buffer;
|
|
|
|
|
+ }
|
|
|
|
|
+ ';
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Generate Mustache Template inheritance block function PHP source.
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param array $nodes Array of child tokens
|
|
|
|
|
+ *
|
|
|
|
|
+ * @return string key of new block function
|
|
|
|
|
+ */
|
|
|
|
|
+ private function block($nodes)
|
|
|
|
|
+ {
|
|
|
|
|
+ $code = $this->walk($nodes, 0);
|
|
|
|
|
+ $key = ucfirst(md5($code));
|
|
|
|
|
|
|
|
- return sprintf($this->prepare(self::BLOCK_ARG, $level), $id, $key, $id, $this->flushIndent());
|
|
|
|
|
|
|
+ if (!isset($this->blocks[$key])) {
|
|
|
|
|
+ $this->blocks[$key] = sprintf($this->prepare(self::BLOCK_FUNCTION, 0), $key, $code);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return $key;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
const SECTION_CALL = '
|
|
const SECTION_CALL = '
|
|
@@ -318,7 +346,8 @@ class Mustache_Compiler
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
return $buffer;
|
|
return $buffer;
|
|
|
- }';
|
|
|
|
|
|
|
+ }
|
|
|
|
|
+ ';
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* Generate Mustache Template section PHP source.
|
|
* Generate Mustache Template section PHP source.
|
|
@@ -368,7 +397,8 @@ class Mustache_Compiler
|
|
|
$value = $context->%s(%s);%s
|
|
$value = $context->%s(%s);%s
|
|
|
if (empty($value)) {
|
|
if (empty($value)) {
|
|
|
%s
|
|
%s
|
|
|
- }';
|
|
|
|
|
|
|
+ }
|
|
|
|
|
+ ';
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* Generate Mustache Template inverted section PHP source.
|
|
* Generate Mustache Template inverted section PHP source.
|
|
@@ -601,6 +631,12 @@ class Mustache_Compiler
|
|
|
return 'last';
|
|
return 'last';
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ if (isset($this->pragmas[Mustache_Engine::PRAGMA_ANCHORED_DOT]) && $this->pragmas[Mustache_Engine::PRAGMA_ANCHORED_DOT]) {
|
|
|
|
|
+ if (substr($id, 0, 1) === '.') {
|
|
|
|
|
+ return 'findAnchoredDot';
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
if (strpos($id, '.') === false) {
|
|
if (strpos($id, '.') === false) {
|
|
|
return 'find';
|
|
return 'find';
|
|
|
}
|
|
}
|