To call a block in magento home page static block - php

I want to call a block in content of home page. I am writing a code like that:
{{block type='blog/menu_sidebar' template='latest_blog/latest-blog.phtml'}}
But phtml file is not coming in home page.
On the other hand when I call block in layout update xml under under the design tab by writing the code like that :
<block type="blog/menu_sidebar" name="right.blog">
<action method="setTemplate" >
<template>latest_blog/latest-blog.phtml</template>
</action>
<block type="blog/tags" name="blog_tags" />
</block>
Then the phtml file is coming in home page.
My problem is that i want to include latest-blog.phtml file in the content of home page because I will have to play with the div structure for designing which i can not play in the layout section.

You can use getLayout() function
<?php echo $this->getLayout()
->createBlock("blog/menu_sidebar")
->setTemplate("latest_blog/latest-blog.phtml")
->toHtml(); ?>
This way u can load. if you have any option you can set by calling
->setCustomOption($optionValue)

Related

How can I add a unique second static block to every category page at the bottom? with magento

I want to add a second static block to the bottom of every category page in my magento 1.9 store.
Like this site
I want a description on top of the products but also a larger description at the bottom.
I am very familiar with magento and hard coding but it seems I get stuck here. I search the web for ours but no solution.
I could manage it to get a static block at the footer but then on every page it is the same text and I want different text at each category.
You can do this through layout.xml. To add a static block simply add:
<block type="cms/block" name="block_key">
<action method="setBlockId">
<block_id>block_key</block_id>
</action>
</block>
to the content node on the category section. Then you can call it via:
<?php echo $this->getChildHtml('block_key'); ?>
where ever you would like in your template.
To add custom text you can use the magic set method which you will add to the custom design section on the category page.
<reference name="block_key">
<action method="setCustomText">
<text>This is my custom text</text>
</action>
</reference>
Then within your template/block you can get this from using the following :
<?php echo $this->getCustomText(); ?>
Let me know if this works for you.

magento - use a frontend template in adminhtml

I have a order timeline page on frontend at :
/magento/app/design/frontend/default/mytheme/template/sales/order/info.phtml
I am trying to use the same page on adminhtml, from sales -> orders on a single view order page I am creating a hyperlink on click of that I would like to show a popup that will display the timeline same as displayed on frontend page of info.phtml. Can I use the same template info.phtml I have in frontend or I have to create one more for backend? also any ideas how to approach this? thanks
I have created a black in
magento/app/design/adminhtml/default/default/layout/sales.xml and added the template code of info.phtml in timeline.phtml
<adminhtml_sales_order_timeline>
<block type="adminhtml/sales_order_timeline" name="timeline_tracking" template="sales/order/view/timeline.phtml"></block>
</adminhtml_sales_order_timeline>
Edit :
The request is going to the controller :
public function timelineAction()
{
$this->loadLayout();
$this->renderLayout();
}
which loads the following layout :
<adminhtml_sales_order_timeline>
<remove name="header" />
<remove name="footer" />
<block type="adminhtml/sales_order_timeline" name="sales_order_timeline" template="sales/order/timeline/timeline.phtml" />
</adminhtml_sales_order_timeline>
this removes the header and footer but this is not displaying the content of the template timeline.phtml, I have not done any other configurations for block, what am I missing?
your Admin layouts will look for the phtmls inside adminhtml/ folder and not the frontend/ folders. What you can do is copy the info.phtml inside your adminhtml/default/default/template/your_folder_name and include that in your adminhtml layout.

Add Shopping cart block to cms page

