1

I created a custom model Create.php in this path \Vendor\ModuleName\Model\Subscription\Order and a simple function inside it :

<?php
namespace Vendor\ModuleName\Model\Subscription\Order;

class Create
{
    public function __construct();
    public function helloFromModel() {
        return 'Hello';
    }
}

I'm calling this function in my block file \app\code\Vendor\ModuleName\Block

<?php 
namespace Vendor\HelloWorld\Block;
use Magento\Framework\View\Element\Template;

class HelloWorld extends Template
{
public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Vendor\HelloWorld\Model\Subscription\Order\CreateFactory $orderFactory
        ) {

        $this->_orderFactory = $orderFactory;

        parent::__construct($context);
    }

    public function helloFromBlock() {
        $model =  $this->_orderFactory->create();
        return $model->helloFromModel();
    }
}

And I expect to have a Hello in the phtml file : <h1><?php $this->helloFromBlock(); ?></h1>

However, I have nothing printed. What wrong am I doing ?

4

You need to echo a returned value: <?php echo $this->helloFromBlock(); ?>

  • How I did not see this ?! Thank you so much ! – androniennn Dec 14 '16 at 8:17
  • It happens to all of us, you'd gone code blind. – Andy Myers Dec 14 '16 at 9:00

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.