XaminProject / handlebars.php

Handlebars processor for php
331 stars 134 forks source link

Support HTML Attribute/Value style arguments #44

Closed ryan-mahoney closed 9 years ago

ryan-mahoney commented 10 years ago

I like to use my helpers like this:

{{#SomeHelper format="xyz" value="123"}}

but... there is not convenient way to access these values... until now ;)

$args = $template->parseTagAttributes($args);

everplays commented 10 years ago

Thanks Ryan, please consider the following:

I like to use my helpers like this:

{{#SomeHelper format="xyz" value="123"}}

but... there is not convenient way to access these values... until now ;)

$args = $template->parseTagAttributes($args);

You can merge this Pull Request by running

git pull https://github.com/Opine-Org/handlebars.php master

Or view, comment on, or merge it at:

https://github.com/XaminProject/handlebars.php/pull/44 Commit Summary

  • parse HTML style attributes and values
  • typo

File Changes

  • M src/Handlebars/Template.phphttps://github.com/XaminProject/handlebars.php/pull/44/files#diff-0(29)

Patch Links:

— Reply to this email directly or view it on GitHubhttps://github.com/XaminProject/handlebars.php/pull/44 .

ryan-mahoney commented 10 years ago

Sorry -- didn't intend to push my composer change, that was for my own testing. Re: other changes, I will go ahead and do so. Thanks!

everplays commented 10 years ago

@virtuecenter could you rebase this PR to remove the extra commits and write tests? or I shall finish it myself?

jslegers commented 10 years ago

Consider the following alternate syntaxes for achieving the same flexibility.

{{#SomeHelper format=xyz&value=123}}
{{#SomeHelper format=xyz value=123}}
{{#SomeHelper format=xyz;value=123}}
{{#SomeHelper format=xyz,value=123}}
{{#SomeHelper format=xyz-value=123}}

Advantages

Demo

function parse_str_separator($str, &$array, $separator = '&') {
    if ($separator === '&') {
        parse_str($str, $array);
    } else {
        $array = array();
        preg_match_all('@([^=]+)=([^\\' . $separator . ']*)' . $separator . '?\\s?@', $str, $matches, PREG_SET_ORDER);
        foreach ($matches as $m) {
            $array[$m[1]] = $m[2];
        }
    }
}

$query = http_build_query(array('format' => 'xyz', 'value' => '123'));
var_dump($query);
parse_str_separator($query, $arr);
var_dump($arr);

$separator = ' ';
$query = http_build_query(array('format' => 'xyz', 'value' => '123'), '', $separator);
var_dump($query);
parse_str_separator($query, $arr, $separator);
var_dump($arr);

$separator = ';';
$query = http_build_query(array('format' => 'xyz', 'value' => '123'), '', $separator);
var_dump($query);
parse_str_separator($query, $arr, $separator);
var_dump($arr);

$separator = '-';
$query = http_build_query(array('format' => 'xyz', 'value' => '123'), '', $separator);
var_dump($query);
parse_str_separator($query, $arr, $separator);
var_dump($arr);

$separator = ',';
$query = http_build_query(array('format' => 'xyz', 'value' => '123'), '', $separator);
var_dump($query);
parse_str_separator($query, $arr, $separator);
var_dump($arr);

Output

string(20) "format=xyz&value=123"
array(2) {
  ["format"]=>
  string(3) "xyz"
  ["value"]=>
  string(3) "123"
}
string(20) "format=xyz value=123"
array(2) {
  ["format"]=>
  string(3) "xyz"
  ["value"]=>
  string(3) "123"
}
string(20) "format=xyz;value=123"
array(2) {
  ["format"]=>
  string(3) "xyz"
  ["value"]=>
  string(3) "123"
}
string(20) "format=xyz-value=123"
array(2) {
  ["format"]=>
  string(3) "xyz"
  ["value"]=>
  string(3) "123"
}
string(20) "format=xyz,value=123"
array(2) {
  ["format"]=>
  string(3) "xyz"
  ["value"]=>
  string(3) "123"
}
jslegers commented 10 years ago

See also https://github.com/XaminProject/handlebars.php/issues/68 for a broader perspective on these syntaxes and even more alternate syntaxes.

JustBlackBird commented 10 years ago

@jslegers, you forgot one important thing. Neither of the following constructions

  {{#SomeHelper format=xyz&value=123}}
  {{#SomeHelper format=xyz value=123}}
  {{#SomeHelper format=xyz;value=123}}
  {{#SomeHelper format=xyz,value=123}}
  {{#SomeHelper format=xyz-value=123}}

are supported by Handlebars.js. The way it uses for complex arguments is described here: http://handlebarsjs.com/block_helpers.html#hash-arguments

jslegers commented 10 years ago

@JustBlackBird :

Neither of the following constructions are supported by Handlebars.js.

Good point. I didn't realize Handlebars.js already supported the syntax nav id="nav-bar" class="top"

The way it uses for complex arguments is described here: http://handlebarsjs.com/block_helpers.html#hash-arguments

There are some limitations to this syntax, though, which I addressed at https://github.com/XaminProject/handlebars.php/issues/68. Nevertheless, I guess it's better to address this to the Handlebars.js support team as the XaminProject/handlebars.php is essentially still just a PHP port of Handlebars.js.

everplays commented 9 years ago

It's implemented by #75, so I'm closing this one.