I would like to add a shopping cart block a cms page, but whenever I try, nothing happens...not even an error.
I've tried following this tutorial, http://www.magento.cc/how-to-use-php-on-a-cms-page.html.
So I created the new folders in app/code/local and then a Test.php file, yet when I try to include
{{block type="YourModule_Custom/test" my_param1="value 1" another_param="value 2"}}
in the cms page, nothing appears.
Here's my code in the Test.php page:
<?php
class YourModule_Custom_Block_Test extends Mage_Core_Block_Abstract
{
protected function _toHtml()
{
echo 'TEST';
$this->getChildHtml('header');
return $html;
}
}
You can do a local.xml update. And place your content as .phtml file
<cms_index_index>
<reference name="content">
<block type="Your_custom/Block" name="home_main" as="home_main" template="cms/default/home.phtml">
</block>
</reference>
</cms_index_index>
Create a file named home.phtml in cms/default/ Then you can use your own block type to use your custom module / functions
Then add your home page content in there.
I don't know what are you trying to achieve. But from your question, I have a strong feeling that, you are trying to set a template of your own through CMS page. If that is the case, let us analyze why your block didn't show any output.
Your block definition is like this
{{block type="YourModule_Custom/test" my_param1="value 1" another_param="value 2"}}
There is no problem with this defintion. But it is good, if you add name to your block. If you set a template along with that, then you don't need any backend code for setting a template for your block. That is your block should look like this
{{block type="YourModule_Custom/test" name="test.block" template="test.phtml"}}
Now when magento encounter this, it will find your block, set described name to that block (for later reference this name will be used. However it is not relevant in this case), set template specified to your block and then render the content in that template.
So you should have a block of type that you specified. You had it right now(you dont need that _toHtml() inside that). Along with that you need a template file test.phtml and it should be in the location app/design/frontend/<your_package>/<your_theme>/template/test.phtml. You dont have that file right now. So create it and add this content inside that file
<p><?php echo "I am here. Can you see me ?"; ?></p>
Now check your CMS page output. You can see that content. Isn't it ?
So right now what you are trying to do is, instead of setting a template along with that block definition, you are trying to set it through your block. Is it wrong ? Obviously, NO. There are some cases, where we need to go with that way. I assume you really need it. So again let us redifine our block in this form.
{{block type="YourModule_Custom/test" name="test.block"}}
Hmm. Here we didn't set a template to this block right now. You can hence set it through your block definition. You used _toHtml() method.
<?php
class YourModule_Custom_Block_Test extends Mage_Core_Block_Abstract
{
protected function _toHtml()
{
echo 'TEST';
$this->getChildHtml('header');
return $html;
}
}
?>
This method is using to set a template and then renders the content. So you are in the right track. But the problem here is, you are not setting any template!! plus your method returns a variable $html which holds nothing. So what should we return through a _toHtml() ? The answer lies in Mage_Core_Block_Template. Let us look on _toHtml() definition
protected function _toHtml()
{
if (!$this->getTemplate()) {
return '';
}
$html = $this->renderView();
return $html;
}
Basically what this does is, it checks whether a template is set , if not return nothing. If it does renders it. That means, it is obvious that we need to set a template. So your block should look like this.
<?php
class YourModule_Custom_Block_Test extends Mage_Core_Block_Template
{
protected function _toHtml()
{
$this->setTemplate('test.phtml');
$html = parent::_toHtml();
return $html;
}
}
Note that your block extends from Mage_Core_Block_Template rather from Mage_Core_Bock_Abstract. This is because setTemplate() method is defined in Mage_Core_Block_Template class. In _toHtml(), we are setting our template and then leave rest to our parent block. Now check whether content in test.phtml is showing in your CMS page. It does right ?
I don't understand exact what do you want. but by your question my understanding is you want to display shopping cart block into any CMS page like Home page, About us page etc.
If my understanding is right then here is one solution for you.
You can insert this code into your cms page from admin.
Admin -> CMS -> Pages -> Select any page on which you want to display block -> Click on design tab from left navigation ->Under Page Layout section insert the below code in "Layout Update XML" field. Click on save.
<reference name="content">
<block type="checkout/cart" name="checkout.cart">
<action method="setCartTemplate"><value>checkout/cart.phtml</value></action>
<action method="setEmptyTemplate"><value>checkout/cart/noItems.phtml</value></action>
<action method="chooseTemplate"/>
<action method="addItemRender"><type>simple</type><block>checkout/cart_item_renderer</block><template>checkout/cart/item/default.phtml</template></action>
<action method="addItemRender"><type>grouped</type><block>checkout/cart_item_renderer_grouped</block><template>checkout/cart/item/default.phtml</template></action>
<action method="addItemRender"><type>configurable</type><block>checkout/cart_item_renderer_configurable</block><template>checkout/cart/item/default.phtml</template></action>
<block type="core/text_list" name="checkout.cart.top_methods" as="top_methods" translate="label">
<label>Payment Methods Before Checkout Button</label>
<block type="checkout/onepage_link" name="checkout.cart.methods.onepage" template="checkout/onepage/link.phtml"/>
</block>
<block type="page/html_wrapper" name="checkout.cart.form.before" as="form_before" translate="label">
<label>Shopping Cart Form Before</label>
</block>
<block type="core/text_list" name="checkout.cart.methods" as="methods" translate="label">
<label>Payment Methods After Checkout Button</label>
<block type="checkout/onepage_link" name="checkout.cart.methods.onepage" template="checkout/onepage/link.phtml"/>
<block type="checkout/multishipping_link" name="checkout.cart.methods.multishipping" template="checkout/multishipping/link.phtml"/>
</block>
<block type="checkout/cart_coupon" name="checkout.cart.coupon" as="coupon" template="checkout/cart/coupon.phtml"/>
<block type="checkout/cart_shipping" name="checkout.cart.shipping" as="shipping" template="checkout/cart/shipping.phtml"/>
<block type="checkout/cart_crosssell" name="checkout.cart.crosssell" as="crosssell" template="checkout/cart/crosssell.phtml"/>
<block type="checkout/cart_totals" name="checkout.cart.totals" as="totals" template="checkout/cart/totals.phtml"/>
</block>
</reference>
After that you will see shopping cart block on your CMS page. If you don't need any block from them you can remove that blck from the above code.
if you try to call add to cart from cms page then add url like
checkout/cart/add?product=$id&qty=$qty
Example-
<a href="{{store url='checkout/cart/add?product=5&qty=3'}}">
<img src="{{skin url='images/addtocart.jpg'
}}" alt="product5" /></a>

