如何在 October CMS 中添加 Twig 测试?
How do I add a Twig test in October CMS?
这在我的 registerMarkupTags 函数中运行良好。
'functions' => [
'isNumeric' => function($value) { return is_numeric($value); }
所以我可以写:
{% if isNumeric(result) %}
我想把它作为一个测试,例如
{% if result is numeric %}
您可以为 twig 添加自定义扩展
来源:https://tutorialmeta.com/october-cms/october-cms-extend-custom-twig-markup
Add below code to your plugin.php
file
// other imports
use Twig\Extension\AbstractExtension as TwigExtension;
// our extension class
// you can declare here inside plugin.php file or in your `plugin\classes` file
// for simplicity we have declared it here
class MyTwigExtension extends TwigExtension {
public function getTests() {
return [
new \Twig\TwigTest('numeric', function ($value) {
return is_numeric($value);
})
];
}
}
class Plugin extends PluginBase {
public function boot() {
Event::listen('cms.page.beforeDisplay',
function ($controller, $url, $page) {
$controller->getTwig()->addExtension(new MyTwigExtension);
}
);
}
// other code
}
Now from your markup
{% if 12 is numeric %}
yes numeric.
{% else %}
not a numeric.
{% endif %}
如有疑问请评论。
这在我的 registerMarkupTags 函数中运行良好。
'functions' => [
'isNumeric' => function($value) { return is_numeric($value); }
所以我可以写:
{% if isNumeric(result) %}
我想把它作为一个测试,例如
{% if result is numeric %}
您可以为 twig 添加自定义扩展
来源:https://tutorialmeta.com/october-cms/october-cms-extend-custom-twig-markup
Add below code to your
plugin.php
file
// other imports
use Twig\Extension\AbstractExtension as TwigExtension;
// our extension class
// you can declare here inside plugin.php file or in your `plugin\classes` file
// for simplicity we have declared it here
class MyTwigExtension extends TwigExtension {
public function getTests() {
return [
new \Twig\TwigTest('numeric', function ($value) {
return is_numeric($value);
})
];
}
}
class Plugin extends PluginBase {
public function boot() {
Event::listen('cms.page.beforeDisplay',
function ($controller, $url, $page) {
$controller->getTwig()->addExtension(new MyTwigExtension);
}
);
}
// other code
}
Now from your markup
{% if 12 is numeric %}
yes numeric.
{% else %}
not a numeric.
{% endif %}
如有疑问请评论。