2

Let's say that we have this controller :

<?php
class Hello_World_IndexController extends Mage_Core_Controller_Front_Action
{
    public function indexAction()
    {
        echo 'Hello Stackers !' ;
    }
}

Obviously, when triggering this controller, we will have a black message Hello Stackers !, and the controller page is not having/importing any theme (JS, CSS) files even in the default (rwd) Magento theme .

How can we import theme in controllers pages ?

2

You can use the following between the loadLayout and the renderLayout method calls:

// In case of a JS file under the /js folder
$this->getLayout()->getBlock('head')->addJs('my.js');
// In case of a JS file under the /skin folder
$this->getLayout()->getBlock('head')->addItem('skin_js','my.js');
// In case of a CSS file under the /skin folder
$this->getLayout()->getBlock('head')->addItem('skin_css','my.css');

If you only need the default theme you need to update your code with the following:

$this->loadLayout();
$this->renderLayout();
4
  • In that case, the theme's header and footer will be also loaded. What if we want to have an empty page, displaying only Hello Stackers !, and importing the desired JS/CSS files ? EDIT: Just like creating an empty design layout page from CMS-Pages . Dec 21 '16 at 12:42
  • @androniennn see my updated answer. Dec 21 '16 at 13:09
  • Your edit will load the header and footer. What about just echo the message without header and footer. Thank you for your help. Dec 21 '16 at 13:20
  • @androniennn you should never use echo directly in your controller, here's why: magento.stackexchange.com/q/110845/2380 Dec 21 '16 at 13:31

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.