Skip to content
This repository has been archived by the owner on Jan 26, 2019. It is now read-only.

Handlebars.js like helpers #74

Open
JustBlackBird opened this issue Aug 19, 2014 · 6 comments
Open

Handlebars.js like helpers #74

JustBlackBird opened this issue Aug 19, 2014 · 6 comments
Milestone

Comments

@JustBlackBird
Copy link
Contributor

At the moment writing custom helpers is complicated for users who does not familiar with internal architecture of Handlebars.php. One should manually parse arguments, gets them from the context, render nested blocks, works with else section and so on.

Is there any chance a simple, Handlebars.js like, interface will be implemented for helpers?

@everplays
Copy link
Contributor

You are right, it is too much work on developer to get a helper work the way they want it. However, I don't think neither me nor @fzerorubigd be able to work on this in near future. Yet, I can imagine that the result would look like this:

helper functions receive one argument (instead of three) which could be an instance of a class named \Handlebars\HelperArguments (name thing needs more thinking). By this class, developer can access the parsed arguments or render the content of block by calling a simple method.

Any suggestions?

@JustBlackBird
Copy link
Contributor Author

I suggest to use helpers signature from handlebars.js. Namely:

function helper($arg1, $arg2, ..., $argn, $options) {
    return 'an output';
}

@everplays, what do you think about backward compatibility?

@everplays
Copy link
Contributor

Yeah, that makes our API as close as possible to main implementation.

We can either drop the BC or use reflection to find out if the last
parameter is what we are going to use as equivalent of js' options
parameter.
On Aug 19, 2014 4:01 PM, "Dmitriy S. Simushev" [email protected]
wrote:

I suggest to use helpers signature from handlebars.js. Namely:

function helper($arg1, $arg2, ..., $argn, $options) {
return 'some helpers result';}

@everplays https://github.com/everplays, what do you think about
backward compatibility?


Reply to this email directly or view it on GitHub
#74 (comment)
.

@fzerorubigd
Copy link
Contributor

We can keep the old style helpers too. For example another interface or
function.

On Tue, Aug 19, 2014 at 4:01 PM, Dmitriy S. Simushev <
[email protected]> wrote:

I suggest to use helpers signature from handlebars.js. Namely:

function helper($arg1, $arg2, ..., $argn, $options) {
return 'some helpers result';}

@everplays https://github.com/everplays, what do you think about
backward compatibility?


Reply to this email directly or view it on GitHub
#74 (comment)
.


http://cyberrabbits.net

@JustBlackBird
Copy link
Contributor Author

I've thought more about the new helpers style. It seems that

$helper = function ($positional_args, $options) {
  // Helper's body
}

style is the most comfortable for PHP. Here $options is an array with the following structure:

$options = array(
  // Named arguments
  'hash' => array(),
  // A renderer for block helper
  'fn' => function($context) {},
  // A render for the else block
  'inverse' => function($context) {},
  // The current context.
  // Just associative array with resolved variables without all that stuff from \Handlebars\Context
  'context' => array(),
);

All arguments are already parsed and resolved. Thus the helper should know nothing about the arguments parsing and resolving.

I could try to implement it and send a pull request. But I cannot and don't want to keep backward compatibility. Thus, as I think, the next major version should be released with this changes.

@fzerorubigd, @everplays, what do you think about it?

@fzerorubigd
Copy link
Contributor

Sorry for delay, @everplays is in imigration process (Goodluck dude :) ) And I have another pile of task in my issue tracker :)

As I said before, backward compatibility is important, but we can break things when there is no another option.
I create another branch, please use that branch for PR. When every thing is done, we can move development to master branch again, with a tag.

Thank you

@fzerorubigd fzerorubigd added this to the v0.11.0 milestone Dec 12, 2014
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants