Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

Page 1 of 2
add a new block to page.xml
 
shin_mangaman
Member
 
Total Posts:  55
Joined:  2008-09-17
 

Hi everyone,

I’m a beginner and I’m trying to learn the xml markup. Sorry if this has been answered anywhere else, I have looked and I don’t think it has. I decided to imitate the demo multi-store to learn so I wanted to insert a new block over the header. I created a simple template named <i>overheader.phtml</i> in <i>template/page/html</i> folder:

<class="demo-notice">This is a demo storeAny orders placed through this store will not be honored or fulfilled.</p>
I added a new block named <i>overheader</i> to <i>page.xml</i> like this:
...
<
block type="page/html_overhead" name="overheader" as="overheader" template="page/html/overheader.phtml"/>
<
block type="page/html_header" name="header" as="header">
        <
block type="page/template_links" name="top.links" as="topLinks"/>
        <
block type="page/switch" name="store_language" as="store_language" template="page/switch/languages.phtml"/>
        <
block type="core/text_list" name="top.menu" as="topMenu"/>
</
block>
...
Finally I modified <i>1column.phtml</i>:
...
<
body <?php echo $this->getBodyClass()?'class="'.$this->getBodyClass().'"':'' ?>>
<
div class="overheader">
    
<?php echo $this->getChildHtml('overheader'?>
</div>
<
div class="wrapper">
        <!-- 
[start] header -->
...
The problem is: the new template isn’t displayed. I tried many values for <i>type</i> attribute but nothing changed. I read that I can use static blocks but from what I understand they contain only html code so I can’t include other templates inside of them. Moreover why layouts use xml tag if we can’t change them in this way? So what’s wrong with this code? Thank you very much smile
 
Magento Community Magento Community
Magento Community
Magento Community
 
hydroxide
Jr. Member
 
Total Posts:  1
Joined:  2008-12-29
 

I’m also a beginner so I may not be 100% accurate in my response.

When you created a new block “overheader” in page.xml, you set its type as “page/html_overhead”.  Since you want to use this type, you need to manually create this block type.  These block types can be created by opening up app/code/core/Mage/.  Since the top level of your block type is “page”, navigate to the “page” folder.  You will see a subfolder called “Block”. This is where all the block types “page” are stored/created.

The next element of the block type is “html”, so go to the “html” folder under the “page/Block” folder.  You will see a list of php files (e.g. Header.php, Footer.php, etc.).  These are the php files that the other block types refer to - e.g. page/html_header, page/html_footer, etc.

Create a new file called “overhead.php”.  For just a simple block, inserting the following into that file should suffice:

<?php

class Mage_Page_Block_Html_Overhead extends Mage_Core_Block_Template
{
    
function __construct()
    
{
        parent
::__construct();
        
$this->setTemplate('page/html/overheader.phtml');
    
}

}

?>

Then you can refer to this in your phtml file.  Also, make sure that the relevant css file (skin/frontend/default/default/styles.css I believe) sets the correct dimensions for the block.  Something like:

.overhead {
height:100px;
width:100%;
position:absolute;
top:0;
}

Hopefully this helps.

Cheers,

Nick

shin_mangaman - 29 December 2008 02:10 AM

Hi everyone,

I’m a beginner and I’m trying to learn the xml markup. Sorry if this has been answered anywhere else, I have looked and I don’t think it has. I decided to imitate the demo multi-store to learn so I wanted to insert a new block over the header. I created a simple template named <i>overheader.phtml</i> in <i>template/page/html</i> folder:

<class="demo-notice">This is a demo storeAny orders placed through this store will not be honored or fulfilled.</p>
I added a new block named <i>overheader</i> to <i>page.xml</i> like this:
...
<
block type="page/html_overhead" name="overheader" as="overheader" template="page/html/overheader.phtml"/>
<
block type="page/html_header" name="header" as="header">
        <
block type="page/template_links" name="top.links" as="topLinks"/>
        <
block type="page/switch" name="store_language" as="store_language" template="page/switch/languages.phtml"/>
        <
block type="core/text_list" name="top.menu" as="topMenu"/>
</
block>
...
Finally I modified <i>1column.phtml</i>:
...
<
body <?php echo $this->getBodyClass()?'class="'.$this->getBodyClass().'"':'' ?>>
<
div class="overheader">
    
<?php echo $this->getChildHtml('overheader'?>
</div>
<
div class="wrapper">
        <!-- 
[start] header -->
...
The problem is: the new template isn’t displayed. I tried many values for <i>type</i> attribute but nothing changed. I read that I can use static blocks but from what I understand they contain only html code so I can’t include other templates inside of them. Moreover why layouts use xml tag if we can’t change them in this way? So what’s wrong with this code? Thank you very much smile
 
Magento Community Magento Community
Magento Community
Magento Community
 
shin_mangaman
Member
 
Total Posts:  55
Joined:  2008-09-17
 

Sorry for the late hydroxide, I was on holiday smile Thank you very much! Your information are precious! I tried your solution and it seems to work; I have only to learn how to write code for new blocks. Thank you again! Bye

 
Magento Community Magento Community
Magento Community
Magento Community
 
Dorgo
Sr. Member
 
Total Posts:  127
Joined:  2008-12-16
 

Thanks guys, another beginner here. This thread has helped me understand things a bit better.

One question though. Is adding a new file to app/code/core/Mage/html/ the correct way to approach this? I was under the impression that i should stick to modifying things under app\design\frontend\default\{myDesign} and skin\frontend\default\{myDesign} only. Easing upgrades, transfers etc.

I guess the new file in app/code/core/Mage/html/ wont be overwritten in an update… but its another thing to remember when moving or reinstalling. Is there another correct way to add a simple block?

 
Magento Community Magento Community
Magento Community
Magento Community
 
chrisdarl
Member
 
Total Posts:  45
Joined:  2008-07-17
 

As far as I know, it’s best not to edit any core files, changes should be made in the local folder so nothing will be effected by updates

/app/code/local/Mage/…

Hope this helps!

 
Magento Community Magento Community
Magento Community
Magento Community
 
unclek
Jr. Member
 
Total Posts:  7
Joined:  2008-08-18
Canada
 

I’ve tried this both on my active theme, and in the default core files and still can’t get the block to show up. I’ve refreshed the cache as well. Is there anything that has changed in this process since 1.3.1, or something else I may have missed?

 
Magento Community Magento Community
Magento Community
Magento Community
 
BarneyHall
Jr. Member
 
Total Posts:  26
Joined:  2009-03-09
 

Hi there.

Similarly, this is all very new to me but thought I’d post as I’m trying to do the same thing, but also worry about modifying files outside the two threads suggested in the design guide.

I’m finding I can do this by adding HTML to the head.phtml document, then track down the CSS doc to remove styling on the surrounding classes (borders/margins that sort fo thing).

head.phtml is loaded into the page whatever container phtml page is loaded, i.e. 3column.phtml, 1column.phtml etc.

I’m only inserting a <p></p> tag at present, but I hope to replace this with a <div></div> and style it using my own classes, either added to the boxes.css or inserted within a seperate custom css for my client’s needs.

Very early to say, but I believe we must be able to create your own themes without having to go into the “code” folders… just wouldn’t make sense to me with all the advice regarding “don’t as you won’t be able to upgrade”.

Just my few pennies worth…

 
Magento Community Magento Community
Magento Community
Magento Community
 
BarneyHall
Jr. Member
 
Total Posts:  26
Joined:  2009-03-09
 

A quick follow up…

Hasn’t gone as smoothly as I’d hoped.

1) Trying to insert new DIVs, with your own styling (particularly layout) seems to conflict with pre-existing block styling.

2) Strangley, although I can manipulate background images through CSS I can’t seem to bring in my own logo. Including anything other than hardcoded logo, i.e. <img src="<?php echo $this->getLogoSrc() ?>” alt="<?php echo $this->getLogoAlt() ?>” /> just seems to get ignored.

