/** * @param \Box\Brainy\Compiler\TemplateCompiler $compiler * @param string $name * @param string $childBlockVar * @return string */ protected static function compileForced($compiler, $name, $childBlockVar) { $nameVar = '$' . $compiler->getUniqueVarName(); $output = "{$nameVar} = {$name};\n"; $output .= "{$childBlockVar} = \$_smarty_tpl->tpl_vars['smarty']['blocks'][{$nameVar}] ?: null;\n"; $output .= "\$_smarty_tpl->tpl_vars['smarty']['blocks'][{$nameVar}] = function (\$_smarty_tpl) use ({$childBlockVar}) {\n"; return $output; }
/** * @param \Box\Brainy\Compiler\TemplateCompiler $compiler A compiler reference * @param array|null $args Arguments * @return mixed */ public static function compileOpenShorthand(\Box\Brainy\Compiler\TemplateCompiler $compiler, $args) { $start = self::getRequiredArg($args, 'start'); $to = self::getRequiredArg($args, 'to'); $step = self::getOptionalArg($args, 'step', 1); $max = self::getOptionalArg($args, 'max', INF); $var = $start['var']; $value = $start['value']; $total = "ceil(({$step} > 0 ? {$to} + 1 - ({$value}) : {$value} - ({$to}) + 1) / abs({$step}))"; if ($max !== INF) { $total = "min({$total}, {$max})"; } $stepVar = '$' . $compiler->getUniqueVarName(); $totalVar = '$' . $compiler->getUniqueVarName(); $iterationVar = '$' . $compiler->getUniqueVarName(); $output = "{$stepVar} = {$step};\n"; $output .= "{$totalVar} = (int) {$total};\n"; $output .= "if ({$totalVar} > 0) {\n"; $output .= " \$_smarty_tpl->setVariable({$var}, 0);\n"; $varVar = "\$_smarty_tpl->tpl_vars[{$var}]"; $output .= " for ({$varVar} = {$value}, {$iterationVar} = 1; {$iterationVar} <= {$totalVar}; {$varVar} += {$stepVar}, {$iterationVar}++) {\n"; self::openTag($compiler, 'for', array('for')); return $output; }
/** * @param \Box\Brainy\Compiler\TemplateCompiler $compiler A compiler reference * @param array|null $args Arguments * @return mixed */ public static function compileOpen(\Box\Brainy\Compiler\TemplateCompiler $compiler, $args) { $name = self::getRequiredArg($args, 'name'); $assign = self::getOptionalArg($args, 'assign'); $paramArray = self::flattenCompiledArray($args); $paramArray = self::exportArray($paramArray); $tmpVar = '$' . $compiler->getUniqueVarName(); // Evaluate the function name dynamically at runtime $output = "{$tmpVar} = {$name};\n"; // Safety Dance $output .= "if (!array_key_exists({$tmpVar}, \$_smarty_tpl->tpl_vars['smarty']['functions'])) {\n"; $output .= " \$funcs = implode(', ', array_keys(\$_smarty_tpl->tpl_vars['smarty']['functions'])) ?: '<none>';\n"; $output .= " throw new \\Box\\Brainy\\Exceptions\\SmartyException('Call to undefined function \\'' . {$tmpVar} . '\\'. Defined functions: ' . \$funcs);\n"; $output .= "}\n"; if ($assign) { $output .= "ob_start();\n"; } $output .= "\$_smarty_tpl->tpl_vars['smarty']['functions'][{$tmpVar}]({$paramArray});\n"; if ($assign) { $output .= "\$_smarty_tpl->setVariable({$assign}, ob_get_clean());\n"; } return $output; }