Custom Variables and Filters in Static Blocks in Magento 2

Custom Variables and Filters in Static Blocks in Magento 2

In the following article I’d like to share my experience of creating custom variables and filters in static blocks, including further configuration.

This topic seems rather interesting to me, and I think it would be very significant for developers. I was confused when I faced a similar task and spent a lot of time trying to find solution, but finally found it in Magento 2 code.

So let’s begin:

At first we have to create module, for example Belvg_CustomStaticBlockFilters. It will consist  of the following files:

  • Block/Block.php — our modified block, that will be used on CMS pages or anywhere else.
  • etc/module.xml — well-known module file.
  • Helper/Data.php — additional code (if required)
  • Model/Template/Filter.php — our magic filter :-)

Block.php code example:

Filters.php code example:

Right after Class and Function description let’s start their implementation:

Example of use in layout.xml:

Example of use in code:

dataFormat filter example of use:

Custom Variables and Filters in Static Blocks in Magento 2

Eventually we end up with the following page.

Custom Variables and Filters in Static Blocks in Magento 1

I do really hope this article will help you with module development in future. Have a nice day, friends!



Post a new comment

top
BelVG Newsletter
Subscribe to our mailing list and get interesting stuff and updates to your email inbox.
Email *