fix for #2236 + add new |nested filter

This commit is contained in:
Andy Miller
2018-10-26 12:56:07 -06:00
parent adddcb4ff4
commit ec4504e017
3 changed files with 27 additions and 1 deletions

View File

@@ -1,3 +1,11 @@
# v1.8.13
## mm/dd/2018
1. [](#new)
* Added new `|nested()` Twig filter to access array objects with dot notation syntax
1. [](#bugfix)
* Fixed issue with complex lists structure and nested dot-notation [#2236](https://github.com/getgrav/grav/issues/2236)
# v1.8.12
## 10/24/2018

View File

@@ -31,6 +31,7 @@ class AdminTwigExtension extends \Twig_Extension
new \Twig_SimpleFilter('toYaml', [$this, 'toYamlFilter']),
new \Twig_SimpleFilter('fromYaml', [$this, 'fromYamlFilter']),
new \Twig_SimpleFilter('adminNicetime', [$this, 'adminNicetimeFilter']),
new \Twig_SimpleFilter('nested', [$this, 'nestedFilter']),
];
}
@@ -42,6 +43,23 @@ class AdminTwigExtension extends \Twig_Extension
];
}
public function nestedFilter($current, $name)
{
$path = explode('.', trim($name, '.'));
foreach ($path as $field) {
if (is_object($current) && isset($current->{$field})) {
$current = $current->{$field};
} elseif (is_array($current) && isset($current[$field])) {
$current = $current[$field];
} else {
return null;
}
}
return $current;
}
public function cloneFunc($obj)
{
return clone $obj;

View File

@@ -77,7 +77,7 @@
{% set childName = itemName -%}
{%- elseif childName starts with '.' -%}
{% set childKey = childName|trim('.') %}
{% set childValue = val[childName[1:]] %}
{% set childValue = val|nested(childName) %}
{% set childName = itemName ~ childName %}
{% else %}
{% set childKey = childName %}