CMS in Magento 2: pages, blocks and variables
Everything you need to manage static content in Magento 2: how to create CMS pages, CMS blocks and the custom variables you can use inside them.
How to Create CMS Pages in Magento 2
Creating CMS (Content Management System) pages in Magento 2 lets you add static content such as about pages, contact pages, company policies, FAQs and more to your online store. Here is a guide on how to create CMS pages in Magento 2:
Step 1: Log in to the Admin Panel
Log in to the Magento 2 admin panel using your login credentials.

Step 2: Go to the Content Menu
Once inside the admin panel, click “Content” in the main menu.
Step 3: Select “Pages”
In the menu, under the “Elements” section, click “Pages”.

Step 4: Add a New CMS Page
Click the “Add New Page” button in the top right corner of the page.

Step 5: Fill In the CMS Page Information
Fill in the fields provided to define the new CMS page. This information can include the page title, the URL key, the page content using the WYSIWYG editor, SEO meta information and other configuration options.
Step 6: Configure the CMS Page Settings (optional)
Magento 2 offers several options for configuring CMS page settings, such as assigning specific layouts, defining custom navigation methods and other advanced settings. Configure these settings if needed.
Step 7: Save the CMS Page
After filling in all the necessary information, click “Save” to save the new CMS page.

Step 8: Check the CMS Page
After saving the CMS page, check that it displays correctly on your online store. You can browse the storefront and look for the page in the menu structure, or use the URL you specified, to make sure it was created correctly.
By following these steps, you should be able to create CMS pages in Magento 2 and easily add static content to your online store. If you run into problems or need more information about a particular step, don’t hesitate to ask for help.
How to Create CMS Blocks in Magento 2
What are CMS blocks?
CMS blocks are simply parts of the layout to be integrated into the e-commerce store. These blocks can consist of HTML code, images, plain text, etc.
How do you create or edit them?
To create or edit them, go to the admin panel, in the Content => (Elements) Blocks menu

At this point we land on the grid and can choose to create a new CMS block by clicking the “Add New Block” button at the top right

Or, from the grid, by clicking the “Select” button => “Edit” in the “Action” column. Clicking “Delete” instead will, of course, delete our block

Of course, these actions can also be performed in bulk, by using the checkboxes on the left of the grid to select the blocks we are interested in, then choosing the action from the “Actions” dropdown menu at the top left of the grid.
At this point we are inside the create/edit block page, where we can enable or disable the block, choose the title we want and set the identifier, which is very important because it will be needed when we want to integrate the block (it is recommended not to use special characters or spaces). Finally, we can enable it only for specific store views, and there is the page builder to add the content of our block.

How do you integrate them into the storefront?
There are several ways to integrate blocks into the storefront, for example by calling the PHP code:
<?php echo $this->getLayout()->createBlock('Magento\Cms\Block\Block')->setBlockId('your_block_identifier')->toHtml(); ?>via XML in the layout of the site theme or of the module that handles the front-end part:
<referenceContainer name="content">
<block class="Magento\Cms\Block\Block" name="block_identifier">
<arguments>
<argument name="block_id" xsi:type="string">block_identifier</argument>
</arguments>
</block>
</referenceContainer>Or through a CMS page (see the article How to Create CMS Pages in Magento 2), by inserting the following code in the page builder’s HTML code:
{{widget type="Magento\Cms\Block\Widget\Block" template="widget/static_block/default.phtml" block_id="1"}}Or again via widget, by associating the CMS block with the widget (see the article How to Create Widgets in Magento 2)
Custom Variables in Magento 2
Custom Variables in Magento 2 are a versatile tool that lets you insert dynamic content into emails, CMS pages and static blocks. In this post, we will explore what custom variables are, how to create them and how to use them effectively.
What Are Custom Variables?
Custom variables in Magento 2 allow you to define dynamic values that can be called anywhere within your store. This is particularly useful for managing information that changes frequently or has to be reused in several places on the site, such as addresses, phone numbers, terms and conditions, etc.
How to Create a Custom Variable
Steps to Create One
- Access the Admin Panel: Log in to the Magento 2 admin panel.
- Navigate to the Custom Variables Section: Go to
Stores>Settings>Configuration. - Add a New Variable: Go to
System>Custom Variables, then clickAdd New Variable. - Define the Variable:
- Variable Name: Enter an identifying name.
- Variable Code: This code will be used to call the variable in templates.
- Variable Value: Enter the content you want to be output.
Creation Example
Suppose we want to create a custom variable for the customer service phone number.
- Variable Name:
Customer Service Phone - Variable Code:
customer_service_phone - Variable Value:
+39 123 456 7890
Using Custom Variables
Custom variables can be used in various contexts, including emails, CMS pages and static blocks.
Including Variables in Emails
To include a custom variable in an email template, use the following syntax:
{{customVar code="customer_service_phone"}}Including Variables in CMS Pages or Static Blocks
To use a custom variable in a CMS page or a static block, use the same syntax:
{{customVar code="customer_service_phone"}}Editing Custom Variables
If you need to update the value of a custom variable, go back to the Custom Variables section and edit the value directly. The change will automatically be reflected everywhere the variable is used.
Best Practices
- Descriptive Names: Use descriptive names for your variables, so you can easily remember their purpose.
- Maintenance: Review and update your variables periodically to make sure they contain correct, up-to-date information.
- Organization: Keep an organized list of the variables you use to avoid duplication and confusion.
Conclusion
Custom variables in Magento 2 are a powerful tool that lets you manage and insert dynamic content with ease. By following the steps described in this guide, you will be able to create, manage and use custom variables to improve the efficiency and consistency of your store.