6

I've created a new menu on the backend, called "X" with an item called "Y". When I click on this item, it appears nowhere because I don't know how to display it. Let me show you what I have done.

In the cotroller.php I have put this:

public function indexAction()
    {
 $block = $this->getLayout()
        ->createBlock('core/text', 'example-block')
        ->setText(‘Hello’);
        $this->_addContent($block);

        $this->renderLayout();
    }

For this code, if I click on the menu "Y", I get the output "Hello". Ok, but now I don't know how to call a phtml or something in order to display whatever I want.

I don’t know how to call another cms/block or something like that to watch it there. Using this code snippet:

$this->getlayout()->createBlock('cms/block')->setBlockId('my-cms')->to
Html();

(where my-cms is a cms page that I created before)

the output appears at the top, so it doesn’t work the way I want it to work.

4

Putting your two code snippets together, I get:

public function indexAction()
    {
       $block = $this->getLayout()
       ->createBlock('cms/block')
       ->setBlockId('my-cms');
        $this->_addContent($block);

        $this->renderLayout();
    }

Have you tried this?

|improve this answer|||||
  • omggg, really?! I tried that but with "toHtml()" at the end.. omg.. YES, your code works. Thanks a lot !! EDIT: One more question.. if I want to put PHP code to that CMS (because creating that CMS from cms->static block, didn't let me to put php code).. how can I do that? Really thanks a lot. – inavas Jan 28 '13 at 15:24
  • @inavas To use PHP code, you should create your own block type, which extends from Mage_Core_Block_Template which has an associated template (phtml) file. In the phtml template, you can then call all your blocks methods using $this->... Another possibility is to directly use PHP code in the phtml template by using a block of "core/template", but this is not good style and you should really avoid this. – mpaepper Jan 28 '13 at 15:32
  • I see, I will try the first option then. Thanks a lot ! :) – inavas Jan 28 '13 at 15:39
  • Hi, sorry to disturb you again.. I tried to create a new template and I failed.. so I tried the "easy way" that was creating a new .phtml on /template/customphp/mycode.phtml and call it in different ways but it doesnt appear.. I try it with: $this->getLayout()->createBlock('core/template')->setTemplate('customphp/mycode.phtml') but it appears nothing. So I try calling a cms/block that calls "mycode.phtml": {{block type="core/template" template="customphp/mycode.phtml"}} but nothign... what I am doing wrongggg? Sorry to disturb, but it's very important for me knowing this. THANKS!! – inavas Jan 29 '13 at 15:38
  • @inavas Did you insert the template to the correct design folder? What is the path before /template? Also, did you still use $this->_addContent($block); ? Besides, your code looks correct and should work. – mpaepper Jan 29 '13 at 16:15

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.