function testCompileSimpleBlock() { $block = '<li>{blocktrans}This email <em>{$email}</em> is already registered. If you forgot your password, you can recover it easily.{/blocktrans}</li>'; $compiler = new Pluf_Template_Compiler('dummy', array(), false); $compiler->templateContent = $block; $this->assertEqual('<li><?php ob_start(); ?>This email <em>%%email%%</em> is already registered. If you forgot your password, you can recover it easily.<?php $_b_t_s=ob_get_contents(); ob_end_clean(); echo(Pluf_Translation::sprintf(__($_b_t_s), array(\'email\' => Pluf_Template_safeEcho($t->_vars->email, false)))); ?></li>', $compiler->getCompiledTemplate()); }
public function testCompileMultiFolders() { $folders = array(dirname(__FILE__) . '/tpl1', dirname(__FILE__) . '/tpl2'); $compiler = new Pluf_Template_Compiler('tpl-extends.html', $folders); $compiled = file_get_contents(dirname(__FILE__) . '/tpl-extends.compiled.html'); $this->assertEquals($compiled, $compiler->getCompiledTemplate() . "\n"); }
/** * Constructor. * * If the folder name is not provided, it will default to * Pluf::f('template_folders') * If the cache folder name is not provided, it will default to * Pluf::f('tmp_folder') * * @param string Template name. * @param string Template folder paths (null) * @param string Cache folder name (null) */ function __construct($template, $folders = null, $cache = null) { $this->tpl = $template; if (null == $folders) { $this->folders = Pluf::f('template_folders'); } else { $this->folders = $folders; } if (null == $cache) { $this->cache = Pluf::f('tmp_folder'); } else { $this->cache = $cache; } if (defined('IN_UNIT_TESTS')) { if (!isset($GLOBALS['_PX_tests_templates'])) { $GLOBALS['_PX_tests_templates'] = array(); } } $this->compiled_template = $this->getCompiledTemplateName(); $b = $this->compiled_template[1]; $this->class = 'Pluf_Template_' . $b; $this->compiled_template = $this->compiled_template[0]; if (!class_exists($this->class, false)) { if (!file_exists($this->compiled_template) or Pluf::f('debug')) { $compiler = new Pluf_Template_Compiler($this->tpl, $this->folders); $this->template_content = $compiler->getCompiledTemplate(); $this->write($b); } include $this->compiled_template; } }
public function testCompile() { return ''; $compiler = new Pluf_Template_Compiler('dummy', array(), false); $compiler->templateContent = '{blocktrans $count, $toto}We have one {$toto} element.{plural}We have {$counter} {$toto} elements.{/blocktrans}'; $this->assertEquals('<?php $_b_t_c=$t->_vars[\'count\']; ob_start(); ?>We have one %2$s element.<?php $_b_t_s=ob_get_contents(); ob_end_clean(); ob_start(); ?>We have %1$d %2$s elements.<?php $_b_t_p=ob_get_contents(); ob_end_clean(); echo(sprintf(_n($_b_t_s, $_b_t_p, $_b_t_c), $_b_t_c, Pluf_Template_safeEcho($t->_vars[\'toto\'], false))); ?>', $compiler->getCompiledTemplate()); }
function _parseFunction($name, $args) { switch ($name) { case 'if': $res = 'if (' . $this->_parseFinal($args, $this->_allowedInExpr) . '): '; array_push($this->_blockStack, 'if'); break; case 'else': if (end($this->_blockStack) != 'if') { trigger_error(sprintf(__('End tag of a block missing: %s'), end($this->_blockStack)), E_USER_ERROR); } $res = 'else: '; break; case 'elseif': if (end($this->_blockStack) != 'if') { trigger_error(sprintf(__('End tag of a block missing: %s'), end($this->_blockStack)), E_USER_ERROR); } $res = 'elseif(' . $this->_parseFinal($args, $this->_allowedInExpr) . '):'; break; case 'foreach': $res = 'foreach (' . $this->_parseFinal($args, array_merge(array(T_AS, T_DOUBLE_ARROW, T_STRING, T_OBJECT_OPERATOR, T_LIST, $this->_allowedAssign, '[', ']')), array(';', '!')) . '): '; array_push($this->_blockStack, 'foreach'); break; case 'while': $res = 'while(' . $this->_parseFinal($args, $this->_allowedInExpr) . '):'; array_push($this->_blockStack, 'while'); break; case '/foreach': case '/if': case '/while': $short = substr($name, 1); if (end($this->_blockStack) != $short) { trigger_error(sprintf(__('End tag of a block missing: %s'), end($this->_blockStack)), E_USER_ERROR); } array_pop($this->_blockStack); $res = 'end' . $short . '; '; break; case 'assign': $res = $this->_parseFinal($args, $this->_allowedAssign) . '; '; break; case 'literal': if (count($this->_literals)) { $res = '?>' . array_shift($this->_literals) . '<?php '; } else { trigger_error(__('End tag of a block missing: literal'), E_USER_ERROR); } break; case '/literal': trigger_error(__('Start tag of a block missing: literal'), E_USER_ERROR); break; case 'block': $res = '?>' . $this->_extendBlocks[$args] . '<?php '; break; case 'superblock': $res = '?>~~{~~superblock~~}~~<?php '; break; case 'trans': $argfct = $this->_parseFinal($args, $this->_allowedAssign); $res = 'echo(__(' . $argfct . '));'; break; case 'blocktrans': array_push($this->_blockStack, 'blocktrans'); $res = ''; $this->_transStack = array(); if ($args) { $this->_transPlural = true; $_args = $this->_parseFinal($args, $this->_allowedAssign, array(';', '[', ']'), true); $res .= '$_b_t_c=' . trim(array_shift($_args)) . '; '; } $res .= 'ob_start(); '; break; case '/blocktrans': $short = substr($name, 1); if (end($this->_blockStack) != $short) { trigger_error(sprintf(__('End tag of a block missing: %s'), end($this->_blockStack)), E_USER_ERROR); } $res = ''; if ($this->_transPlural) { $res .= '$_b_t_p=ob_get_contents(); ob_end_clean(); echo('; $res .= 'Pluf_Translation::sprintf(_n($_b_t_s, $_b_t_p, $_b_t_c), array('; $_tmp = array(); foreach ($this->_transStack as $key => $_trans) { $_tmp[] = '\'' . addslashes($key) . '\' => Pluf_Template_safeEcho(' . $_trans . ', false)'; } $res .= implode(', ', $_tmp); unset($_trans, $_tmp); $res .= ')));'; $this->_transStack = array(); } else { $res .= '$_b_t_s=ob_get_contents(); ob_end_clean(); '; if (count($this->_transStack) == 0) { $res .= 'echo(__($_b_t_s)); '; } else { $res .= 'echo(Pluf_Translation::sprintf(__($_b_t_s), array('; $_tmp = array(); foreach ($this->_transStack as $key => $_trans) { $_tmp[] = '\'' . addslashes($key) . '\' => Pluf_Template_safeEcho(' . $_trans . ', false)'; } $res .= implode(', ', $_tmp); unset($_trans, $_tmp); $res .= '))); '; $this->_transStack = array(); } } $this->_transPlural = false; array_pop($this->_blockStack); break; case 'plural': $res = '$_b_t_s=ob_get_contents(); ob_end_clean(); ob_start(); '; break; case 'include': // XXX fixme: Will need some security check, when online editing. $argfct = preg_replace('!^[\'"](.*)[\'"]$!', '$1', $args); $_comp = new Pluf_Template_Compiler($argfct, $this->templateFolders); $res = $_comp->compile(); $this->updateModifierStack($_comp); break; default: $_end = false; $oname = $name; if (substr($name, 0, 1) == '/') { $_end = true; $name = substr($name, 1); } // Here we should allow custom blocks. // Here we start the template tag calls at the template tag // {tag ...} is not a block, so it must be a function. if (!isset($this->_allowedTags[$name])) { trigger_error(sprintf(__('The function tag "%s" is not allowed.'), $name), E_USER_ERROR); } $argfct = $this->_parseFinal($args, $this->_allowedAssign); // $argfct is a string that can be copy/pasted in the PHP code // but we need the array of args. $res = ''; if (isset($this->_extraTags[$name])) { if (false == $_end) { if (method_exists($this->_extraTags[$name], 'start')) { $res .= '$_extra_tag = Pluf::factory(\'' . $this->_allowedTags[$name] . '\', $t); $_extra_tag->start(' . $argfct . '); '; } if (method_exists($this->_extraTags[$name], 'genStart')) { $res .= $this->_extraTags[$name]->genStart(); } } else { if (method_exists($this->_extraTags[$name], 'end')) { $res .= '$_extra_tag = Pluf::factory(\'' . $this->_allowedTags[$name] . '\', $t); $_extra_tag->end(' . $argfct . '); '; } if (method_exists($this->_extraTags[$name], 'genEnd')) { $res .= $this->_extraTags[$name]->genEnd(); } } } if ($res == '') { trigger_error(sprintf(__('The function tag "{%s ...}" is not supported.'), $oname), E_USER_ERROR); } } return $res; }
public function testCompileSimpleAssignInLoop() { $compiler = new Pluf_Template_Compiler('dummy', array(), false); $compiler->templateContent = '{assign $counter = 1} {foreach $lines as $line} {$counter} {assign $counter = $counter+1} {/foreach}'; $this->assertEquals('<?php $t->_vars[\'counter\'] = 1; ?> <?php foreach ($t->_vars[\'lines\'] as $t->_vars[\'line\']): ?> <?php Pluf_Template_safeEcho($t->_vars[\'counter\']); ?> <?php $t->_vars[\'counter\'] = $t->_vars[\'counter\']+1; ?> <?php endforeach; ?>', $compiler->getCompiledTemplate()); }