There are three methods for that:
object.attribute
object['attribute']
attr(object, 'attribute')
How are these different from each other?
There are three methods for that:
object.attribute
object['attribute']
attr(object, 'attribute')
How are these different from each other?
{{ entry.title }}
compiles down to:
echo twig_escape_filter($this->env, craft\helpers\Template::attribute($this->env, $this->source, (isset($context["entry"]) || array_key_exists("entry", $context) ? $context["entry"] : (function () { throw new RuntimeError('Variable "entry" does not exist.', 19, $this->source); })()), "title", []), "html", null, true);
...and
{{ entry['title'] }}
compiles down to:
echo twig_escape_filter($this->env, craft\helpers\Template::attribute($this->env, $this->source, (isset($context["entry"]) || array_key_exists("entry", $context) ? $context["entry"] : (function () { throw new RuntimeError('Variable "entry" does not exist.', 20, $this->source); })()), "title"), "html", null, true);
...and
{{ attribute(entry, 'title') }}
compiles down to:
echo twig_escape_filter($this->env, craft\helpers\Template::attribute($this->env, $this->source, (isset($context["entry"]) || array_key_exists("entry", $context) ? $context["entry"] : (function () { throw new RuntimeError('Variable "entry" does not exist.', 21, $this->source); })()), "title", [], "array"), "html", null, true);
If they look similar, that's because... they are identical in terms of the PHP code they compile down to.
So the difference is... whichever one uses the fewest keystrokes to type.
entry.dwfgfrg is definedreturns true, butentry['dwfgfrg'] is definedreturns false. – Piotr Pogorzelski Mar 04 '21 at 17:23entryis properly defined, and it's only theattributethat is missing. – Lindsey D Mar 05 '21 at 17:50