october cms extend custom twig markup
OctoberCMS has a rich feature set and it's really easy to extend It. But sometimes some features are not directly extendable because it is not possible to satisfy 100% of need.
So to overcome that need we need to find some smart workarounds :)
For Ex: OctoberCMS allow use to add custom twig functions
and twig filter
for markup. but unfortunately, it is not allowing us to add twig test
.
But can easily extend this feature and add our own
twig test
so we can use them inconditions
.
for this, you need to listen for cms.page.beforeDisplay
event where we can extend twig environment
and add our custom test
.
// other imports
use TwigExtensionAbstractExtension as TwigExtension;
// our extension class
// you can declare here inside plugin.php file or in your `pluginclasses` file
// for simplicity we have declared it here
class MyTwigExtension extends TwigExtension {
public function getTests() {
return [
new TwigTwigTest('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
}
Once you add extension code you can start using
isNumeric
in the page markup area
{% if 12 is numeric %}
yes numeric.
{% else %}
not a numeric.
{% endif %}
please share your thoughts in the comments, and stay tuned for new OctoberCMS blogs.
october cms extend custom twig markup
Related Questions
- → OctoberCMS Backend Loging Hash Error
- → "failed to open stream" error when executing "migrate:make"
- → OctoberCMS - How to make collapsible list default to active only on non-mobile
- → Create plugin that makes objects from model in back-end
- → October CMS Plugin Routes.php not registering
- → OctoberCMS Migrate Table
- → How to install console for plugin development in October CMS
- → OctoberCMS Rain User plugin not working or redirecting
- → October CMS Custom Mail Layout
- → October CMS - How to correctly route
- → October CMS create a multi select Form field
- → How to update data attribute on Ajax complete
- → October CMS - Conditionally Load a Different Page
- → How to disable assets combining on development in OctoberCMS