Call a custom phtml file in ajax layer popup

Call a custom phtml file in ajax layer popup

October 12, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Call a custom phtml file in ajax layer popup

Calling Custom PHTML Files in AJAX Layer Popups

In Magento development, there are times when you need to open a custom .phtml file inside an AJAX layer popup. This can be useful for displaying additional content or forms within a lightbox overlay.

Understanding the Problem

Let’s say you have a custom phtml file called myfile.phtml located in your theme directory:

app/design/frontend/{vendor}/{theme}/Magento_Theme/templates/myfile.phtml

You want to open this file inside an AJAX layer when a button is clicked. The challenge lies in generating the Ajax URL to load the content without refreshing the entire page.

Solving the Issue

To achieve this, you’ll need to use Magento’s built-in Ajax capabilities along with some JavaScript and XML configuration.

Step 1: Create the PHTML File

First, ensure your myfile.phtml contains the content you want to display in the popup:

<!-- app/design/frontend/{vendor}/{theme}/Magento_Theme/templates/myfile.phtml -->
<h2>This is my custom phtml file</h2>
<p>Welcome to your custom content!</p>

Step 2: Create a Controller Action

Next, create an empty controller action in your module:

<!-- app/code/{Vendor}/{Module}/Controller/Index/Display.php -->
namespace {Vendor}\{Module}\Controller\Index;

use Magento\Framework\App\Action;
use Magento\Framework\Controller\ResultFactory;

class Display extends Action
{
    public function execute()
    {
        return $this->resultFactory->create(ResultFactory::TYPE_LAYOUT);
    }
}

Step 3: Create a Layout XML File

Define the layout file for your custom phtml:

<!-- app/code/{Vendor}/{Module}/view/frontend/layout/myfile_display.xml -->
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceContainer name="content">
            <block class="{Vendor}\{Module}\Block\MyfileDisplay" template="Magento_Theme::myfile.phtml"></block>
        </referenceContainer>
    </body>
</page>

Step 4: Create a Block Class

Define the block class to render your phtml file:

<!-- app/code/{Vendor}/{Module}/Block/MyfileDisplay.php -->
namespace {Vendor}\{Module}\Block;

use Magento\Framework\View\Element\Template;
use Magento\Framework\View\Element\Template\Context;

class MyfileDisplay extends Template
{
    public function __construct(Context $context)
    {
        parent::__construct($context);
    }
}

Step 5: Add the JavaScript

Finally, add the required JavaScript code to open the layer:

<!-- app/code/{Vendor}/{Module}/view/frontend/web/js/myfile.js -->
require(['jquery', 'Magento_Ui/js/modal/Modal'], function ($, Modal) {
    var options = {
        type: 'popup',
        responsive: true,
        innerScroll: true
    };

    $('#myfile-button').click(function (event) {
        event.preventDefault();
        var modal = new Modal(options);
        modal.openUrl(window.baseUrl + 'module/index/display');
    });
});

Don’t forget to include this JavaScript file in your layout XML.

Conclusion

By following these steps, you can successfully open a custom .phtml file inside an AJAX layer popup. This approach allows you to display additional content or forms without refreshing the entire page, providing a better user experience.

Remember that proper Magento module development practices and theme inheritance should be followed for a maintainable codebase. For more information on Magento development best practices, feel free to explore our resources at Laravel Company.

Extensions Themes Configuration Errors

Our network