call a phtml file in Layout Update XML to set title in magento [duplicate]

In Magento how to call a phtml file in cms page to set page title which title I set in my phtml file? I am using
$this->getLayout()->getBlock('head')->setTitle('your title');
to set page title.
To call a phtml file in a cms page or cms static block:
{{block type="core/template" template="templateFolder/your_template.phtml"}}
If you know, where the block file(php file) for your phtml file resides, then you can use it as type.
Example: Suppose you want to call new.phtml file that resides in catalog/product folder, and you know that its corresponding Block file(php file) resides in Catalog/Product folder, then you can use:
{{block type="catalog/product" template="catalog/product/new.phtml"}}
More reading: here
Hope this helps!
You cannot change the title of the page from a template file when using it in a cms block or cms page because the head block is already rendered when the page (or block) content is parsed.
It's not possible to change page title from phtml file of cms pages as already told by #Marius
you need to add to it's design in cms page as given below :
<reference name="head">
<action method="setCustomTitle" translate="title"> <title> Custom Title </title> </action>
</reference>
Add the below XML piece under CMS > Pages > Manage Content > Select a specific CMS Page
Navigate to "Design" tab > Layout Update XML >
<reference name="head">
<action method="setCustomTitle" translate="title"> <title> Custom Title </title> </action>
</reference>
Make sure the CACHE folders are DELETED under below:
{Root Magento Folder}/var/cache
{Root Magento Folder}/var/full_page_cache
Hope this helps!
Happy Coding...

How to call a phtml file in cms page to set page title

In Magento how to call a phtml file in cms page to set page title which title I set in my phtml file? I am using
$this->getLayout()->getBlock('head')->setTitle('your title');
to set page title.
To call a phtml file in a cms page or cms static block:
{{block type="core/template" template="templateFolder/your_template.phtml"}}
If you know, where the block file(php file) for your phtml file resides, then you can use it as type.
Example: Suppose you want to call new.phtml file that resides in catalog/product folder, and you know that its corresponding Block file(php file) resides in Catalog/Product folder, then you can use:
{{block type="catalog/product" template="catalog/product/new.phtml"}}
More reading: here
Hope this helps!
You cannot change the title of the page from a template file when using it in a cms block or cms page because the head block is already rendered when the page (or block) content is parsed.
It's not possible to change page title from phtml file of cms pages as already told by #Marius
you need to add to it's design in cms page as given below :
<reference name="head">
<action method="setCustomTitle" translate="title"> <title> Custom Title </title> </action>
</reference>
Add the below XML piece under CMS > Pages > Manage Content > Select a specific CMS Page
Navigate to "Design" tab > Layout Update XML >
<reference name="head">
<action method="setCustomTitle" translate="title"> <title> Custom Title </title> </action>
</reference>
Make sure the CACHE folders are DELETED under below:
{Root Magento Folder}/var/cache
{Root Magento Folder}/var/full_page_cache
Hope this helps!
Happy Coding...

Categories