How to Get the Name of Current Layout of a CMS Page in Magento?

How to Get the Name of Current Layout of a CMS Page in Magento?

September 27, 2026 · Illustrated author avatarBy Stefan Bogdanescu
How to Get the Name of Current Layout of a CMS Page in Magento?

Accessing Current Page Layout Name in Magento CMS Pages

As a developer working with the Magento e-commerce platform, you might find yourself needing to interact with custom fields or layouts within the CMS page setup. This guide will walk you through how to retrieve the name of the current layout assigned to a specific CMS page.

Understanding Magento’s CMS Page Layout Structure

In Magento, each CMS page can have its own unique layout, which is defined under the “Design” tab in the admin area. These layouts are used to control the structure and appearance of your content pages.

When you create or edit a CMS page:

  1. Navigate to Content > Pages.
  2. Click on the desired page or click “Add New Page.”
  3. Under the “Design” section, select the layout from the dropdown menu.

The options available in this dropdown typically include: 1column, 2columns-left, 2columns-right, and 3columns. You can also create custom layouts via XML files located under app/design/frontend/<your_package>/<your_theme>/layout/.

Retrieving the Current Layout Name

To access the current layout name within your PHP code (e.g., when displaying custom fields or processing page-specific logic), you’ll need to leverage Magento’s object manager and layout manager. Here’s a step-by-step approach:

  1. First, ensure you have an instance of the Magento\Cms\Model\Page class available in your script. If you’re working within a CMS block, this should already be provided via the getPage() method of the Magento\Framework\View\Element\Template class.

  2. Use the getLayoutUpdateXml() method to retrieve the layout updates applied to the current page:

$layoutUpdates = $page->getLayoutUpdateXml();
  1. Parse these layout updates using a simple regular expression or string manipulation to extract the name of the main layout file (usually the first part before any additional type and title attributes):
preg_match('/(^[^\s]+)/', $layoutUpdates, $matches);
$mainLayout = $matches[1];
  1. Now you have the name of the main layout in $mainLayout. You can perform an if statement or switch case to handle logic based on this value:
switch ($mainLayout) {
    case '1column':
        // Display custom fields for 1-column layouts
        break;
    case '2columns-left':
        // Handle 2 columns left layout
        break;
    default:
        // Default behavior or error handling
}

Best Practices and Further Reading

When working with layout names in Magento, it’s essential to:

  • Be aware that the available layout options depend on your theme’s configuration.
  • Use the getLayoutUpdateXml() method sparingly, as it retrieves all layout updates (which might include complex XML structures for modules).
  • Consider using Magento’s built-in event observers or custom plugins to handle page-specific logic instead of parsing layout names directly.

For more information on Magento CMS development and best practices, I recommend checking out the following resources:

Conclusion

In this blog post, we explored how to retrieve the name of the current layout assigned to a CMS page in Magento. By understanding the platform’s layout structure and leveraging the object manager, you can now easily identify the active layout within your PHP code.

Remember that this method should be used judiciously and combined with proper error handling and performance considerations, as parsing layout updates directly might not scale well for complex projects.

Happy coding!

Themes Admin CLI

Our network