It looks like even creating a different logo file involves manipulating “app\code\core\mage\page\etc\config.xml” so bang goes working in only the two recommended interface/theme threads (as per Dorgo’s post).

I really hope I’m just missing something simple here…

 
Magento Community Magento Community
Magento Community
Magento Community
 
BarneyHall
Jr. Member
 
Total Posts:  26
Joined:  2009-03-09
 

Hello again. I finally managed to add my own custom block (within the layout page.xml) to load html from a custom callout template.

It’s a lot simpler than it seems (everything is so confusing to start with). Note: I was going wrong last week - so please ignore all that!

1) I created a new template within: design\frontend\default\my_theme\template\callouts\my_callout.phtml

Within my_callout.phtml I created the HTML I wanted to display, i.e. anything you want (for example, <h1>Hello</h1>).

2) Then I modified the layout file page.xml to include a reference to this template by including the following line:

<block type="core/template" name="right.custom.callout" as="my_callout" template="callouts/my_callout.phtml"/>

3) Then in the page layout file (for example 1column.phtml) I added a “getChildHTML” method like this:

<?php echo $this->getChildHtml('my_callout') ?>

And voila it works! The custom HTML within my_callout.phtml is now displayed on my page…

 
Magento Community Magento Community
Magento Community
Magento Community
 
ahmedwali
Jr. Member
 
Total Posts:  2
Joined:  2009-03-19
 

hi there i do all step but not work
my step :

i create new file Test.php in app/code/local/Mage/Page/Block/Html/
with content

