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

HemalHerath
  • 113
  • 5

1 Answers1

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; ?>
P_U
  • 808
  • 8
  • 19