1

I want to pass some value in phtml file to another phtml file somewhere else

as an example 1.phtml file like below

<?php

$inputMessage = 'Hello World';
Mage::getSingleton('core/session')->setWelcomeMessage($inputMessage);

?>

and 2.phtml file like below

<?php

$outputMessage = Mage::getSingleton('core/session')->getWelcomeMessage();
echo $this->__($outputMessage);
        
?>

if I use something like this it will give an error like below

Fatal error: Uncaught Error: Class 'Mage' not found in C:\xamppp\htdocs\iconmirror\app\code\Codazon\Slideshow\view\frontend\templates\slideshow.phtml:92 Stack trace: #0 C:\xamppp\htdocs\iconmirror\vendor\magento\framework\View\TemplateEngine\Php.php(59): include() #1 C:\xamppp\htdocs\iconmirror\vendor\magento\framework\View\Element\Template.php(255): Magento\Framework\View\TemplateEngine\Php->render(Object(Codazon\Slideshow\Block\Widget\Slideshow), 'C:/xamppp/htdoc...', Array) #2 C:\xamppp\htdocs\iconmirror\vendor\magento\framework\View\Element\Template.php(279): Magento\Framework\View\Element\Template->fetchView('C:/xamppp/htdoc...')

3 C:\xamppp\htdocs\iconmirror\vendor\magento\framework\View\Element\AbstractBlock.php(659):

Magento\Framework\View\Element\Template->_toHtml() #4 C:\xamppp\htdocs\iconmirror\vendor\magento\framework\View\Layout.php(542): Magento\Framework\View\Element\AbstractBlock->toHtml() #5 C:\xamppp\htdocs\iconmirror\vendor\magento\framework\View\Layout.php(518): Magento\Framework\View\Layout->_renderB in C:\xamppp\htdocs\iconmirror\app\code\Codazon\Slideshow\view\frontend\templates\slideshow.phtml on line 92

please help me with this im new to magento 2

6
  • Are you looking for Magento 1 solution or Magento 2? You have shared code for Magento 1 and tagged question with magento 2.
    – P_U
    Mar 6 '18 at 5:24
  • oh im using magento 2 how can I do that in magento 2 i just found above code in google thats why i try that its not work Mar 6 '18 at 5:28
  • it will not work because code you are using is Magento 1 and implementing it in Magento 2.
    – P_U
    Mar 6 '18 at 5:29
  • can u help me with how to do this in magento 2. it would be really helpful Mar 6 '18 at 5:30
  • please check this link: magento.stackexchange.com/questions/94265/…
    – P_U
    Mar 6 '18 at 5:47
2

You need to write block in your custom module.

Path: Vendor/Module/Block/Customer/Links.php

<?php

namespace Vendor\Module\Block\Customer;

use Magento\Framework\ObjectManagerInterface;

class Links extends \Magento\Framework\View\Element\Template
{
    /**
     *
     * @var \Magento\Customer\Model\Session
     */
    protected $_customerSession;

    /**
     * @param \Magento\Framework\View\Element\Template\Context $context
     * @param \Magento\Customer\Model\Session $customerSession
     */
    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Customer\Model\Session $customerSession
    ) {
        $this->_customerSession = $customerSession;
        parent::__construct($context);
    }

    /**
     * Check customer is logged in or not
     *
     * @return boolean
     */
    public function isLoggedIn()
    {
        return $this->_customerSession->isLoggedIn();
    }

    /**
     * Check customer is logged in or not
     *
     * @return boolean
     */
    public function getCustomerName()
    {
        return $this->_customerSession->getCustomer()->getName();
    }
}

Your Vendor\Module/view/frontend/templates/Customer/links.phtml

<?php if($block->isLoggedIn()): ?>
    <li>
        <div>
            <a href="<?= $this->getUrl('customer/account/logout'); ?>" class="action primary"><?= __('logout'); ?></a>
        </div>
        <div><?= __('Hi, %1', $block->getCustomerName()); ?><a href="<?= $this->getUrl('customer/account/edit'); ?>"><?= __('Edit'); ?></a></div>
    </li>
<?php endif; ?>
3
  • Please accept as answer if it helped you. It will help others too.
    – P_U
    Mar 6 '18 at 6:14
  • so cant i pass value module to module because in your case its passing value same module block to view Mar 6 '18 at 6:38
  • You can write same block code in helper file and can call helper file in any module's template.
    – P_U
    Mar 6 '18 at 6:51

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.