XaminProject / handlebars.php

Handlebars processor for php
331 stars 132 forks source link

How to use #invoke recursively inside #define helper? #169

Closed kirtanbodawala closed 7 years ago

kirtanbodawala commented 7 years ago

In the following example, I have multiple menus with submenu and that submenu also has the submenu.

{{#define render_menu}}
    <li>
        {{label}}
        {{#if submenu}}
            <ul>
                {{#each submenu}}
                        {{#invoke render_menu}}
                {{/each}}
            </ul>

        {{/if}}
    </li>
{{/define}}
<ul>
    {{#each menu.fronthero}}
        {{#invoke render_menu}}
    {{/each}}
</ul>
everplays commented 7 years ago

I don't believe that either define or invoke are implemented in handlebars.php. You can either implement it yourself or define it as a helper and use partials to achieve what you want.

kirtanbodawala commented 7 years ago

Got the solution..! To make invoke work inside of the define helper you have clone the define and inserted it into invoke. Just add below lines inside Helpers.php in helperInvoke function:

$this->tpl["INVOKE"][$args] = clone($this->tpl["DEFINE"][$args]);
        if ($this->tpl["INVOKE"][$args]){
            return $this->tpl["INVOKE"][$args]->render($context);
        }