diff --git a/docs/tags/import.md b/docs/tags/import.md index 8d9488f..2d940d6 100644 --- a/docs/tags/import.md +++ b/docs/tags/import.md @@ -1,6 +1,8 @@ Tag {import} ============ +Import [macro](./macro.md) from another template + ```smarty {import 'page.macros.tpl'} ``` diff --git a/src/Aspect.php b/src/Aspect.php index c2e9d8f..401c983 100644 --- a/src/Aspect.php +++ b/src/Aspect.php @@ -75,10 +75,6 @@ class Aspect { * @var array Templates storage */ protected $_storage = array(); - /** - * @var array template directory - */ - protected $_tpl_path = array(); /** * @var string compile directory */ @@ -115,7 +111,6 @@ class Aspect { "truncate" => 'Aspect\Modifier::truncate', "escape" => 'Aspect\Modifier::escape', "e" => 'Aspect\Modifier::escape', // alias of escape - "url" => 'urlencode', // alias of escape:"url" "unescape" => 'Aspect\Modifier::unescape', "strip" => 'Aspect\Modifier::strip', "default" => 'Aspect\Modifier::defaultValue' @@ -220,6 +215,15 @@ class Aspect { 'type' => self::BLOCK_COMPILER, 'open' => 'Aspect\Compiler::filterOpen', 'close' => 'Aspect\Compiler::filterClose' + ), + 'macro' => array( + 'type' => self::BLOCK_COMPILER, + 'open' => 'Aspect\Compiler::macroOpen', + 'close' => 'Aspect\Compiler::macroClose' + ), + 'import' => array( + 'type' => self::INLINE_COMPILER, + 'parser' => 'Aspect\Compiler::tagImport' ) ); @@ -611,23 +615,18 @@ class Aspect { return $template; } - /** - * Remove all compiled templates. - * - * @param string $scm - * @return int - */ - public function compileAll($scm = null) { - //return FS::rm($this->_compile_dir.'/*'); - } /** * @param string $tpl + * @param bool $cache * @return bool */ - public function clearCompiledTemplate($tpl) { + public function clearCompiledTemplate($tpl, $cache = true) { $file_name = $this->_compile_dir."/".$this->_getHash($tpl); if(file_exists($file_name)) { + if($cache) { + unset($this->_storage[$tpl]); + } return unlink($file_name); } else { return true; @@ -635,10 +634,10 @@ class Aspect { } /** - * @return int + * */ public function clearAllCompiles() { - + \Aspect\FSProvider::clean($this->_compile_dir); } /** diff --git a/src/Aspect/Compiler.php b/src/Aspect/Compiler.php index e387b6f..cd01016 100644 --- a/src/Aspect/Compiler.php +++ b/src/Aspect/Compiler.php @@ -688,9 +688,10 @@ class Compiler { * * @param Tokenizer $tokens * @param Scope $scope + * @throws ImproperUseException * @return string */ - public static function macrosOpen(Tokenizer $tokens, Scope $scope) { + public static function macroOpen(Tokenizer $tokens, Scope $scope) { $tokens->get('.'); $name = $tokens->get(Tokenizer::MACRO_STRING); if($tokens->is('(')) { @@ -706,7 +707,7 @@ class Compiler { } } - public static function macrosClose(Tokenizer $tokens, Scope $scope) { + public static function macroClose(Tokenizer $tokens, Scope $scope) { $scope->tpl->_macros[ $scope["name"] ] = $scope->getContent(); } diff --git a/src/Aspect/FSProvider.php b/src/Aspect/FSProvider.php index 0b5471e..16479c4 100644 --- a/src/Aspect/FSProvider.php +++ b/src/Aspect/FSProvider.php @@ -24,7 +24,7 @@ class FSProvider implements ProviderInterface { foreach($iterator as $file) { /* @var \splFileInfo $file*/ if($file->isFile()) { - if(strpos($file->getBasename(), ",") !== 0) { + if(strpos($file->getBasename(), ".") !== 0) { unlink($file->getRealPath()); } } elseif($file->isDir()) { @@ -46,10 +46,6 @@ class FSProvider implements ProviderInterface { } } - public static function put($path, $content) { - file_put_contents($path, $content); - } - public function __construct($template_dir) { if($_dir = realpath($template_dir)) { $this->_path = $_dir; diff --git a/tests/cases/AspectTest.php b/tests/cases/AspectTest.php index ce26b68..b82ce24 100644 --- a/tests/cases/AspectTest.php +++ b/tests/cases/AspectTest.php @@ -1,37 +1,9 @@ aspect = $aspect = Aspect::factory(ASPECT_RESOURCES.'/template', ASPECT_RESOURCES.'/compile'); - $aspect->setCompileDir(ASPECT_RESOURCES.'/compile'); - $aspect->setForceCompile(false); - $aspect->setCompileCheck(false); - } - - public function tpl($code) { - Misc::put(ASPECT_RESOURCES.'/template/custom.tpl', $code); - } - - public function rmTpl() { - Misc::rm(ASPECT_RESOURCES.'/template/custom.tpl'); - } +class AspectTest extends \Aspect\TestCase { public function testAddRender() { $test = $this; @@ -40,7 +12,8 @@ class AspectTest extends \PHPUnit_Framework_TestCase { $test->assertInstanceOf('Aspect\Render', $tpl); echo "Inline render"; }, array( - "name" => 'render.tpl' + "name" => 'render.tpl', + "scm" => false ))); $this->assertSame("Inline render", $this->aspect->fetch('render.tpl', array())); @@ -51,68 +24,69 @@ class AspectTest extends \PHPUnit_Framework_TestCase { "a" => "a", "b" => "b" ); - + $this->tpl('template1.tpl', 'Template 1 a'); + $this->tpl('template2.tpl', 'Template 2 b'); $this->assertSame("Template 1 a", $this->aspect->fetch('template1.tpl', $a)); $this->assertSame("Template 2 b", $this->aspect->fetch('template2.tpl', $a)); $this->assertInstanceOf('Aspect\Render', $this->aspect->getTemplate('template1.tpl')); $this->assertInstanceOf('Aspect\Render', $this->aspect->getTemplate('template2.tpl')); - $this->assertSame(2, iterator_count(new FilesystemIterator(ASPECT_RESOURCES.'/compile'))); + $this->assertSame(3, iterator_count(new FilesystemIterator(ASPECT_RESOURCES.'/compile'))); } public function testStorage() { - $this->tpl('Custom template'); + $this->tpl('custom.tpl', 'Custom template'); $this->assertSame("Custom template", $this->aspect->fetch('custom.tpl', array())); - $this->rmTpl(); + $this->aspect->clearCompiledTemplate('custom.tpl', false); $this->assertSame("Custom template", $this->aspect->fetch('custom.tpl', array())); - $this->tpl('Custom template 2'); + $this->tpl('custom.tpl', 'Custom template 2'); $this->assertSame("Custom template", $this->aspect->fetch('custom.tpl', array())); } public function testCheckMTime() { - $this->aspect->setCompileCheck(true); - $this->tpl('Custom template'); + $this->aspect->setOptions(Aspect::FORCE_COMPILE); + $this->tpl('custom.tpl', 'Custom template'); $this->assertSame("Custom template", $this->aspect->fetch('custom.tpl', array())); sleep(1); - $this->tpl('Custom template (new)'); + $this->tpl('custom.tpl', 'Custom template (new)'); $this->assertSame("Custom template (new)", $this->aspect->fetch('custom.tpl', array())); } public function testForceCompile() { - $this->aspect->setForceCompile(true); - $this->tpl('Custom template'); + $this->aspect->setOptions(Aspect::FORCE_COMPILE); + $this->tpl('custom.tpl', 'Custom template'); $this->assertSame("Custom template", $this->aspect->fetch('custom.tpl', array())); - $this->tpl('Custom template (new)'); + $this->tpl('custom.tpl', 'Custom template (new)'); $this->assertSame("Custom template (new)", $this->aspect->fetch('custom.tpl', array())); } public function testSetModifier() { $this->aspect->addModifier("mymod", "myMod"); - $this->tpl('Custom modifier {$a|mymod}'); + $this->tpl('custom.tpl', 'Custom modifier {$a|mymod}'); $this->assertSame("Custom modifier (myMod)Custom(/myMod)", $this->aspect->fetch('custom.tpl', array("a" => "Custom"))); } public function testSetFunctions() { - $this->aspect->setForceCompile(true); + $this->aspect->setOptions(Aspect::FORCE_COMPILE); $this->aspect->addFunction("myfunc", "myFunc"); $this->aspect->addBlockFunction("myblockfunc", "myBlockFunc"); - $this->tpl('Custom function {myfunc name="foo"}'); + $this->tpl('custom.tpl', 'Custom function {myfunc name="foo"}'); $this->assertSame("Custom function MyFunc:foo", $this->aspect->fetch('custom.tpl', array())); - $this->tpl('Custom function {myblockfunc name="foo"} this block1 {/myblockfunc}'); + $this->tpl('custom.tpl', 'Custom function {myblockfunc name="foo"} this block1 {/myblockfunc}'); $this->assertSame("Custom function Block:foo:this block1:Block", $this->aspect->fetch('custom.tpl', array())); } public function testSetCompilers() { - $this->aspect->setForceCompile(true); + $this->aspect->setOptions(Aspect::FORCE_COMPILE); $this->aspect->addCompiler("mycompiler", 'myCompiler'); $this->aspect->addBlockCompiler("myblockcompiler", 'myBlockCompilerOpen', 'myBlockCompilerClose', array( 'tag' => 'myBlockCompilerTag' )); - $this->tpl('Custom compiler {mycompiler name="bar"}'); + $this->tpl('custom.tpl', 'Custom compiler {mycompiler name="bar"}'); $this->assertSame("Custom compiler PHP_VERSION: ".PHP_VERSION." (for bar)", $this->aspect->fetch('custom.tpl', array())); - $this->tpl('Custom compiler {myblockcompiler name="bar"} block1 {tag name="baz"} block2 {/myblockcompiler}'); + $this->tpl('custom.tpl', 'Custom compiler {myblockcompiler name="bar"} block1 {tag name="baz"} block2 {/myblockcompiler}'); $this->assertSame("Custom compiler PHP_VERSION: ".PHP_VERSION." (for bar) block1 Tag baz of compiler block2 End of compiler", $this->aspect->fetch('custom.tpl', array())); } }