XaminProject / handlebars.php

Handlebars processor for php
331 stars 134 forks source link

Adding complex dynamic data to helpers and sub-templating from custom helpers #68

Closed jslegers closed 9 years ago

jslegers commented 10 years ago

I'm currently experimenting with the following features.

Sub-templating

Syntax :

<div class="site-body">
    <div class="site-center">
        <div class="cell">
            {{{template panel this name}}}
        </div>
    </div>
</div>

Explanation :

The problem with the syntax {{{template panel this name}}} is that it's rather restrictive in naming of parameters that are past to a subtemplate (or other helper).

To allow more custom naming, alternate syntaxes could be :

{{{template panel data=this&name=John}}}
{{{template panel data=this name=John}}}
{{{template panel data=this;name=John}}}
{{{template panel data=this,name=John}}}
{{{template panel data=this-name=John}}}

See also https://github.com/XaminProject/handlebars.php/pull/44

The problem with these syntaxes, is distinguishing between what content should passed literally (eg. John) and what content should passed as a reference (eg. this). Variations on this syntax that add this distinction would be :

{{{template panel data=this&name="John"}}}
{{{template panel data=this&name='John'}}}
{{{template panel data={{this}}&name=John}}}
{{{template panel data=$this&name=John}}}
{{{template panel data=${this}&name=John}}}

As any content in between a beginning and end tag remains unparsed for custom helpers, there are nevertheless still many alternatives worth considering :

{{#template panel}}
<input type="array" name="data" value="{{this}}" />
<input type="string" name="name" value="John" />
<output type="string" name="panel" />
<!-- This syntax would even allow you to store the output -->
<!-- of the template as a variable named panel -->
{{/template}}
{{#template panel}}
<parameters>
    <array data="{{this}}" />
    <string name="John" />
</parameters>
{{/template}}
{{#template panel}}
<input>
    <array data="{{this}}" />
    <string name="John" />
</input>
<output>
    <string panel />
</output>
{{/template}}
{{#template panel}}
<template>
    <parameter data="{{this}}" />
    <parameter name="John" />
</template>
{{/template}}
{{#template panel}}
data : (array) {{this}}
name : (string) John
{{/template}}
{{#template panel}}
data = {{this}}
name = John
{{/template}}

Would you be interested in adding any of these syntaxes to the XaminProject/handlebars.php library? I am still considering which syntax to use for my own PowerTools project and I figured I might as well contribute the syntax I eventually choose to the XaminProject/handlebars.php project if anyone is interested.


EDIT :

See also https://github.com/wycats/handlebars.js/issues/817

JustBlackBird commented 10 years ago

@jslegers, the syntax, you proposed, is not compatible with Handlebars.js. I think there is no reason to implement a new complex arguments passing syntax. It is a step away from compatibility between PHP and JS versions of Handlebars.

jslegers commented 10 years ago

@JustBlackBird :

That's a totally valid point.

I guess this issue should be raised at the handlebars.js community, so I did -> https://github.com/wycats/handlebars.js/issues/817

everplays commented 9 years ago

as main implementation rejected this proposal, I am closing this one too.