Add filters for templates (#30)

This commit is contained in:
bzick
2013-08-01 01:05:19 +04:00
parent e68f3dd99a
commit f1d252a3cc
4 changed files with 99 additions and 36 deletions

View File

@@ -47,7 +47,7 @@ class MacrosTest extends TestCase
$this->tpl("macro_recursive.tpl", '{macro factorial(num)}
{if $num}
{$num} {macro.factorial num=$num-1}
{$num} {macro.factorial num=$num-1} {$num}
{/if}
{/macro}
@@ -104,6 +104,6 @@ class MacrosTest extends TestCase
$this->fenom->compile('macro_recursive.tpl');
$this->fenom->flush();
$tpl = $this->fenom->getTemplate('macro_recursive.tpl');
$this->assertSame("10 9 8 7 6 5 4 3 2 1", Modifier::strip($tpl->fetch(array()), true));
$this->assertSame("10 9 8 7 6 5 4 3 2 1 1 2 3 4 5 6 7 8 9 10", Modifier::strip($tpl->fetch(array()), true));
}
}

View File

@@ -114,4 +114,24 @@ class FenomTest extends \Fenom\TestCase
// printf("remove %010b from option %010b, flags %010b\n", $option, $this->fenom->getOptions(), $flags & ~$option);
// $this->assertSame($this->fenom->getOptions(), $flags & ~$option);
}
public function testFilter() {
$punit = $this;
$this->fenom->addPreFilter(function ($src, $tpl) use ($punit) {
$this->assertInstanceOf('Fenom\Template', $tpl);
return "== $src ==";
});
$this->fenom->addPostFilter(function ($code, $tpl) use ($punit) {
$this->assertInstanceOf('Fenom\Template', $tpl);
return "+++ $code +++";
});
$this->fenom->addFilter(function ($text, $tpl) use ($punit) {
$this->assertInstanceOf('Fenom\Template', $tpl);
return "|--- $text ---|";
});
$this->assertSame('+++ |--- == hello ---||--- world == ---| +++', $this->fenom->compileCode('hello {var $user} god {/var} world')->fetch(array()));
}
}