Skip to content

Instantly share code, notes, and snippets.

@mattwellss
Last active December 25, 2016 18:50
Show Gist options
  • Save mattwellss/a2530209a54fda7fd67d9c7800835f40 to your computer and use it in GitHub Desktop.
Save mattwellss/a2530209a54fda7fd67d9c7800835f40 to your computer and use it in GitHub Desktop.
Some MageStache Examples
<config>
<global>
<events>
<core_copy_fieldset_catalog_product_list_to_mustache>
<observers>
<asdf>
<class>magestache/observer</class>
<method>copyCatalogProductFieldsetToMustache</method>
</asdf>
</observers>
</core_copy_fieldset_catalog_product_list_to_mustache>
<core_copy_fieldset_prepare_list_product_to_mustache>
<observers>
<asdf>
<class>magestache/observer</class>
<method>prepareProductDataForMustache</method>
</asdf>
</observers>
</core_copy_fieldset_prepare_list_product_to_mustache>
</events>
</global>
</config>
<layout>
<catalog_category_layered>
<reference name="category.products">
<action method="unsetChild">
<child>product_list</child>
</action>
<block type="magestache/mustache_template" name="mustache.product_list" as="product_list"
template="catalog/product/list.mustache">
<action method="setChild">
<alias>product_list.nested</alias>
<child>product_list</child>
</action>
<block type="magestache/mustache_template" name="mustache.listed.product"
template="catalog/product/listed_product.mustache">
<action method="setFieldsetName">
<fieldsetName>prepare_list_product</fieldsetName>
</action>
</block>
<action method="setDataBlock">
<block>product_list.nested</block>
</action>
<action method="setFieldsetName">
<fieldsetName>catalog_product_list</fieldsetName>
</action>
</block>
</reference>
</catalog_category_layered>
</layout>
{{%MAGENTO-TEMPLATE}}
{{^has_products}}
<p class="note-msg">{{#__}}There are no products matching the selection.{{/__}}</p>
{{/has_products}}
{{#has_products}}
<div class="category-products">
{{{toolbar_html}}} {{! That's HTML generated by `getToolbarHtml` }}
<ul class="products-grid">
{{#products}}
{{> mustache.listed.product}}
{{/products}}
</ul>
{{{toolbar_html}}}
</div>
{{/has_products}}
{{%MAGENTO-TEMPLATE}}
{{%IMPLICIT-ITERATOR}}
<li class="item">
<a href="{{product_link}}" class="product-image">
<img src="{{img_src}}" id="product-collection-image-{{id}}"/>
</a>
<div class="product-info">
<h2 class="product-name">
<a href="{{product_link}}" title="{{#stripTags}}{{name}}{{/stripTags}}">{{name}}</a>
</h2>
{{#name_after_htmls}}
{{{.}}}
{{/name_after_htmls}}
{{{price_html}}}
{{#has_rating_summary}}
{{{rating_summary_html}}}
{{/has_rating_summary}}
<div class="actions">
{{#configurable}}
{{#salable}}
<button class="button btn-cart" type="button"
title="{{#quoteEscape}}Add to Cart{{/quoteEscape}}">
{{#__}}Add to Cart{{/__}}
</button>
{{/salable}}
{{/configurable}}
<ul class="add-to-links">
{{#wishlist_add_url}}
<li><a href="{{wishlist_add_url}}" class="link-wishlist">
{{#__}}Add to Wishlist{{/__}}
</a></li>
{{/wishlist_add_url}}
{{#compare_add_url}}
<li><a href="{{compare_add_url}}" class="link-compare">
{{#__}}Add to Compare{{/__}}
</a></li>
{{/compare_add_url}}
</ul>
</div>
</div>
</li>
<?php
namespace MattWellss\MageStache;
use Mage;
class Model_Observer
{
/**
* @param \Varien_Event_Observer $observer
* @return void
*/
public function copyCatalogProductFieldsetToMustache(\Varien_Event_Observer $observer)
{
/** @var \Varien_Object $target */
$target = $observer->getTarget();
/** @var \Mage_Catalog_Block_Product_List $source */
$source = $observer->getSource();
$target->addData([
'has_products' => function () use ($source) {
return count($source->getLoadedProductCollection()) > 0;
},
'products' => function () use ($source) {
return $source->getLoadedProductCollection();
}
]);
}
public function prepareProductDataForMustache(\Varien_Event_Observer $observer)
{
/** @var \Varien_Object $target */
$target = $observer->getTarget();
/** @var \MattWellss_MageStache_Block_Mustache_Template $source */
$source = $observer->getSource();
/** @var \Mage_Catalog_Model_Product $tempProduct */
$tempProduct = Mage::getModel('catalog/product', $source->getData());
/** @var \Mage_Catalog_Block_Product_List $productList */
$productList = $source->getParentBlock()->getChild('product_list.nested');
$wishlistHelper = Mage::helper('wishlist');
$target->addData([
'product_link' => $tempProduct->getProductUrl(),
'price_html' => $productList->getPriceHtml($tempProduct, true),
'img_src' => Mage::helper('catalog/image')->init($tempProduct, 'small_image')->keepFrame(false)->resize(400),
'name_after_htmls' => array_map(function ($_nameAfterChildName) use ($productList, $tempProduct) {
return $productList->getChild('name.after')
->getChild($_nameAfterChildName)
->setProduct($tempProduct)
->toHtml();
}, $productList->getChild('name.after')->getSortedChildren()),
'wishlist_add_url' => $wishlistHelper->isAllow()
? $wishlistHelper->getAddUrl($tempProduct) : '',
'compare_add_url' => $productList->getAddToCompareUrl($tempProduct)
]);
if ($target->getData('has_rating_summary')) {
$target->setData('rating_summary_html', $productList->getReviewsSummaryHtml($tempProduct, 'short'));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment