How to set up lesti:FPC to work with dynamic templates? - magento

I'm would like to use lesti:FPC with a module I developed.
To enable SEO friendly urls ALL requests to the module are sent to the module index action, and there a template is dynamically loaded. How can I make lesti:FPC work under this scenario?
On the module layout file (../layout/addon.xml) I have:
<block type="addon/index" name="addon_index" template="addon/index.phtml"/>
On the module indexAction I have:
if($condition)
{
$this->getLayout()->getBlock('addon_index')->setTemplate('addon/a.phtml');
}
else
{
$this->getLayout()->getBlock('addon_index')->setTemplate('addon/b.phtml');
}
Would adding 'addon_index' to the lesti:FPC layout-handles be enough to get my module pages cached?

Calling Dynamic Block in Lesti Fpc depends on conditions can be done using observer
**Config.xml**
<frontend>
<events>
<core_block_abstract_to_html_before>
<observers>
<atwix_test>
<type>model</type>
<class>namspace_test/observer</class>
<method>insertBlock</method>
</atwix_test>
</observers>
</core_block_abstract_to_html_before>
</events>
</frontend>
class Namespace_Test_Model_Observer
{
public function insertBlock($observer)
{
/** #var $_block Mage_Core_Block_Abstract */
/*Get block instance*/
$_block = $observer->getBlock();
/*get Block type*/
$_type = $_block->getType();
/*Check block type*/
if ($_type == 'catalog/product_price') {
/*Clone block instance*/
$_child = clone $_block;
/*set another type for block*/
$_child->setType('test/block');
/*set child for block*/
$_block->setChild('child', $_child);
/*set our template*/
$_block->setTemplate('at.phtml');
}
}
}
And finally, here is a template at.phtml code:
echo $this->getChildHtml('child');
Hope it will help you.

Related

How to override the radio.phtml in custom module?

I have created a custom module which change the template for the bundle product option, it is in the radio.phtml.
I am now overriding the radio.phtml on a new theme xxx/template/bundle/catalog/product/view/type/bundle/option/radio.phtml.
But I want to put the radio.pthml into my custom module folder, that is
template/mycompany/mymodule/radio.phtml
I understand that I can do in the mymodule.xml layout by using <action method="setTemplate">, but how can I know the <reference> name for the radio.phtml?
you need to rewrite this block because the theme are set in block..
Mage_Bundle_Block_Catalog_Product_View_Type_Bundle_Option_Radio
you can in this function
protected function _construct()
{
$this->setTemplate('bundle/catalog/product/view/type/bundle/option/radio.phtml');
}
you can rewrite block module like that
<global>
<blocks>
<bundle>
<rewrite>
<catalog_product_view_type_bundle_option_radio>Spacename_Modulname_Block_Adminhtml_Radio</catalog_product_view_type_bundle_option_radio>
</rewrite>
</bumdle>
</blocks>
</global>
and call your phtml file
class Spacename_Modulname_Block_Adminhtml_Radio extends Mage_Core_Block_Template
{
protected function _construct()
{
$this->setTemplate('test/radio.phtml'); //your file path link here
}
}

Add Top Links on condition basis in magento

