Skip to content

Added support for inline partials. People that are more familiar with… - #221

Merged
rexm merged 4 commits into
Handlebars-Net:masterfrom
mikeprince3:master
Aug 25, 2017
Merged

rexm merged 4 commits into
Handlebars-Net:masterfrom
mikeprince3:master

Conversation

@mikeprince3

Copy link
Copy Markdown
Contributor

… the codbase should look at some of the hackier bits. I left comments to explain the thinking in the code.

The template below can be used as a simple test. I have done some pretty complicated nesting of inline partials with parameters and different contexts and everything seems to work pretty well. I'm sure some aspects of this can and should be improved so please feel free to put things where they should be.

{{#*inline "inlineTest"}}
hello inline partial
{{/inline}}
{{> inlineTest}}

… the codbase should look at some of my hackier bits. I left comments to explain the thinking
@rexm

rexm commented Aug 13, 2017

Copy link
Copy Markdown
Member

This is a pretty effective solution. Only 1 aspect gives me discomfort... the empty built-in helper. What problem does that solve? Otherwise, it seems fairly straightforward. In terms of the base syntax, an inline partial more or less is a block helper, so this approach makes sense. We do need some unit test coverage to call it complete, however.

@mikeprince3

mikeprince3 commented Aug 15, 2017 •

Copy link
Copy Markdown
Contributor Author

I don't like the empty helper either but it seemed to help side-step understanding a big chunk of code. Basically, without the empty helper, some aspect of the code would throw an error regarding not knowing what the "*inline" block was for.

I also suspect the addition to the BlockHelperFunctionBinder.VisitBlockHelperExpression is a bit of a hack too. I'll admit to being new Expressions too so this was a simple way for me to get it working without hitting that learning curve.

Given your more thorough knowledge of the code base, if you can provide some guidance as to how to accomplish the same things, I am happy to take another shot at it and add some tests as well.

@yringler

Copy link
Copy Markdown

+1. This is a super awesome and super useful; any template of size is much more maintainable when broken to components, and it's nice not to have to clutter the file system with a handful of partials and the c# code with all the registering.
I bumped into this thread when I realized that this wasn't already supported.
Hope this is released to NuGet soon.

@rexm

rexm commented Aug 22, 2017 •

Copy link
Copy Markdown
Member

I think if you can add some unit tests, this is good enough to go as-is. It works and after thinking about the approach more, I dig the simplicity because inline partials are syntactically a weird kind of block helper.

@mikeprince3

Copy link
Copy Markdown
Contributor Author

While testing, it turned out that using Handlebars.RegisterTemplate would pollute the static space and could collide with other templates that were compiled with the same instance. This was only an issue with Handlebars.Compile. It worked fine with Handlebars.Create. That said, it seemed more appropriate that the inline partials should be scoped to the compiled template and nothing else. To do this, I added a collection of compiled inline partials to pass around with the BindingContext.

I duplicated the existing Partial test into InlinePartial tests and everything is passing.

@mikeprince3

Copy link
Copy Markdown
Contributor Author

On a side note... thanks for putting this project together. It is solving some problems I have in a very good way. Great code.

@mikeprince3

Copy link
Copy Markdown
Contributor Author

Along the lines of the static RegisterTemplate issue, using a new instance per compile would solve the scoping issue created by the inline partials and simplify the commit quite a bit. Instead of passing around the InlinePartials in the context, I could just add them to the config. I chose to scope them is because I suspect many people (myself included) use the convenience methods listed on the readme.md without realizing the issue. I'm good either way. lmk

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants