Search code examples
phpxmlmagentozend-frameworkmage

Stuck on No_Frills_Magento_layout section 2.4


I have been following this guide fairly successfully up to this point here is the code I am working with:

public function complexAction()
        {
            $layout= Mage::getSingleton('core/layout');
            $path = Mage::getModuleDir('', 'Nofrills_Booklayout') . DS . 'page-layouts' . DS . 'complex.xml';
            $xml = simplexml_load_file($path, Mage::getConfig()->getModelClassName('core/layout_element'));
            $layout->setXml($xml);
            $layout->generateBlocks();
            echo $layout->setDirectOutput(true)->getOutput();
        }                   
    }

After loading the corresponding url all I get is a white screen. I var_dumped $path and $Xml and both seem to display the right info. but when I do the same with:

$layout->setDirectOutput(true)->getOutput();

I get:

string(0) ""

Any advice will be helpful.

original code from complex.xml

<layout>    
    <block type="nofrills_booklayout/template" name="root" template="simple-page/2col.phtml" output="toHtml">
        <block type="nofrills_booklayout/template" name="additional_head" template="simple-page/head.phtml" />

        <block type="nofrills_booklayout/template" name="sidebar">
            <action method="setTemplate"><template>simple-page/sidebar.phtml</template></action>
        </block>

        <block type="core/text_list" name="content" />
    </block>    
</layout>

Solution

  • OK, so based on your comments above, it sounds like getOutput returns an empty string, and that the blank white screen isn't a hidden PHP error, it's the result of Magento/PHP not rendering anything for the layout.

    First debugging step: Make sure you're loading the complex.xml you think you are. The following should echo back the contents of the file

    header('Content-Type: text/plain'); //tell the browser not to render HTML
    echo $xml->asXml();
    echo "\nDone\n";
    

    Assuming you see the same XML that's in the file, move on to the next steps.

    The following layout XML (in complex.xml)

    <layout>    
        <block type="nofrills_booklayout/template" name="root" template="simple-page/2col.phtml" output="toHtml">
            <block type="nofrills_booklayout/template" name="additional_head" template="simple-page/head.phtml" />
    
            <block type="nofrills_booklayout/template" name="sidebar">
                <action method="setTemplate"><template>simple-page/sidebar.phtml</template></action>
            </block>
    
            <block type="core/text_list" name="content" />
        </block>    
    </layout>
    

    should

    • Instantiate a nofrills_booklayout/template block
    • Set that block's template to simple-page/2col.phtml
    • Render that template simple-page/2col.phtml

    The simple-page/2col.phtml template contains the following

    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="utf-8"/>
        <title></title>
        <?php echo $this->getChildhtml('additional_head'); ?>
    </head>
    <body>
        <?php echo $this->getChildhtml('sidebar'); ?>
        <section>
        <?php echo $this->getChildhtml('content'); ?>
        </section>
    </body>
    </html>
    

    So, since your code renders an empty string vs. a blank page with an HTML skeleton, that tells me Magento/PHP never get to 2col.phtml. I would

    • Make sure the "Allow Symlinks" is set to yes in the backend. Despite its label, this feature actually means "don't let people render phtml files outside of app/design, and the No Frills modules mess with this a little so you don't need to stress about app/design until you're ready. You've gotten this far in the book, so I don't think that's a problem, but it's worth checking

    • Make sure your current module code has a simple-page/2col.phtml

    • Try creating a template block directly in PHP -- does this work?

    .

    $block = Mage::getSingleton('core/layout')
        ->createBlock('nofrills_booklayout/template')
        ->setTemplate('simple-page/2col.phtml');
    
    var_dump($block->toHtml());
    

    My guess is your simple-page/2col.phtml is missing, or PHP can't load it for some reasons. Hopefully the above gets you on the right track. Feel free to get in touch via support if you need additional assistance.