-
Notifications
You must be signed in to change notification settings - Fork 9.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Enabling the use of looping (for in ..) into Template.php #9401
Changes from 2 commits
7717a56
3026960
18ed2eb
e77cedf
6da53be
a69e9cd
104e664
28e9d61
9111eb5
37f49b7
28ec3f8
8fd5123
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,11 @@ class Template implements \Zend_Filter_Interface | |
const CONSTRUCTION_IF_PATTERN = '/{{if\s*(.*?)}}(.*?)({{else}}(.*?))?{{\\/if\s*}}/si'; | ||
|
||
const CONSTRUCTION_TEMPLATE_PATTERN = '/{{(template)(.*?)}}/si'; | ||
|
||
/** | ||
* Looping regular expression | ||
*/ | ||
const LOOP_PATTERN = '/{{loop(.*?)delimiter=(.*?)}}(.*?){{\/loop}}/si'; | ||
|
||
/**#@-*/ | ||
|
||
|
@@ -131,6 +136,8 @@ public function filter($value) | |
} | ||
} | ||
|
||
$value = $this->_filterLoop($value); | ||
|
||
if (preg_match_all(self::CONSTRUCTION_PATTERN, $value, $constructions, PREG_SET_ORDER)) { | ||
foreach ($constructions as $construction) { | ||
$callback = [$this, $construction[1] . 'Directive']; | ||
|
@@ -371,4 +378,59 @@ protected function getStackArgs($stack) | |
} | ||
return $stack; | ||
} | ||
|
||
/** | ||
* Filter the string as template. | ||
* | ||
* @param string $value | ||
* @return string | ||
*/ | ||
protected function _filterLoop($value) | ||
{ | ||
if (preg_match_all(self::LOOP_PATTERN, $value, $constructions, PREG_SET_ORDER)) { | ||
foreach ($constructions as $construction) { | ||
|
||
$full_text_to_replace = $construction[0]; | ||
$objectArrayData = $this->_getVariable($construction[1], ''); | ||
$delimiter = $construction[2]; | ||
$loop_text_to_replace = $construction[3]; | ||
|
||
if (is_array($objectArrayData) || $objectArrayData instanceof Varien_Data_Collection) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You should allow more than arrays and object collections in here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Applied :) |
||
|
||
$loopText = []; | ||
foreach ($objectArrayData as $k => $objectData) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why do we need There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is not needed. Will be removed. |
||
|
||
if (!$objectData instanceof Varien_Object) { // is array? | ||
|
||
if (!is_array($objectData)) { | ||
continue; | ||
} | ||
|
||
$_item = new Varien_Object(); | ||
$_item->setData($k, $objectData); | ||
$objectData = $_item; | ||
} | ||
|
||
$this->_templateVars['item'] = $objectData; | ||
|
||
if (preg_match_all(self::CONSTRUCTION_PATTERN, $loop_text_to_replace, $attributes, PREG_SET_ORDER)) { | ||
|
||
$subText = $loop_text_to_replace; | ||
foreach ($attributes as $attribute) { | ||
$text = $this->_getVariable($attribute[2], ''); | ||
$subText = str_replace($attribute[0], $text, $subText); | ||
} | ||
$loopText[] = $subText; | ||
} | ||
unset($this->_templateVars['item']); | ||
|
||
} | ||
$replaceText = implode($delimiter, $loopText); | ||
$value = str_replace($full_text_to_replace, $replaceText, $value); | ||
} | ||
} | ||
} | ||
|
||
return $value; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please use CamelCase style for variable names
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok.