变量赋值

可以在模板中进行一些变量的赋值,以便于进行后续计算处理。

node 版本

assign 标签也是用于页面快捷赋值,这个还是用起来比较方便。

  1. public function testBaseUse()
  2. {
  3. $parser = $this->createParser();
  4. $source = <<<'eot'
  5. <assign name="helloWorld" value="say hello to the world" />
  6. <assign name="test.hello" value="hello" />
  7. eot;
  8. $compiled = <<<'eot'
  9. <?php $helloWorld = 'say hello to the world'; ?>
  10. <?php $test->hello = 'hello'; ?>
  11. eot;
  12. $this->assertSame($compiled, $parser->doCompile($source, null, true));
  13. }

node 版本.初始化为 Null 值

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

node 版本.初始化为指定变量

  1. public function testNode2()
  2. {
  3. $parser = $this->createParser();
  4. $source = <<<'eot'
  5. <assign name="test.hello" value="$hello" />
  6. eot;
  7. $compiled = <<<'eot'
  8. <?php $test->hello = $hello; ?>
  9. eot;
  10. $this->assertSame($compiled, $parser->doCompile($source, null, true));
  11. }

node 版本.初始化为函数格式化变量

  1. public function testNode3()
  2. {
  3. $parser = $this->createParser();
  4. $source = <<<'eot'
  5. <assign name="test.hello" value="$hello|md5" />
  6. eot;
  7. $compiled = <<<'eot'
  8. <?php $test->hello = md5($hello); ?>
  9. eot;
  10. $this->assertSame($compiled, $parser->doCompile($source, null, true));
  11. }

node 版本.初始化为函数格式化占位变量

  1. public function testNode4()
  2. {
  3. $parser = $this->createParser();
  4. $source = <<<'eot'
  5. <assign name="test.hello" value="$hello|test=0,1|foo=**" />
  6. eot;
  7. $compiled = <<<'eot'
  8. <?php $test->hello = foo(test($hello, 0,1)); ?>
  9. eot;
  10. $this->assertSame($compiled, $parser->doCompile($source, null, true));
  11. }

JS 风格版本

  1. public function testLet()
  2. {
  3. $parser = $this->createParser();
  4. $source = <<<'eot'
  5. {% let foo = 'foo' %}
  6. {% let hello = hello . 'foo' %}
  7. eot;
  8. $compiled = <<<'eot'
  9. <?php $foo = 'foo'; ?>
  10. <?php $hello = $hello . 'foo'; ?>
  11. eot;
  12. $this->assertSame($compiled, $parser->doCompile($source, null, true));
  13. }

JS 风格版本.初始化值

  1. public function testLet2()
  2. {
  3. $parser = $this->createParser();
  4. $source = <<<'eot'
  5. {% let foo 'foo' %}
  6. eot;
  7. $compiled = <<<'eot'
  8. <?php $foo = 'foo'; ?>
  9. eot;
  10. $this->assertSame($compiled, $parser->doCompile($source, null, true));
  11. }

JS 风格版本.初始化为 Null 值

  1. public function testLet3()
  2. {
  3. $parser = $this->createParser();
  4. $source = <<<'eot'
  5. {% let foo %}
  6. eot;
  7. $compiled = <<<'eot'
  8. <?php $foo = null; ?>
  9. eot;
  10. $this->assertSame($compiled, $parser->doCompile($source, null, true));
  11. }

JS 风格版本.初始化为 Null 值带上等于符

  1. public function testLet4()
  2. {
  3. $parser = $this->createParser();
  4. $source = <<<'eot'
  5. {% let foo = %}
  6. eot;
  7. $compiled = <<<'eot'
  8. <?php $foo = null; ?>
  9. eot;
  10. $this->assertSame($compiled, $parser->doCompile($source, null, true));
  11. }