mirror of
https://github.com/Lastorder-DC/rhymix.git
synced 2026-04-30 07:42:11 +09:00
Inherit parent vars and add own vars when a template is included with vars from another template that has vars; add unit tests for vars inheritance
This commit is contained in:
parent
5b47151440
commit
6085b82d19
7 changed files with 92 additions and 12 deletions
|
|
@ -50,42 +50,42 @@ class TemplateParserV2Test extends \Codeception\Test\Unit
|
|||
{
|
||||
// Basic usage
|
||||
$source = '<include src="foobar" />';
|
||||
$target = '<?php $__tpl = new \Rhymix\Framework\Template($this->relative_dirname, "foobar", "html"); echo $__tpl->compile(); ?>';
|
||||
$target = '<?php $__tpl = new \Rhymix\Framework\Template($this->relative_dirname, "foobar", "html"); if ($this->vars): $__tpl->setVars($this->vars); endif; echo $__tpl->compile(); ?>';
|
||||
$this->assertEquals($target, $this->_parse($source));
|
||||
|
||||
// Legacy 'target' attribute
|
||||
$source = '<include target="subdir/foobar" />';
|
||||
$target = '<?php $__tpl = new \Rhymix\Framework\Template($this->relative_dirname, "subdir/foobar", "html"); echo $__tpl->compile(); ?>';
|
||||
$target = '<?php $__tpl = new \Rhymix\Framework\Template($this->relative_dirname, "subdir/foobar", "html"); if ($this->vars): $__tpl->setVars($this->vars); endif; echo $__tpl->compile(); ?>';
|
||||
$this->assertEquals($target, $this->_parse($source));
|
||||
|
||||
// Conditional include
|
||||
$source = '<include src="../up/foobar" if="$cond" />';
|
||||
$target = '<?php if(!empty($cond)): ?><?php $__tpl = new \Rhymix\Framework\Template($this->relative_dirname, "../up/foobar", "html"); echo $__tpl->compile(); ?><?php endif; ?>';
|
||||
$target = '<?php if(!empty($cond)): ?><?php $__tpl = new \Rhymix\Framework\Template($this->relative_dirname, "../up/foobar", "html"); if ($this->vars): $__tpl->setVars($this->vars); endif; echo $__tpl->compile(); ?><?php endif; ?>';
|
||||
$this->assertEquals($target, $this->_parse($source));
|
||||
|
||||
// Conditional include with legacy 'cond' attribute
|
||||
$source = '<include target="legacy/cond.statement.html" cond="$cond" />';
|
||||
$target = '<?php if(!empty($cond)): ?><?php $__tpl = new \Rhymix\Framework\Template($this->relative_dirname, "legacy/cond.statement.html", "html"); echo $__tpl->compile(); ?><?php endif; ?>';
|
||||
$target = '<?php if(!empty($cond)): ?><?php $__tpl = new \Rhymix\Framework\Template($this->relative_dirname, "legacy/cond.statement.html", "html"); if ($this->vars): $__tpl->setVars($this->vars); endif; echo $__tpl->compile(); ?><?php endif; ?>';
|
||||
$this->assertEquals($target, $this->_parse($source));
|
||||
|
||||
// Path relative to Rhymix installation directory
|
||||
$source = '<include src="^/modules/foobar/views/baz" when="$cond" />';
|
||||
$target = '<?php if(!empty($cond)): ?><?php $__tpl = new \Rhymix\Framework\Template("modules/foobar/views", "baz", "html"); echo $__tpl->compile(); ?><?php endif; ?>';
|
||||
$target = '<?php if(!empty($cond)): ?><?php $__tpl = new \Rhymix\Framework\Template("modules/foobar/views", "baz", "html"); if ($this->vars): $__tpl->setVars($this->vars); endif; echo $__tpl->compile(); ?><?php endif; ?>';
|
||||
$this->assertEquals($target, $this->_parse($source));
|
||||
|
||||
// Unless
|
||||
$source = '<include src="^/modules/foobar/views/baz" unless="$cond" />';
|
||||
$target = '<?php if(empty($cond)): ?><?php $__tpl = new \Rhymix\Framework\Template("modules/foobar/views", "baz", "html"); echo $__tpl->compile(); ?><?php endif; ?>';
|
||||
$target = '<?php if(empty($cond)): ?><?php $__tpl = new \Rhymix\Framework\Template("modules/foobar/views", "baz", "html"); if ($this->vars): $__tpl->setVars($this->vars); endif; echo $__tpl->compile(); ?><?php endif; ?>';
|
||||
$this->assertEquals($target, $this->_parse($source));
|
||||
|
||||
// With variables
|
||||
$source = '<include src="foobar" vars="$vars" />';
|
||||
$target = '<?php $__tpl = new \Rhymix\Framework\Template($this->relative_dirname, "foobar", "html"); $__tpl->setVars($__Context->vars); echo $__tpl->compile(); ?>';
|
||||
$target = '<?php $__tpl = new \Rhymix\Framework\Template($this->relative_dirname, "foobar", "html"); if ($this->vars): $__tpl->setVars($this->vars); endif; $__tpl->addVars($__Context->vars); echo $__tpl->compile(); ?>';
|
||||
$this->assertEquals($target, $this->_parse($source));
|
||||
|
||||
// With array literal passed as variables
|
||||
$source = '<include src="foobar" vars="[\'foo\' => \'bar\']" />';
|
||||
$target = '<?php $__tpl = new \Rhymix\Framework\Template($this->relative_dirname, "foobar", "html"); $__tpl->setVars([\'foo\' => \'bar\']); echo $__tpl->compile(); ?>';
|
||||
$target = '<?php $__tpl = new \Rhymix\Framework\Template($this->relative_dirname, "foobar", "html"); if ($this->vars): $__tpl->setVars($this->vars); endif; $__tpl->addVars([\'foo\' => \'bar\']); echo $__tpl->compile(); ?>';
|
||||
$this->assertEquals($target, $this->_parse($source));
|
||||
|
||||
// Blade-style @include
|
||||
|
|
@ -1071,7 +1071,7 @@ class TemplateParserV2Test extends \Codeception\Test\Unit
|
|||
|
||||
// cond is OK in includes
|
||||
$source = '<include src="foo.html" cond="$bar" />';
|
||||
$target = '<?php if(!empty($bar)): ?><?php $__tpl = new \Rhymix\Framework\Template($this->relative_dirname, "foo.html", "html"); echo $__tpl->compile(); ?><?php endif; ?>';;
|
||||
$target = '<?php if(!empty($bar)): ?><?php $__tpl = new \Rhymix\Framework\Template($this->relative_dirname, "foo.html", "html"); if ($this->vars): $__tpl->setVars($this->vars); endif; echo $__tpl->compile(); ?><?php endif; ?>';;
|
||||
$this->assertEquals($target, $this->_parse($source));
|
||||
|
||||
// loop
|
||||
|
|
@ -1171,6 +1171,18 @@ class TemplateParserV2Test extends \Codeception\Test\Unit
|
|||
$this->_normalizeWhitespace($expected),
|
||||
$this->_normalizeWhitespace($executed_output)
|
||||
);
|
||||
|
||||
// Variable scope check
|
||||
$tmpl = new \Rhymix\Framework\Template('./tests/_data/template', 'v2varscope.html');
|
||||
$tmpl->disableCache();
|
||||
|
||||
$executed_output = $tmpl->compile();
|
||||
//Rhymix\Framework\Storage::write(\RX_BASEDIR . 'tests/_data/template/v2varscope.executed.html', $executed_output);
|
||||
$expected = file_get_contents(\RX_BASEDIR . 'tests/_data/template/v2varscope.executed.html');
|
||||
$this->assertEquals(
|
||||
$this->_normalizeWhitespace($expected),
|
||||
$this->_normalizeWhitespace($executed_output)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue