Create helper for generate data attributes
Hi all, for now we have lot of parts, that contains code like this:
<button type="button" title="<?php echo $this->__('Add to Cart') ?>" class="button btn-cart" data-mage-redirect="{event: 'click', url: '<?php echo $this->getAddToCartUrl($_product) ?>'}">
<span><span><?php echo $this->__('Add to Cart') ?></span></span>
</button>
I think we need create some helper, that will contains methods like this:
class ...Helper {
...
public function getMageRedirect($url, $event = 'click') {
$result = array(
'event' => $event,
'url' => $url
);
return 'data-mage-redirect="' . json_encode($result) . '"';
}
}
As example we will have something like this:
<?php
$_dataHelper = $this->helper('...our_helper...');
?>
<button type="button" title="<?php echo $this->__('Add to Cart') ?>" class="button btn-cart" <?php echo $_dataHelper->getMageRedirect($this->getAddToCartUrl($_product)) ?>>
<span><span><?php echo $this->__('Add to Cart') ?></span></span>
</button>
Any things about this?
@igor-svizev
I searched for occurrences of such redirects in existing code base and found only one use case of generating such a "data-mage-redirect" attribute -- in the product list template as in your code example. So creating such a generic helper for sake of code reusing is not appealing in this case...
Did you intend only to propose a solution of mere code reusing or you'd like to have a framework feature that allows using arbitrary helpers in templates, similar to Zend View or Twig? If the latter, I'd recommend you to wait until next update (2.0.0.0-dev45) where you'll see support of Twig template engine along with phtml.
The Magento-Twig integration (will be available in 2.0.0.0-dev45 release) already has helpers capability (supported natively by Twig library), but for phtml-templates we have only interface of Mage_Core_Block_Template. By inspecting implementation of rendering phtml-templates, you'll see that $this is not the block object anymore -- it is a renderer -- a different object that transfers public method class to the block. In that implementation, it would be relatively easy to implement "helpers" support in phtml-renderer (and not bloat interface of Mage_Core_Block_Template or Mage_Core_Block_Abstract).
So let us know if you want to contribute such a change after 2.0.0.0-dev45 is released.
Issue closed due to the absence of activity.