Magento: include custom block in view.phtml - magento

I have created a block accrording to this tutorial: http://www.gravitywell.co.uk/blog/post/how-to-creating-your-own-custom-block-in-magento . Now I need to use my manufacturerblock.phtml on my pruduct view page (just somewhere closed to product description text).
In local.xml (under <reference name="header">) I added: (edit: I tried also under <reference name="product.info"> in <catalog_product_view>
<block type="aimitmanufacturerblock/manufacturerblock" name="manufacturerblock" as="manufacturerblock" template="aimitmanufacturerblock/manufacturerblock.phtml" />
and I used <?php echo $this->getChildHtml('manufacturerblock')?> in my view.phtml.
But it seems the phtml is not included.

Edit: Apologies, I should have followed the link to the tutorial you gave first, I see it's all about creating a new block type! However, I'll leave my answer for the moment - why do you need to create a new block type at all, there are loads of built in block types?. This answer from Clockworkgeek could be useful to you too;
Create a new Block in Magento
Original answer;
The issue is your block type - the block type isn't like a variable that you define yourself, there are specific built-in Magento block types which are used to instantiate the appropriate classes. This SO thread will be useful to you for further explanation;
Understanding Magento Block and Block Type
For another great explanation on block types read this one too;
What Block Type for Left Column in Magento Theme?

If you want to use the block in catalog/product/view.phtml, you need to reference product.info in the catalog_product_view handle.
And I can just assume that the module is activated, the class alias is declared, the class is created and the cache is flushed

Related

Set template in layout handle for Adminhtml

In the backend of my Magento Webshop I want to set a template for the blocks of System->Config. Therefore I use the layout handle of the Mage_Adminhtml_System_ConfigController which is adminhtml_system_config_index I think. The problem is, nothing happens. Even if want to remove blocks, nothing happens:
<adminhtml_system_config_index>
<reference name="left">
<action method="setTemplate">
<template>myModule/template.phtml</template>
</action>
</reference>
</adminhtml_system_config_index>
OR removing block:
<adminhtml_system_config_index>
<remove name="left" />
</adminhtml_system_config_index>
What am I doing wrong? I even tried system_config_index as layout handle.
You're doing three possible things wrong here.
First Tip: Are you editing the right XML file?
A lot of common layout problems come from the XML file you're editing not being the XML file that's loaded by the system. Switch on developer mode in your site, set display_errors to 1, and then deliberately introduce a non well formed error into your XML files and try to load the page.
<!-- note the trailing greater than sign at the end -->
<adminhtml_system_config_index>
...
</adminhtml_system_config_index> >
If the page loads without error, then you're not loading the XML file you think you are.
Second Tip: Are you using the correct layout handle?
I'm not sure you are. Despite the fact the default system configuration page uses the index action, this action actually forwards the page to the edit action.
#File: app/code/core/Mage/Adminhtml/controllers/System/ConfigController.php
public function indexAction()
{
$this->_forward('edit');
}
Forwarding means there's an internal redispatch, but no http redirect. That means your actual handle is
`adminhtml_system_config_edit`
This is why I use (disclaimer: and built, and sell) Commerce Bug. The Layout tab always displays the current handles, and I can avoid making assumptions that send me down the wrong path.
Third Tip: Are you confusing the block named left with the left hand column?
In general, Magento's container blocks (left, content, right, etc.) are not template blocks. Instead they're container blocks (text/list blocks to be precise) that hold and multiple template blocks.
This is another place I use Commerce Bug, specifically the new Graphviz feature. This will show you the block structure for any particular page. In a factory default system, you'd see something like this for the System Configuration page
As you can see, the left block has no template, so changing its template will have no effect. The block you probably want is left.child1.
Fourth Tip: Is the block you're trying to modify added by the layout XML.
Without getting too deeply into layout rendering (which would take a book), there are some block which are added after the layout generates all its blocks. If this is the case, your block will not be available in layout XML files.
If you look at the editAction method, you can see that the tabs block is added after loadLayout is called (look for adminhtml/system_config_tabs). This means it won't be available in layout xml files.
public function editAction()
{
//...
//the `loadLayout` method call creates blocks based on layout XML files
$this->loadLayout();
$this->_setActiveMenu('system/config');
$this->getLayout()->getBlock('menu')->setAdditionalCacheKeyInfo(array($current));
$this->_addBreadcrumb(Mage::helper('adminhtml')->__('System'), Mage::helper('adminhtml')->__('System'),
$this->getUrl('*/system'));
//this line add the `child1` block to the layout. (child one is the
//name chosen since the block has no explicit name
$this->getLayout()->getBlock('left')
->append($this->getLayout()->createBlock('adminhtml/system_config_tabs')->initTabs());
//...
This means you can't modify the template of this block from a layout XML file. You'll need to use a custom module that listens for an appropriate event and then makes your changes via PHP. I'd try the event controller_action_layout_render_before or controller_action_layout_render_before_adminhtml_system_config_edit with PHP code that looks something like
$block = Mage::getSingleton('core/layout')->getBlock('child1');
if($block)
{
$block->setTemplate('foo.phtml');
}

How do I place Magento theme blocks?

I am using two plugins, easycatalogimg and bannerslider.
For sanity purpose, I am using the default Magento theme, I am just trying to learn the template engine.
When I enable easycatalogimg, it appears above the bannerslider on the homepage. I am using the following code to call bannerslider.phtml, within CMS -> Homepage.
{{block type='bannerslider/bannerslider' template='bannerslider/bannerslider.phtml'}}
I would like to call the bannerslider, then easycatalogimg. Problem is, I cannot determine what block type easycatalogimg is. If I could, I would just turn off the homepage display, and then place the block beneath that. When I turn on the setting to make easycatalogimg appear on the homepage, it inserts itself above the rest of the page content.
Are there files I can open to determine what block type easycatalogimg is? Then I could do this code, right under the bannerslider.
{{block type='foo/bar' template='default/default/easycatalogimg.home.phtml'}}
Right now, the easycatalogimg appears on the homepage.
IN AN IDEAL WORLD, the solution here would be that I would edit a page like...
frontend/base/default/template/cms/content_heading.phtml
...within that page, call out the bannerslider.phtml and easycatalogimg/homepage.phtml.
Open the template file of the block you want to know the type. If it's a default Magento template, it will generally have the Block_Class name at the top. If it doesn't, run:
<?php echo get_class($this) ?>
That will give you the Block_Class name which then gets translated into Magento's calling convention, such as:
<?php $block = $this->getLayout()->getBlock('core/template') ?>
Where 'core/template' gets translated into Mage_Core_Block_Template

Magento get which layout being used on phtml files

Is there a way I could get which layout being used on a certain phtml files?
Here in my case, I want to check what layout being used on catalog/list.phtml, I used that information to make conditional "if" on the product image grid size.
I've tried to google it out. But all the result is just explaining about xml layout things. The closest clue I got is this thread
Magento get layout for given page
which stated the use of this snippet
$left_block = $this->loadLayout()->getLayout()->getBlock('left');
but when I tried it on the phtml files, I got an exception error
UPDATE
joe's answer below has give me some more clue, the exception gone. But the behavior doesn't really what I need. That snippet of code seems to be just check whether the specified block is defined on the XML. What I really need is whether that block exist on a certain page.
In my case, I need to check what layout being used on catalog/product/list.phtml. if it's 3 columns, I'm gonna make the image resized smaller. If it 1 column, I'll make it bigger.
Is there any way I could do that??
If I read the question correctly, then try:
$this->getLayout()->getBlock('root')->getTemplate();
Remove loadLayout():
$left_block = $this->getLayout()->getBlock('left');
By the time you are in the PHTML file, the layout is already loaded.
In PHTML files, $this refers to the Mage_Core_Block_Template class (or a class that extends it). This class doesn't have a loadLayout() method defined, which is why you get an exception; instead, loadLayout() is part of Mage_Core_Controller_Varien_Action.

Magento - conditionals in layout files

Is it possible in Magento to conditionally add blocks into a layout xml file?
Im thinking along the lines of having an admin config option checkbox - if checked then a block needs to be added to the page and vice versa if not checked.
I could think of a way of doing this via code but not the actualy layout file system itself.
The ifconfig paramater can be used to conditionally call an action method
<action method="someBlockMethod" ifconfig="path/to/config"><param1>value</param></action>
The path/to/config path is passed to Mage::getStoreConfigFlag() to return a boolean.
I'd try using this in combination with the insert method
<action method="insert" ifconfig="path/to/config"><param>block_name</param></action>
The block with the name or alias of block_name will need to be already inserted into the layout object by other PHP or XML, so you may need to take additional steps to unset it from its original blocks after inserting it into your new block.
you can try this (i haven't tried it myself):
<action ifconfig='your/extension/active'

Magento layout related basic Questions?

Here is what I think about Magento (plz correct me if I am wrong )
1)Each module has its own layout.xml stored in /interface/theme/layouts/ folder.
2)Magento loads all these layouts for current theme and creates a big xml file.
Now what I am confused at .
a)If magento loads /default/default/ (interface & theme) then why all the templates & layouts are inside base/default/ ??
b)what if I create my module name “page” inside my namespace “Jason” i.e Jason_Page , now what will happen to blocks in layout files which are named
c)Since all the layouts are loaded and merged into one big xml file , then what happen to all those reference blocks which have same name attribute and are inside “Default” handle tag ?
e.g
d)what is Local.xml layout for and its use ??
e)wats the relation ship between a module name foo , and its layout name foo.xml ?
What will happen to layout.xml if two modules with same name exist in diff namespace ?
Thanks in advance .
1) Each module can choose to define layout files that go into the /interface/theme/layouts/ folder. You can accomplish this by specifying the layouts in your module's config.xml file like this:
<frontend>
<layout>
<updates>
<jason_page>
<file>jasonpage.xml</file>
</jason_page>
</updates>
</layout>
</frontend>
2) Yes.
a) Magento's interface is built using descendency. Templates and layouts are first taken from your chosen interface and theme, and if not found there, will be taken from base. This allows you to define only those things that change from the base when creating a new them.
b) If you create a new module with blocks, you will specify the classpath for those blocks in your config.xml:
<global>
<blocks>
<jason_page>
<class>Jason_Page_Block</class>
</jason_page>
</blocks>
</global>
This will define the tag jason_page to refer to your blocks. Then, when defining a layout, you will reference your blocks as:
<block type='jason_page/blockname' as='blockname' name='blockname' />
And your blocks will be named as: Jason_Page_Block_Blockname in /app/code/local/Jason/Page/Block/Blockname.php
c) Each time you use a <reference> tag and add some child blocks, they are added to the large tree as you say. Make sure to use different name/as tags for the blocks, and you'll be fine. If the name is identical, you will probably cause errors.
d) local.xml (in app/etc) defines some configuration parameters for your site, such as database connection information and encryption keys. it doesn't have to do with layouts.
d part 2) Since you define your layout files, including their names, it's up to you to not cause collisions. Choosing a module name more unique than Page would go far here. If you do have a module with the same name as a default module (e.g. Page), prefix the file with your namespace when declaring it in your config.xml file.
Hope that helps!
Thanks,
Joe
Just to add to Joe's answer, you can have interface/theme/layout/local.xml file, and it will be loaded after all other core and module layout XML files.
Use this file for local theme customizations.
I just recently wrote a post about how to use static blocks. Here is the meat of it:
Step One: Create Static Block in Your Magento Admin
Magento Admin Panel—>Static Blocks—>Add New Block
1) Name your Static Block, in this case Custom footer Links
2) Label the Identifier (This is the link you will use to call the block later) in this case, custom-footer-links
3) Choose what store view you would like it to render in
4) Set Status to Enabled
2)Now for the fun part! Add your navigation links to the block. Make sure to use to make them match your sites color and theme.
Step Two: Inserting Code to Call the Static Block
This part is going to require you to FTP into your Magento site and modify footer.phtml
app—>design—>frontend—->default—>(your template)—>template—>page—>footer.phtml
Find where in the footer you want your navigation links to display and insert:
getLayout()->createBlock(‘cms/block’)->setBlockId(‘custom-footer-links’)->toHtml(); ?>
Now most of the time the Static block should display just fine but in some cases you are going to have do some extra steps to have the block display.
1) Instead of inserting:
getLayout()->createBlock(‘cms/block’)->setBlockId(‘custom-footer-links’)->toHtml(); ?>
Use:
custom-footer-links
2)Modify catalog.xml
app—>design—>frontend—>default—>f002—>layout—>catalog.xml
Add under
custom-footer-links

Resources