/   /   /  OroCommerce B2B for developer: render and compile content blocks programmatically

Note:

For more extensions and themes visit our store

OroCommerce B2B for developer: render and compile content blocks programmatically


The Orocommerce B2B System manage static block by Content Blocks which are the foundation of your site and they can help create a custom layout. In Oro applications, you can modify the existing content blocks, or create your own. More info and how create manually plz follow this link :
https://oroinc.com/b2b-ecommerce/doc/current/user-guide/marketing-content-blocks 

So let’s imagine that you have create one content block , with unique alias ‘home-banner-top’ with any HTML content and restrictions , the question how you can render inside your theme by layout system .

Ok this banner well get showed in the home , the standard route for home page is oro_frontend_root So inside Resources/views/layouts/theme-name add folder  oro_frontend_root .

Note:  theme-name is your theme name or current default theme like blank.

Now inside folder  oro_frontend_root add banner.yml and fill with :

  1.  
  2. layout:
  3.     actions:
  4.         - '@setBlockTheme':
  5.             themes: 'banner.html.twig'
  6.         - '@addTree':
  7.             items:
  8.                 home_banner_top:
  9.                     blockType: content_block
  10.                     prepend: true
  11.                     options:
  12.                         alias:  home-banner-top
  13.             tree:
  14.                 page_content:
  15.                     home_banner_top: ~

What’s means ? Is easy you have added new block which will get related to our content block already created manually from administration ‘home-banner-top’, the type of block is content_block , for more clean and structured YML we use  @addTree and placing our new content block directly inside  page_content . More info about layout system PLZ refer to our course:
https://www.ibnab.com/en/blog/orocommerce/orocommerce-layouts-system-mvsc  or to official docs https://oroinc.com/b2b-ecommerce/doc/current/dev-guide/front-ui/layouts 

And we added a file twig which is our renderer file for this block :

  1.  
  2.         - '@setBlockTheme':
  3.             themes: 'banner.html.twig'

you should add file banner.html.twig inside folder oro_frontend_root and push this code:
  1.  
  2. {% block _home_banner_top_widget %}
  3.   {{  block.vars.contentBlock.content | raw  }}
  4. {% endblock %}

Is easy you’re using the id ‘home_banner_top ‘ of your block to define where you will render this snippet of code and we use {{  block.vars.contentBlock.content  }} to render the content of block.

Note: you can dump all variables of this block by using {{  block.vars.contentBlock  }} in developer mode

Now you have ability to render you HTML but let’s using other case when you  wanna use custom widget inside you content block and render it . Here you should render and compile in same time so your content twig is:

  1.  
  2. {% block _home_banner_top_widget %}
  3.   {{  block.vars.contentBlock.content | render_content }}
  4. {% endblock %}

the twig function render_content will compile and render you content not just simple raw of html . That is all .

Comments

Related Posts

make your store more efficient

Solving problems. With open source technology. Professional results. That’s what makes Ibnab your best choice

IBNAB is a company made of a group of professionals whose work is providing secure open source solutions. Our company strives for reaching magnificent results with each experience and provides professional open source solutions that cover every part of the business process.