I have two phtml which have its own unique block file. I need to pass variable from one phtml to another.


$option_id = 20;

<a href="<?php echo $baseUrl ?>/brandsmodules/index/demo" ></a>


In this page is where I need to get the variable. I don't want to pass variables in URL as parameter due to security concerns.


You can send variable in URL like this to pass variable in another .phtml

In your PHTML 1

<?php $option_id = 20; ?>

<a href="<?php echo $this->getUrl('brandsmodules/index/demo', ['optionId' => $option_id]) ?>

In your PHTML 2

And you will get your optionId in another phtml by using $_GET['optionId'].

Please check and let me know if any question.

|improve this answer|||||
  • @RameshKR does this work for you ? – Aasim Goriya Feb 11 '19 at 11:08

You can try

<?php echo $this->getLayout()->createBlock("Magento\Framework\View\Element\Template")->setKey('dfd')->setTemplate("VendorName_ModuleName::fileName.phtml")->toHtml(); ?>

print your value as

|improve this answer|||||

Inside your block class add two method.

  1. setCustomData

  2. getCustomData

public function setCustomData($data)
    $this->registry->register('custom_data', $data);

public function getCustomData()
    return $this->registry->registry('custom_data');

From first phtml, you set data like:


and from 2nd phtml, you can get data like:

$data = $block->getCustomData();

add $this->registry using DI. For example:

public function __construct(
    \Magento\Framework\Registry $registry
) {
    $this->registry = $registry;
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.