Support block calls with positional parameters in PHP version

databind
Vitaliy Filippov 2015-03-09 22:48:02 +03:00
parent cc4434a2fe
commit 7b2f556c71
2 changed files with 24 additions and 12 deletions

View File

@ -4,7 +4,7 @@
* Homepage: http://yourcmc.ru/wiki/VMX::Template * Homepage: http://yourcmc.ru/wiki/VMX::Template
* License: GNU GPLv3 or later * License: GNU GPLv3 or later
* Author: Vitaliy Filippov, 2006-2015 * Author: Vitaliy Filippov, 2006-2015
* Version: V3 (LALR), 2015-02-17 * Version: V3 (LALR), 2015-03-09
* *
* This file contains the implementation of VMX::Template compiler. * This file contains the implementation of VMX::Template compiler.
* It is only used when a template is compiled in runtime. * It is only used when a template is compiled in runtime.
@ -194,7 +194,7 @@ class VMXTemplateCompiler
foreach ($this->st->functions as $n => $f) foreach ($this->st->functions as $n => $f)
{ {
$code .= $f['body']; $code .= $f['body'];
$functions[$n] = true; $functions[$n] = $f['args'];
} }
// Assemble the class code // Assemble the class code
@ -225,7 +225,7 @@ $code
$fn = self::$functions[$fn]; $fn = self::$functions[$fn];
} }
$argv = []; $argv = [];
$q = @self::$functionSafeness[$fn]; $q = isset(self::$functionSafeness[$fn]) ? self::$functionSafeness[$fn] : false;
if ($q > 0) if ($q > 0)
{ {
$q = isset($args[$q-1]) ? $args[$q-1][1] : true; $q = isset($args[$q-1]) ? $args[$q-1][1] : true;
@ -234,7 +234,7 @@ $code
{ {
$q = true; $q = true;
} }
else elseif ($q == self::Q_IF_ALL || $q == self::Q_ALL_BUT_FIRST)
{ {
$q = true; $q = true;
$n = count($args); $n = count($args);
@ -259,8 +259,9 @@ $code
} }
else else
{ {
$this->lexer->warn("Unknown function: '$fn'"); // A block reference or unknown function
$r = "false"; $r = "\$this->parent->call_block_list('$fn', array(".implode(', ', $argv)."), '".addcslashes($this->lexer->errorinfo(), "'\\")."')";
$q = true;
} }
return [ $r, $q ]; return [ $r, $q ];
} }

View File

@ -8,7 +8,7 @@
* Homepage: http://yourcmc.ru/wiki/VMX::Template * Homepage: http://yourcmc.ru/wiki/VMX::Template
* License: GNU GPLv3 or later * License: GNU GPLv3 or later
* Author: Vitaliy Filippov, 2006-2015 * Author: Vitaliy Filippov, 2006-2015
* Version: V3 (LALR), 2015-02-17 * Version: V3 (LALR), 2015-03-09
* *
* The template engine is split into two parts: * The template engine is split into two parts:
* (1) This file - always used when running templates * (1) This file - always used when running templates
@ -65,7 +65,7 @@ class VMXTemplate
const TS_RFC822 = 7; const TS_RFC822 = 7;
// Version of code classes, saved into static $version // Version of code classes, saved into static $version
const CODE_VERSION = 4; const CODE_VERSION = 5;
// Data passed to the template // Data passed to the template
var $tpldata = array(); var $tpldata = array();
@ -296,11 +296,11 @@ class VMXTemplate
); );
return NULL; return NULL;
} }
foreach ($class::$functions as $loaded_function => $true) foreach ($class::$functions as $loaded_function => $args)
{ {
// FIXME Do it better // FIXME Do it better
// Remember functions during file loading // Remember functions during file loading
$this->function_search_path[$loaded_function][] = $fn; $this->function_search_path[$loaded_function][] = array($fn, $args);
} }
} }
} }
@ -459,10 +459,21 @@ class VMXTemplate
if (isset($this->function_search_path[$block])) if (isset($this->function_search_path[$block]))
{ {
// FIXME maybe do it better! // FIXME maybe do it better!
$fn = $this->function_search_path[$block][0]; $fn = $this->function_search_path[$block][0][0];
return $this->parse_real($fn, NULL, $block, $args); return $this->parse_real($fn, NULL, $block, $args);
} }
throw new VMXTemplateException("$errorinfo Unknown block '$block'"); throw new VMXTemplateException("Unknown block '$block'$errorinfo");
}
function call_block_list($block, $args, $errorinfo)
{
if (isset($this->function_search_path[$block]))
{
$fun = $this->function_search_path[$block][0];
$args = array_combine($fun[1], array_pad(array_slice($args, 0, count($fun[1])), count($fun[1]), NULL));
return $this->parse_real($fun[0], NULL, $block, $args);
}
throw new VMXTemplateException("Unknown block or function '$block'$errorinfo");
} }
static function array1($a) static function array1($a)