magento: get variable from block or hlper into javascript file - magento

I am including a js file via the layout xml:
for example:
<reference name="head">
<action method="addItem">
<type>js</type>
<name>myjs.js</name>
</action>
</reference>
This is fine. But, i am setting up an ajax call inside myjs.js and would like to pull the post url from a helper class.
How would this be possible?

Inside your reference to the head I would add a block which would output a partial that sets a js var ahead of the myjs.js file being added.
For instance:
<reference name="head">
<block type="page/html" name="mycustomblock" template="page/html/mycustomblock.phtml"/>
<action method="addItem">
<type>js</type>
<name>myjs.js</name>
</action>
</reference>
And your file mycustomblock.phtml would contain:
<?php
// below is javascript that is being populated by your helper
?>
<script> var myblogpost = <?php echo Mage::helper('mycustom/helper')->blogPostUrl();?>; </script>

Related

Remove canonical link in Magento 1.9.3.x

I want to remove the canonical link from a html page (Site Map).
<link rel="canonical" href="https://www.example.com/seositemap">
I used the following code in a layout and it does not work
<?xml version="1.0"?>
<layout version="0.1.0">
<seositemap_index_index>
<reference name="content">
<block type="seositemap/map" name="map" template="seositemap/map.phtml"/>
</reference>
<reference name="head">
<action method="removeItem"><type>link_rel</type><rel>canonical</rel></action>
</reference>
</seositemap_index_index>
</layout>
Any idea ?
here is how the method looks like
public function removeItem($type, $name)
{
unset($this->_data['items'][$type.'/'.$name]);
return $this;
}
there is no rel parameter. You should use "name" instead. And its content has to be the href of current link

Nested block and getChildHtml() in magento

I have some trouble when i get data with function getChildHtml() .
Here is my layout.xml file
<?xml version="1.0"?>
<layout version="0.1.0">
<hello_index_index>
<reference name="content">
<block type="hello/hello" name="title" template="hello/title.phtml">
<block type="hello/hello" name="title1" template="hello/title1.phtml">
<block type="hello/hello" name="title2" template="hello/title2.phtml"/>
</block>
</block>
</reference>
</hello_index_index>
</layout>
when i try print data in file title2.phtml to the screen with code is
echo $this->getChildHtml('title2');
OR
echo $this->getChildHtml('title1');
In the first way is no data displayed on the screen .
In the second way have data displayed on the screen but only data in file title1.phtml .
So how to display the data in file title2.phtml
Thank everybody .
As your code example is somewhat obscured, it's difficult for me to understand what your blocks actually are and/or do.
Remove the association between "title1" and "title2", then try rendering your block(s) with getChildHtml again.
<?xml version="1.0"?>
<layout version="0.1.0">
<hello_index_index>
<reference name="content">
<block name="title" template="hello/title.phtml" type="hello/hello">
<block name="title1" template="hello/title1.phtml" type="hello/hello" />
<block name="title2" template="hello/title2.phtml" type="hello/hello" />
</block>
</reference>
</hello_index_index>
</layout>
<block type="hello/hello" name="title" template="hello/title.phtml">
<block type="hello/hello" name="title1" template="hello/title1.phtml">
<block type="hello/hello" name="title2" template="hello/title2.phtml"/>
</block>
</block>
in title.phtml write $this->getChildHtml('title1'); and in title1.phtml write
$this->getChildHtml('title2')
because magento blocks can render only "child" blocks when using "getChildHtml()"

Add Banner Below Menu in Category Page

I need to add the category banner below the Breadcrumbs.
I have tried by adding new reference
<?php echo $this->getChildHtml('category_banner') ?>
in the 2columns-left.phtml
and in local.xml
<catalog_category_view>
<reference name="category_banner">
<block type="core/template" name="topbanner" template="catalog/category/category-image.phtml" before="-"></block>
</reference>
</catalog_category_view>
<catalog_category_default translate="label">
<reference name="category_banner">
<block type="core/template" name="banner" template="catalog/category/category-image.phtml"></block>
</reference>
</catalog_category_default>
But output is not coming.
Added new line inside the all layout i.e 2columns-left.phtml etc.
<?php echo $this->getChildHtml('category_banner') ?>
added below line inside your xml
<catalog_category_view>
<reference name="root">
<block type="core/template" name="category_banner" template="catalog/category/category-image.phtml" before="-"></block>
</reference>
</catalog_category_view>

Magento getChildHtml block not rendering

I'm attempting to rearrange some elements on a webpage and my code won't render it its new position.
The top of the page is handled in view.phtml and the bottom in list.phtml. I'm wanting to move form.phtml from view to list.
The xml looks like this:
<block type="review/product_view" name="product.info" template="catalog/product/view.phtml">
<block type="catalog/product_view_media" name="product.info.media" as="media" template="catalog/product/view/media.phtml">
<action method="disableGallery"/>
</block>
<block type="review/form" name="product.review.form" as="review_form" template="review/form.phtml"/>
the view.phtml contains this call:
<?php echo $this->getChildHtml('review_form') ?>
However, no dice. I'm very new to Magento, so any help would be great. I've looked into the following questions and more with no results:
Nested block within a reference not rendering when using ->getChildHtml()
Magento $this->getChildHtml('media') return blank
Call magento template via $this->getChildHtml()
Thanks!
As per as your description,review child block is call in under view then...
it should be
<block type="review/product_view" name="product.info" template="catalog/product/view.phtml">
<block type="catalog/product_view_media" name="product.info.media" as="media" template="catalog/product/view/media.phtml">
<action method="disableGallery"/>
</block>
<block type="review/form" name="product.review.form" as="review_form">
<block type="page/html_wrapper" name="product.review.form.fields.before" as="form_fields_before" translate="label">
<label>Review Form Fields Before</label>
<action method="setMayBeInvisible"><value>1</value></action>
</block>
</block>
.........

Understanding Magento Adminhtml Layouts

I am trying to load a custom layout page for my custom admin page "devices". Its not working ! :)
<layout version="0.1.0">
<adminhtml_devices_index_index>
<reference name="content">
<block type="adminhtml/template" name="index" output="toHtml" template="devices/index.phtml"/>
</reference>
</adminhtml_devices_index_index>
</layout>
If this is the XML in my app/design/adminhtml/default/default/layout/devices.xml
What are the different files/attributes I must be aware of ?
Also, what does type='adminhtml/template' actually point to ?
Note: My Controller code is
public function indexAction() {
$this->loadLayout();
$this->renderLayout();
}
For a quick test, use a non-template block. The block is going to be output as a course of rendering in the "content" block, so no need to specify output="toHtml".
<reference name="content">
<block type="core/text" name="devices.test">
<action method="setText">
<val>This is some sample text</val>
</action>
</block>
</reference>
The block type argument adminhtml/template points to the block class group "adminhtml", which is defined in Mage_Adminhtml config.xml:
<global>
<blocks>
<adminhtml>
<class>Mage_Adminhtml_Block
Based on this, the adminhtml/template argument points to the block name Mage_Adminhtml_Block_Template.
Try it:
<layout version="0.1.0">
<devices_adminhtml_index_index>
<reference name="content">
<block type="adminhtml/template" name="index" output="toHtml" template="devices/index.phtml"/>
</reference>
</devices_adminhtml_index_index>
</layout>
And I not sure what here should be: adminhtml/template Try replace it with core/template

Resources