Include 标签

可以使用 include 标签来包含外部的模板文件。

使用完整文件名包含

使用方法如下:

  1. <include file="完整模板文件名" />

这种情况下,模板文件名必须包含后缀。

  1. public function testBaseUse()
  2. {
  3. $parser = $this->createParser();
  4. $source = <<<'eot'
  5. <include file="application/app/ui/theme/default/header.html">
  6. eot;
  7. $compiled = <<<'eot'
  8. <?php $this->display('application/app/ui/theme/default/header', [], '.html', true); ?>
  9. eot;
  10. $this->assertSame($compiled, $parser->doCompile($source, null, true));
  11. }

使用变量定义完整的文件

  1. public function testVar()
  2. {
  3. $parser = $this->createParser();
  4. $source = <<<'eot'
  5. {~$headTpl = \Leevel::themesPath() . '/' . 'header.html'}
  6. <include file="$headTpl">
  7. eot;
  8. $compiled = <<<'eot'
  9. <?php $headTpl = \Leevel::themesPath() . '/' . 'header.html'; ?>
  10. <?php $this->display($headTpl, [], '', true); ?>
  11. eot;
  12. $this->assertSame($compiled, $parser->doCompile($source, null, true));
  13. }

包含当前视图目录下的模板文件

  1. public function testInViewDir()
  2. {
  3. $parser = $this->createParser();
  4. $source = <<<'eot'
  5. <include file="test" />
  6. eot;
  7. $compiled = <<<'eot'
  8. <?php $this->display('test', [], '', true); ?>
  9. eot;
  10. $this->assertSame($compiled, $parser->doCompile($source, null, true));
  11. }

包含其他模块的操作模板

其中模块以目录分隔

  1. public function testOtherModule()
  2. {
  3. $parser = $this->createParser();
  4. $source = <<<'eot'
  5. <include file="public+header" />
  6. eot;
  7. $compiled = <<<'eot'
  8. <?php $this->display('public+header', [], '', true); ?>
  9. eot;
  10. $this->assertSame($compiled, $parser->doCompile($source, null, true));
  11. $source = <<<'eot'
  12. <include file="public/header" />
  13. eot;
  14. $compiled = <<<'eot'
  15. <?php $this->display('public/header', [], '', true); ?>
  16. eot;
  17. $this->assertSame($compiled, $parser->doCompile($source, null, true));
  18. }

函数表达式支持

为了防止 . 被解析为 ->,需要由 () 包裹起来,file 内容区的解析规则遵循 if 标签的 condition 特性。

  1. public function testExpr()
  2. {
  3. $parser = $this->createParser();
  4. // 防止 . 被替换加上 () 包裹起来
  5. $source = <<<'eot'
  6. <include file="($path . '/' . $name)" />
  7. <include file="Template::tpl('header')" />
  8. <include file="tpl('header')" />
  9. <include file="$hello.world('header')" />
  10. eot;
  11. $compiled = <<<'eot'
  12. <?php $this->display(($path . '/' . $name), [], '', true); ?>
  13. <?php $this->display(Template::tpl('header'), [], '', true); ?>
  14. <?php $this->display(tpl('header'), [], '', true); ?>
  15. <?php $this->display($hello->world('header'), [], '', true); ?>
  16. eot;
  17. $this->assertSame($compiled, $parser->doCompile($source, null, true));
  18. }