Handlebars.Net icon indicating copy to clipboard operation
Handlebars.Net copied to clipboard

Getting the content of a block helper

Open breenyoung opened this issue 10 years ago • 8 comments

In handlebarsjs you can use this keyword to retrieve the content between the block helper if you want to do processing on it. What' s the equivalent in our dotnet version?

this gives me back the instance of HandlebarsBlockHelper

{{bold}}Rex{{/bold}}

return new HandlebarsBlockHelper( (writer, options, context, parameters) => { // Access 'Rex' to bold? } );

breenyoung avatar Mar 15 '16 12:03 breenyoung

That's not currently supported, but PR welcome of course. The original text of the template is currently thrown away during the process of converting from tokens to an expression tree.

Sent from my iPhone

On Mar 15, 2016, at 08:56, Breen [email protected] wrote:

In handlebarsjs you can use this keyword to retrieve the content between the block helper if you want to do processing on it. What' s the equivalent in our dotnet version?

this gives me back the instance of HandlebarsBlockHelper

{{bold}}Rex{{/bold}}

return new HandlebarsBlockHelper( (writer, options, context, parameters) => { // Access 'Rex' to bold? } );

— You are receiving this because you are subscribed to this thread. Reply to this email directly or view it on GitHub: https://github.com/rexm/Handlebars.Net/issues/105

rexm avatar Mar 15 '16 13:03 rexm

Ah thanks for the speedy reply @rexm !

I'm a little busy with work atm, but I will try and pull this down and take a look into it.

breenyoung avatar Mar 15 '16 13:03 breenyoung

Longer answer - the point at which we still have a sequence of tokens, we can reconstitute the entire or any part of the text template* - each token can reproduce the text equivalent of itself - but once we start converting from tokens to expressions, the expressions do not store any context to say what combination of tokens (or text) were converted or collapsed into the resulting expression. So by the time we get from expression tree to compiled function, the context is long-gone.

*but any unusual whitespace is not preserved

rexm avatar Mar 25 '16 23:03 rexm

I think the OP misunderstood how HandlebarsJS works. Keyword this inside block helper function would return current context. There is no way to access original uncompiled template in HandlebarsJS.

Thus I think this issue can be closed.

imsedim avatar Jun 08 '16 07:06 imsedim

Closing this due to inactivity, and @imsedim indicating this is not actually a feature of handlebarsjs. Can reopen if this situation changes.

rexm avatar Aug 23 '16 16:08 rexm

Bumping because I recently needed this and it is actually supported in HandlebarsJS: https://github.com/wycats/handlebars.js/issues/559

The referenced example in that issue is a markdown block helper.

seanamosw avatar Aug 23 '18 00:08 seanamosw

Browsing the HandlebarsJS docs, I don't think one of the early block helper examples is possible (yet) with Handlebars.Net:

Handlebars.registerHelper('bold', function(options) {
  return new Handlebars.SafeString(
      '<div class="mybold">'
      + options.fn(this)
      + '</div>');
});

seanamosw avatar Aug 23 '18 00:08 seanamosw

PR welcome!

rexm avatar Aug 23 '18 02:08 rexm