SimpleMagento - Magento tutorials, tips and discussions with simplicity in mind
  • Magento 2
  • Frontend
  • Dev Talk
  • Checkout
  • UI components
  • Online Marketing
  • eCommerce Talk
  • Magento 2
  • Frontend
  • Dev Talk
  • Checkout
  • UI components
  • Online Marketing
  • eCommerce Talk
SimpleMagento - Magento tutorials, tips and discussions with simplicity in mind
No Result
View All Result
Home Frontend

Wrap WYSIWYG content using custom HTML

by freelancer
March 27, 2022
0 0
Share on FacebookShare on Twitter

This article will show you how to set up custom HTML wrapping element around “content” reference (but can be applied to any other reference in any other module). Let’s get started.

First of all, you need to set up the following folder structure:

app/design/frontend/your_namespace/your_theme/Magento_Cms/layout

Inside the layout folder, 3 XML files should be created:

  • catalogsearch_advanced_index.xml – this file represents any module/controller/action route mapping (so in this case, this will be loaded when user visits /catalogsearch/advanced route in browser)
  • cms_page_view.xml – this file will be loaded when any CMS page is opened, (for example, Homepage, About us, Customer Service…)
  • cms_page_view_id_enable-cookies.xml – this file will be loaded only when specific page is opened in browser (in this case, “Enable Cookies” page). As a rule of thumb, the XML file should be named according to the following: cms_page_view_id_{{page-identifier}}.xml

Wrap using route-specific update

Paste the following code within body node in catalogsearch_advanced_index.xml:

<referenceContainer name="content" htmlTag="div" htmlClass="advancedsearch-class" htmlId="advancedsearch-id" />

catalogsearch_advanced

Wrap using page-global update

Paste the following code within body node in cms_page_view.xml:

<referenceContainer name="content" htmlTag="div" htmlClass="wysiwyg-general-class" htmlId="wysiwyg-general-id" />

cms_page_view

Wrap using page-specific update

Paste the following code within body node in cms_page_view_id_enable-cookies.xml:

<referenceContainer name="content" htmlTag="div" htmlClass="wysiwyg-specific-class" htmlId="wysiwyg-specific-id" />

cms_page_view_enable_cookies

Wrap using static-block-specific update

All that needs to be done is create your custom block as a child node of the content reference, as shown in the following snippet:

<block class="Magento\Framework\View\Element\Text\ListText"
       name="block-container-wrapper-name"
       as="block-container-wrapper-id"
       htmlTag="div"
       htmlClass="block-class"
       htmlId="block-id">
    <block class="Magento\Cms\Block\Block" name="cms_test_block">
        <arguments>
            <argument name="block_id" xsi:type="string">test-block</argument>
        </arguments>
    </block>
</block>

cms_static_block

There are two important things to mention about this article:

  1. Be careful if you are setting up custom wrapping on both Page-global and Page-specific level, Page-specific values will override Page-global values
  2. The list of allowed HTML tags is as follows: dd, div, dl, fieldset, main, header, footer, ol, p, section, table, tfoot, ul, nav

There is also a zip file containing the files used here, in case you need some help. If you have any questions, leave a comment below. Happy M2 coding! =)

Tags: Magento 2Frontend
Previous Post

Add category attribute programmatically in Magento 2

Next Post

Magento 2 custom widget

freelancer

freelancer

Related Posts

Magento 2

Implementing payment gateway in Magento 2 – Sample 1

April 22, 2022
Magento 2

Implementing payment gateway in Magento 2 Spinned – Sample 1

April 22, 2022
Magento 2 Payment

Implementing payment gateway in Magento 2 – Sample 1

April 22, 2022
Magento 2

Routing in Magento 2 – Sample 1

April 22, 2022
MVC Magento 2

Magento 2 Controllers – Sample 1

April 22, 2022
Magento 2

How to create a basic module in Magento 2 – Sample 1

April 22, 2022

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Categories

  • Administration
  • Search
  • Configuration
  • Starting Up
  • Extensions
  • News
  • PWA
  • Magento 2 API
  • Programming
  • MVC Magento 2
  • UX/UI Design
  • Shipping Magento 2
  • Database
  • Magento 2 Payment
  • Magento 2
  • Cache
  • Frontend
  • Integration
  • Dev Talk
  • Life at Inchoo
  • Checkout
  • Tips
  • UI components
  • Products
  • Online Marketing
  • Debugging
  • Magento
  • Search Magento 2
  • Upgrading Magento 2
  • Marketing
  • eCommerce Talk
  • Events & Observers
  • Uncategorized

Popular Post

Magento 2

Magento 2 frontend architecture – Sample 1

April 22, 2022
Magento 2

Magento 2 Luma Theme Under The Scope

March 27, 2022
Magento 2 Payment

Implementing payment gateway in Magento 2 – Sample 1

April 22, 2022
Dev Talk

Moving the validation error message, this time globally

March 25, 2022
No Result
View All Result
[vc_row full_width="stretch_row" vc_row_background="" css=".vc_custom_1516082354216{margin-top: 30px !important;padding-top: 22px !important;padding-bottom: 22px !important;}"][vc_column el_class=".footer_center" offset="vc_col-lg-offset-3 vc_col-lg-6"]
[vc_empty_space height="15px"][vc_column_text css=".vc_custom_1516083863519{margin-bottom: 0px !important;}" el_class=".copyright"]Copyright © 2018 JNews. Photography Blog theme by Jegtheme.[/vc_column_text][/vc_column][/vc_row]
No Result
View All Result
  • Magento 2
  • Frontend
  • Dev Talk
  • Checkout
  • UI components
  • Online Marketing
  • eCommerce Talk

© 2023 JNews - Premium WordPress news & magazine theme by Jegtheme.

Welcome Back!

Login to your account below

Forgotten Password?

Retrieve your password

Please enter your username or email address to reset your password.

Log In