Mustache: read variables from parent section in child section

Mustache doesn't allow you to refer to parent objects. Any data you want to display while within the child section needs to be contained in the child array.

For example:

$order_store => array(
array(
    'id' => 1,
    'name' => 'Kyriena Cookies',
    'shipping_method' => array(
        array(
            'id' => 1,
            'name' => 'Poslaju',
            'description' => 'Poslaju courier',
            'order_store_id' => '1'
        ),
        array(
            'id' => 2,
            'name' => 'SkyNET',
            'description' => 'Skynet courier',
            'order_store_id' => '1'
        ),
    ),
));

Then you can use the tag {{order_store_id}}.

Dot notation wouldn't help in this case -- it won't magically give you access to the parent array. (By the way, dot notation isn't supported by all mustache parsers, so it's probably best to avoid using it if there's any chance you'll want to reuse your templates with another programming language in the future.)


If the template is to be compiled on the client side, another option is to use HandlebarsJS templates, which are compatible with Mustache, and use the parent notation:

{{../order_store.id}}

Tags:

Php

Mustache