1

I created a simple module, now I try to load the block with:

echo $this->getLayout()->createBlock('creation_newproducts/newproducts')->setTemplate('newproducts/newproduct.phtml')->toHtml();

I can echo content from block function however template file is not loaded.

<?xml version="1.0"?>
    <config>
        <modules>
            <Creation_Newproducts>
                <version>1.0.0</version>
            </Creation_Newproducts>
        </modules>
        <global>
            <blocks>
                <creation_newproducts>
                    <class>Creation_Newproducts_Block_Newproducts</class>
                </creation_newproducts>
            </blocks>
        </global>
    </config>

Template file:

app/design/frontend/default/mt_colinus/template/newproducts/newproducts.phtml

enter image description here

How can i echo the template file?

Is this even possible outside controller?

2 Answers 2

2

Make change in your xml file. I have changed following

Creation_Newproducts_Block_Newproducts > Creation_Newproducts_Block

<?xml version="1.0"?>
<config>
    <modules>
        <Creation_Newproducts>
            <version>1.0.0</version>
        </Creation_Newproducts>
    </modules>
    <global>
        <blocks>
            <creation_newproducts>
                <class>Creation_Newproducts_Block</class>
            </creation_newproducts>
        </blocks>
    </global>
</config>

Then use following code in template

echo $this->getLayout()->createBlock('creation_newproducts/newproducts')->setTemplate('newproducts/newproducts.phtml')->toHtml();

Change file path to same as following. You have not put that file on proper place.

app/design/frontend/default/mt_colinus/template/newproducts/newproducts.phtml
11
  • Thanks now the creation_newproducts/newproducts works fine, however template file still not loaded. Apr 8, 2016 at 13:14
  • Can you please put newproduct.phtml file path? Apr 8, 2016 at 13:15
  • Done! see my question! Apr 8, 2016 at 13:18
  • Its not fixed!!! Apr 8, 2016 at 13:21
  • Check updated answer. You file path is wrong. Apr 8, 2016 at 13:24
0

Use below code

echo $this->getLayout()->createBlock('creation_newproducts/newproducts')->setTemplate('newproducts/newproduct.phtml')->toHtml();

creation/newproducts :

creation is your module group name

newproducts is your block class name

4
  • 2
    That will only work if blocks have been declared in the config.xml Apr 8, 2016 at 13:01
  • Hi, I tried that, I copied the code from a module i wrote before only difference is that the block was used in a controller now its in a template file, i believe namespace is also allowed for block declaration and block works, just the template is not loaded, thx! Apr 8, 2016 at 13:04
  • update code check it Apr 8, 2016 at 13:07
  • Fatal error: Call to a member function setTemplate() on boolean in /Applications/XAMPP/xamppfiles/htdocs/vandonzel/vandonzel.nl/public_html/app/design/frontend/default/mt_colinus/template/page/home.phtml on line 187 Apr 8, 2016 at 13:08

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.