I am little bit stuck with adding top links in my custom phtml.I removed the links block in my xml <remove name="top.links"/> , now after some condtion become true i want to add this block again.When i use this code for top menu it works but not for links
$block = Mage::getSingleton('core/layout');
echo $block->createBlock('catalog/navigation')->setTemplate('catalog/navigation/top.phtml')->toHtml();
This works and disply top menu.But the below code doesn't show anything.
$block = Mage::getSingleton('core/layout');
echo $block->createBlock('page/template_links')->setTemplate('page/template/links.phtml')->toHtml();
Any help ???
After using observer i solve my problem for time being.I think this is alternate way.In my config i define oberser like :
<frontend>
<events>
<controller_action_layout_generate_xml_before>
<observers>
<Mymodule>
<class>Mymodule_Model_Observer</class>
<method>addmyblock</method>
</Mymodule>
</observers>
</controller_action_layout_generate_xml_before>
</events>
</frontend>
while in oberser simply check and remove block :
<?php
class Mymodule_Model_Observer
{
public function addmyblock(Varien_Event_Observer $observer)
{
if(Mage::getStoreConfig("mymodule/general/enable")==1)
{
$layout = $observer->getLayout();
$layout->getUpdate()->addUpdate('<remove name="top.search"/>
<remove name="top.links"/>');
$layout->generateXml();
}
}
}.
Also i removed the code from xml <remove name="top.links"/>.So the code only remove block when the condition become true.

How to load CSS file for a field in Magento?

I have a custom admin config field type in Magento and I would like to load up a CSS file for that config field(also javascript too).
The field class:
class Company_Module_Block_Configure extends Mage_Adminhtml_Block_System_Config_Form_Field
{
protected function _getElementHtml(Varien_Data_Form_Element_Abstract $element)
{
// I would like to load the CSS and Javascripts files to the head.
return "Here comes the field output";
}
}
I have tried this, but no luck. Maybe the head rendered sooner:
Mage::app()->getLayout()->getBlock('head')->addCss('My css url...');
Or somehow I should Observe some kind of "before_body_end" event. Is it possible?
Update #1:
I have started to go through the before_body_end event:
Config.xml
<config>
<global>
<events>
<core_block_abstract_to_html_before>
<observers>
<test>
<class>Company_Module_Model_Observer</class>
<method>test</method>
</test>
</observers>
</core_block_abstract_to_html_before>
</events>
</global>
</config>
And the observed method looks like this:
public function test($block){
if($block->getEvent ()->getBlock()->getNameInLayout() == 'before_body_end'){
}
}

getData doesn't work in Mage_Core_Block_Template custom block

I defined a custom block this way:
<frontend>
<layout>
<updates>
<categorylist module="mymodule">
<file>mymodule.xml</file>
</categorylist>
</updates>
</layout>
</frontend>
<global>
<blocks>
<categorylist>
<class>Alias_CategoryList_Block</class>
</categorylist>
</blocks>
</global>
Then I defined the block class this way
class Alias_CategoryList_Block_List extends Mage_Core_Block_Template
{
public $idCategory = NULL;
// Contructor
public function __construct()
{
echo $this->getData('categoryid');
}
}
and the layout this way:
<default translate="label">
<block type="categorylist/list" name="categorylist.list" output="toHtml" after="-" template="mymodule.phtml"/>
I put the block in a CMS this way:
{{block type="categorylist/list" categoryid="10"}}
But sadly the $this->getData('categoryid'); retrieves nothing.
Cannot figure out what's wrong ?
I tryed even $this->getCategoryid; but even nothing.
Anyone can help?
I'm using Magento 1.7
Would it be silly to add a view (phtml) template file that has
<?php echo $this->getCategoryId(); ?>
instead of trying to accomplish that in the constructor? Also then you wouldn't need your own code behind file you could just use core/template.
So your cms would be
{{block type="core/template" template="awesome.phtml" cateogryid="10"}}
The problem was that I assumed that the layout updates configuration will extend the code in the CMS but the code in the CMS should not have block name and template parameters for my purposes. So I fixed it declearing the template in the contructor and removing the layout updates in configuration (since I don't need the block to override existing blocks):
// Contructor
public function __construct()
{
$this->setTemplate('mymodule.xml');
}

Dynamically insert into layout xml of Magento

Im a newbie at Magento. Im trying to build a module which dynamically inserts code xml into layout xml before its rendered -
something similar to how CMS>pages have been build.
Just like how we can specify layout xml in the design section of a page (admin > cms > page), I would like to insert into layout.xml through my module.
Basically, I would like to
have an admin section from where I can enter layout code through a
form and store in database - I have figured out this part
have Magento pull these pieces of code stored in the database and create an xml file before the layout files are aggregated and interpreted. - Im unable to build this portion.
Any help would be appreciated.
Just a little enlightment
You can add those layout xml by using observer,
Let's say you want those layout xml added before the xml is generated
We can use event controller_action_layout_generate_xml_before
Here's the sample code (in config.xml)
<frontend>
<events>
<controller_action_layout_generate_xml_before>
<observers>
<add_new_layout>
<class>test/observer</class>
<method>addNewLayout</method>
</add_new_layout>
</observers>
</controller_action_layout_generate_xml_before>
</events>
</frontend>
Here's the Observer.php
public function addNewLayout($observer){
$layout = $observer->getEvent()->getLayout();
$update = $layout->getUpdate();
//$action = $observer->getEvent()->getAction();
//$fullActionName = $action->getFullActionName();
//in case you're going to add some conditional (apply these new layout xml on these action or other things, you can modify it by yourself)
//here is the pieces of layout xml you're going to load (you get it from database)
$xml = "<reference name='root'><remove name='footer'></remove></reference>";
$update->addUpdate($xml);
return;
}
Another posibility is to use the core_layout_update_updates_get_after-event and a placeholder (non-existing) layout xml:
<frontend>
<layout>
<updates>
<foobar>
<file>foo/bar.xml</file>
</foobar>
</updates>
</layout>
<events>
<core_layout_update_updates_get_after>
<observers>
<foobar>
<type>singleton</type>
<class>foobar/observer</class>
<method>coreLayoutUpdateUpdatesGetAfter</method>
</foobar>
</observers>
</core_layout_update_updates_get_after>
</events>
</frontend>
Example PHP in your observer:
/**
* Event dispatched when loading the layout
* #param Varien_Event_Observer $observer
*/
public function coreLayoutUpdateUpdatesGetAfter($observer)
{
/** #var Mage_Core_Model_Config_Element $updates */
if (Mage::getStoreConfig('foobar/general/enabled')) {
$file = Mage::getStoreConfig('foobar/general/layout_xml');
if (!empty($file)) {
$updates = $observer->getUpdates();
if ($updates->foobar) {
$updates->foobar->file = $file;
}
}
}
}

Resources