Skip to content

Instantly share code, notes, and snippets.

@xboston
Created September 17, 2013 10:22
Show Gist options
  • Save xboston/6592532 to your computer and use it in GitHub Desktop.
Save xboston/6592532 to your computer and use it in GitHub Desktop.
Phalcon volt - addFunction vs addFilter
<?php
$compiler = new \Phalcon\Mvc\View\Engine\Volt\Compiler();
$compiler->addFunction("test1", function($resolvedArgs, $exprArgs) {
$value = $exprArgs[0]['expr']['value'];
return str_replace('$', '&euro;', $value);
});
$compiler->addFilter("test2", function($resolvedArgs, $exprArgs) {
return "str_replace('$', '&euro;', $resolvedArgs)";
});
$compiler->addFunction("test3", function($resolvedArgs, $exprArgs) {
$value = $exprArgs[0]['expr']['value'];
return "str_replace('$', '&euro;', '$value')";
});
echo highlight_string($compiler->compileString('{{ test1("100 $") }}'),1);
echo '<br />';
echo highlight_string($compiler->compileString('{{ "100 $"|test2 }}'),1);
echo '<br />';
echo highlight_string($compiler->compileString('{{ test3("100 $") }}'),1);
/**
test1: <?php echo 100 &euro;; ?>
test2: <?php echo str_replace('$', '&euro;', '100 $'); ?>
test3: <?php echo str_replace('$', '&euro;', '100 $'); ?>
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment