5

I want to use magento header and footer files(custom theme) in my custom php file. How can I get header and footer. php file location is root folder.

I am using this code

require("app/Mage.php");
umask(0);
Mage::app();
ini_set('display_errors', 1);

Mage::getSingleton('core/session', array('name' => 'frontend'));
$session = Mage::getSingleton("customer/session");
$layout = Mage::getSingleton('core/layout');

$headerBlock = $layout->createBlock('page/html_header')->setTemplate('page/html/header.phtml');
$currencyBlock = $layout->createBlock('directory/currency')->setTemplate('currency/currency.phtml');
$headerBlock->setChild('currency_selector', $currencyBlock);
echo $headerBlock = $headerBlock->toHtml();
Amit Bera
  • 77,456
  • 20
  • 123
  • 237
Banna Online
  • 1,059
  • 2
  • 15
  • 33

2 Answers2

4

You need call design area Mage::app()->loadArea('frontend'); at your script.

<?php
include_once "app/Mage.php";
umask(0);
Mage::app()->loadArea('frontend');

$layout = Mage::getSingleton('core/layout');

//load default xml layout handle and generate blocks
$layout->getUpdate()->load('default');
$layout->generateXml()->generateBlocks();

//get the loaded head and header blocks and output
$headBlock = $layout->getBlock('head');
$headerBlock = $layout->getBlock('header');
echo $headBlock->toHtml() . $headerBlock->toHtml();

get footer

$footerBlock = $layout->getBlock('footer');
echo $footerBlock->toHtml();

I hope this will help you.

Denish Vachhani
  • 4,562
  • 3
  • 16
  • 47
3

Pushpendra Singh ,As @denish says,You have missed to call design area as fronted Mage::app()->loadArea('frontend');

Also,You have call the layout then no need to create blocks ('page/html_header','directory/currency') again using page/html_header, etc if it is already called at your design area.Just need to loaded those blocks using getBlock('');

$headerBlock = $layout->getBlock('header'); 

etc.If want to add new add block which does not ,you frontend area section then use createBlock() AND setChild FOR ADD ON EXITING LAYOUT

see more code at https://magento.stackexchange.com/a/5080

Amit Bera
  • 77,456
  • 20
  • 123
  • 237
  • i have call it out Mage::app()->loadArea('frontend');. don't get dummy credit. tell true to all one. this is not for reputation you want. i am giving answer to help not for reputation as you. – Denish Vachhani Jan 02 '16 at 10:29
  • I have just put my point of view on basic of question. – Amit Bera Jan 02 '16 at 10:48
  • Sorry, denish ,i was said the text As @denish says.. . frontend' for Pushpendra Singh not for you. sorry for if it heart you.I do not take u credit .. This credit goes to all for this answered.May be my English make wrong meaning at u.Also remember once thing my friend that i have wrote answered at Mse not for reputation and it my hobby to answer here and i love it.If your thought about me " this is not for reputation you want" Then it totally wrong and i have no reason for u at this case. – Amit Bera Jan 02 '16 at 10:55
  • ok buddy.......!!! – Denish Vachhani Jan 02 '16 at 10:58