You can write a custom twig extension with a function that uses the PropertyAccess component of symfony to retrieve the value. An example extension implementation can be Like:
<?php
use Symfony\Component\PropertyAccess\PropertyAccess;
class PropertyAccessorExtension extends \Twig_Extension
{
/** @var PropertyAccess */
protected $accessor;
public function __construct()
{
$this->accessor = PropertyAccess::createPropertyAccessor();
}
public function getFunctions()
{
return array(
new \Twig_SimpleFunction('getAttribute', array($this, 'getAttribute'))
);
}
public function getAttribute($entity, $property) {
return $this->accessor->getValue($entity, $property);
}
/**
* Returns the name of the extension.
*
* @return string The extension name
*
*/
public function getName()
{
return 'property_accessor_extension';
}
}
After registering this extension as service, you can then call
{% for entity in array %}
{{ getAttribute(entity, "child.child.prop1") }}
{% endfor %}
Happy coding!