Passing variables from twig to component

In a Twig .htm partial

<div class="sidebar-widget">
   {% component "postBlock" score=55 name="Johan" %}
   <div class="nonsense meta">
       {% partial 'nonsense' score=75 name="Marga" %}

In the nonsense twig that is a child of the previous .htm partial

<div class="sidebar-element">
   <div>My name is: {{ name|default('John Doe') }}</div>
   <div>My score is: {{ score|default('No score') }}</div>

In the component postBlock, onRender function

function onRender() {
   $score = $this->property('score');  // 55
   $name = $this->property('name'); // Johan

To pass from a parent twig to a child twig, you don't have to use the onRender function in the component, only if you want to modify things and pass is from there to the twig.

Last update: Tue, 13 Sep 2022 14:32:15