<?php
class Mage_Page_Block_Html_Test extends Mage_Core_Block_Template
{
    
function __construct()
    
{
        parent
::__construct();
        
$this->setTemplate('page/template/html/test.phtml');
    
}

}
?>
create file test.phtml in app/design/frontend/default/mytemplate_dir/template/page/html/
go to app/design/frontend/default/mytemplate_dir/layout/ and add new line before
<block type="page/html_breadcrumbs" name="breadcrumbs" as="breadcrumbs"/>
and add code
<block type="page/html_test" name="test" as="test" />
and when add
<?php echo $this->getChildHtml('test'?>
in footer.phtml it’s not display can u help me please

 
Magento Community Magento Community
Magento Community
Magento Community
 
seudo
Guru
 
Avatar
Total Posts:  344
Joined:  2008-04-21
 

There is a way to create the block with out extending a magento class.

First off you need to determine what type the template will be.

<block type="core/template" name="new_block_name" as="new_block_name" template="template_location"/>
//here is an example pulled from catalog.xml
<block type="core/template" before="cart_sidebar" name="catalog.compare.sidebar" template="catalog/product/compare/sidebar.phtml"/>
Now if it is going above the header like the posts above mention you must look in magento_install_dir/app/design/frontend/default/your_theme/template/page/

and add

<?php echo $this->getChildHtml('new_block_name'?>
above
<?php echo $this->getChildHtml('header'?>

in all the column .phtml files.

 
Magento Community Magento Community
Magento Community
Magento Community
 
nkos
Jr. Member
 
Total Posts:  8
Joined:  2010-03-07
France
 

Hi
I’m also a beginner, I followed the steps of @hydroxide but this is not working for me and I really dont understand why !!!

Here my steps:

In design/ frontend / layout / page.xml

under

<block type="page/html_breadcrumbs" name="breadcrumbs" as="breadcrumbs"/>

<block type="page/html_authredirect" name="authredirect" as="authredirect" />

In app/ code/ Mage/ Page/ Block/ Html/ Authredirect.php

<?php
class Mage_Page_Block_Html_Authredirect extends Mage_Core_Block_Template
{
    
function __construct()
    
{
        parent
::__construct();
            
        
$this->setTemplate('page/html/authredirect.phtml');
    
}

}
?>

In design/ frontend / template/ page/ html/ authredirect.phtml

<h1>Hello World !</h1>

In design/ frontend / template/ page/ 3columns.phtml
under this line

<?php echo $this->getChildHtml('breadcrumbs'?>

i added:

<?php echo $this->getChildHtml('authredirect'?>

Can you please help me to understand why this is not working ??
I tried to follow exactly the right method but nothing !!

If you copy my codes in your own, do you have the same problem ??

Thank you !

 
Magento Community Magento Community
Magento Community
Magento Community
 
KjoeLjan
Jr. Member
 
Total Posts:  5
Joined:  2010-02-26
 

I got problems with custom blocks in 1.3 as well… It worked perfectly when I added my custom block in the Mage folder, but when I tried to keep the custom blocks out of the core stuff, it didn’t work anymore.

In addition: I think this is a nice place for me to say that Magento definitely needs documentation. The fact that there are so many people asking the same question over and over again, should say enough.

KL.

EDIT:
My problem is solved by fixing a typo…

 
Magento Community Magento Community
Magento Community
Magento Community
 
nkos
Jr. Member
 
Total Posts:  8
Joined:  2010-03-07
France
 

Actually my codes works well but what I didn’t know was that you have to refresh the Magento’s cache in the admin !!!

Now it’s working smile

 
Magento Community Magento Community
Magento Community
Magento Community
 
Razmatazed
Jr. Member
 
Total Posts:  27
Joined:  2009-09-29
Sun Prairie Wisconsin
 

Hello,
I am trying to use these steps to create a new editable box on the right hand side importing a static block.

Here is what I did so far:

1. Created a new static block called support

2. Created a new .phtml file called support.phtml which called the support block:

<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('support')->toHtml() ?>

3. Edited the catalog.xml file

<reference name="right">
            <
block type="core/template" name="support" as="support" template="callouts/support.phtml"/>
            <
block type="catalog/product_compare_sidebar" before="cart_sidebar" name="catalog.compare.sidebar" template="catalog/product/compare/sidebar.phtml"/>
            <
block type="core/template" name="right.permanent.callout" template="callouts/right_col.phtml">
                <
action method="setImgSrc"><src>images/media/col_right_callout.jpg</src></action>
                <
action method="setImgAlt" translate="alt" module="catalog"><alt>Keep your eyes open for our special Back to School items and save A LOT!</alt></action>
            </
block>

by adding

<block type="core/template" name="support" as="support" template="callouts/support.phtml"/>

Here is where I am confused....

1. It works however my block is showing up underneath the add to cart and compare blocks. I need the box to be first on the right hand side

2. In the code above I see a call out for the compare sidebar, however I do not see a call out for the shopping cart. How is this being called into the page???

Any suggestions would be greatly appreciated!

Jessie

 
Magento Community Magento Community
Magento Community
Magento Community
 
benblee
Jr. Member
 
Total Posts:  13
Joined:  2009-01-15
 

You can add “before” and “after” sets to these blocks.  The shopping cart block I think goes by “currency”, so trying something like this…

<block type="core/template" before="currency" name="support" as="support" template="callouts/support.phtml"/>

I know this post is a little old.  Just figured I’d offer a reply just in case.

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top
Page 1 of 2