Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Magento 1 extend filter, add 2 additional if statements and support in if statements conditions like ==, !=, >= and other

Note:

  • This class extends the original Magento 1 class Varien_Filter_Template.
  • If you use it in newer Magento 1 versions please upgrade your code: compare filter method code from original class and change MyLibNamsepace_Filter_Template accordingly.
  • To make the above code work with emails you need to rewrite the core class Mage_Core_Model_Email_Template_Filter to extend the bove class instead of extending default class Varien_Filter_Template

Usage:

{{outer_if myVar!=="ipsum"}} {{if customer.getGroupId()==2}} Public text {{else}} {{inner_if order.getTotal()>1000}} Good deal {{inner_else}} Marketing {{/inner_if}} {{/if}} {{outer_else}} nothing {{outer_if}}

Default core Magento 1 usage of filter template on customer address summary: app/code/core/Mage/Customer/Block/Address/Renderer/Default.php $formater = new Varien_Filter_Template(); To make it work override the method and change it into: $formater = new MyLibNamsepace_Filter_Template();

Magento 2 equivalent code: https://gist.github.com/daniel-ifrim/74a0310c6acd9f664381c895bf0fcb35

<?php
//lib/LenpIt/Filter/Template.php
/**
* Overrides Template constructions filter.
* - adds condition in if construct pattern.
* - adds {inner_if condition}...{inner_else}...{/inner_if} construction
*/
class MyLibNamsepace_Filter_Template extends Varien_Filter_Template
{
const CONSTRUCTION_IF_CONDITION_OPERANDS = "/([^\s=><!]+)\s*((?:>=)|(?:<=)|\>|<|(?:===)|(?:!==)|(?:==)|(?:!=)){1}\s*(.*+)/si";
/**
* Cunstruction logic regular expression
*/
const CONSTRUCTION_OUTER_IF_PATTERN = '/{{outer_if\s*(.*?)}}(.*?)({{outer_else}}(.*?))?{{\\/outer_if\s*}}/si';
const CONSTRUCTION_INNER_IF_PATTERN = '/{{inner_if\s*(.*?)}}(.*?)({{inner_else}}(.*?))?{{\\/inner_if\s*}}/si';
const CONSTRUCTION_DEPEND_PATTERN = '/{{depend\s*(.*?)}}(.*?){{\\/depend\s*}}/si';
const CONSTRUCTION_IF_PATTERN = '/{{if\s*(.*?)}}(.*?)({{else}}(.*?))?{{\\/if\s*}}/si';
/**
* Filter the string as template.
*
* @param string $value
* @return string
*/
public function filter($value)
{
// "inner_if" operand should be first
foreach (array(
self::CONSTRUCTION_OUTER_IF_PATTERN => 'ifExtendedDirective',
) as $pattern => $directive) {
if (preg_match_all($pattern, $value, $constructions, PREG_SET_ORDER)) {
foreach($constructions as $index => $construction) {
$replacedValue = '';
$expression = null;
if($pattern == self::CONSTRUCTION_OUTER_IF_PATTERN)
if (!preg_match(self::CONSTRUCTION_IF_CONDITION_OPERANDS, $construction[1], $expression))
continue;
$callback = array($this, $directive);
if(!is_callable($callback)) {
continue;
}
try {
$replacedValue = call_user_func($callback, $construction, $expression);
} catch (Exception $e) {
throw $e;
}
$value = str_replace($construction[0], $replacedValue, $value);
}
}
}
// "inner_if" operand should be first
foreach (array(
self::CONSTRUCTION_INNER_IF_PATTERN => 'ifExtendedDirective',
) as $pattern => $directive) {
if (preg_match_all($pattern, $value, $constructions, PREG_SET_ORDER)) {
foreach($constructions as $index => $construction) {
$replacedValue = '';
$expression = null;
if($pattern == self::CONSTRUCTION_INNER_IF_PATTERN)
if (!preg_match(self::CONSTRUCTION_IF_CONDITION_OPERANDS, $construction[1], $expression))
continue;
$callback = array($this, $directive);
if(!is_callable($callback)) {
continue;
}
try {
$replacedValue = call_user_func($callback, $construction, $expression);
} catch (Exception $e) {
throw $e;
}
$value = str_replace($construction[0], $replacedValue, $value);
}
}
}
// "depend" and "if" operands should be second
foreach (array(
self::CONSTRUCTION_DEPEND_PATTERN => 'dependDirective',
self::CONSTRUCTION_IF_PATTERN => 'ifDirective',
) as $pattern => $directive) {
if (preg_match_all($pattern, $value, $constructions, PREG_SET_ORDER)) {
foreach($constructions as $index => $construction) {
$replacedValue = '';
$expression = null;
if($pattern == self::CONSTRUCTION_IF_PATTERN)
if (preg_match(self::CONSTRUCTION_IF_CONDITION_OPERANDS, $construction[1], $expression))
$directive = "ifExtendedDirective";
$callback = array($this, $directive);
if(!is_callable($callback)) {
continue;
}
try {
if($expression != null)
$replacedValue = call_user_func($callback, $construction, $expression);
else
$replacedValue = call_user_func($callback, $construction);
} catch (Exception $e) {
throw $e;
}
$value = str_replace($construction[0], $replacedValue, $value);
}
}
}
if(preg_match_all(self::CONSTRUCTION_PATTERN, $value, $constructions, PREG_SET_ORDER)) {
foreach($constructions as $index=>$construction) {
$replacedValue = '';
$callback = array($this, $construction[1].'Directive');
if(!is_callable($callback)) {
continue;
}
try {
$replacedValue = call_user_func($callback, $construction);
} catch (Exception $e) {
throw $e;
}
$value = str_replace($construction[0], $replacedValue, $value);
}
}
return $value;
}
public function ifExtendedDirective($construction, $expression)
{
if (count($this->_templateVars) == 0) {
return $construction[0];
}
$expression[3] = trim($expression[3], "'");
$expression[3] = trim($expression[3], "\"");
switch ($expression[2]) {
case '>=':
$condition = ($this->_getVariable($expression[1], '') >= $expression[3]);
break;
case '<=':
$condition = ($this->_getVariable($expression[1], '') <= $expression[3]);
break;
case '>':
$condition = ($this->_getVariable($expression[1], '') > $expression[3]);
break;
case '<':
$condition = ($this->_getVariable($expression[1], '') < $expression[3]);
break;
case '===':
$condition = ($this->_getVariable($expression[1], '') === $expression[3]);
break;
case '!==':
$condition = ($this->_getVariable($expression[1], '') !== $expression[3]);
break;
case '==':
$condition = ($this->_getVariable($expression[1], '') == $expression[3]);
break;
case '!=':
$condition = ($this->_getVariable($expression[1], '') != $expression[3]);
break;
default:
$condition = false;
}
if($condition) {
return $construction[2];
} else {
if (isset($construction[3]) && isset($construction[4]))
return $construction[4];
else
return '';
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.