New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Ability to pass more than one variable data to a template #1611
Comments
You can pass an object to the template containing all the data you need. |
Yep, that's the workaround that is being done. If these are separate data why do we need to mutate to create a combined data before passing to handlebar. From the API these come as separate data and expected to be treated as separate. Once you look at the partial template and its data the problem becomes compounded #1612. |
Are you using Handlebars to render the complete page on the server, or do you build a single-page app, that fetches data from a rest-service and runs the template in the browser? Server-sideI don't think there is a good way to render parts of a page before all data is assembled. Once you start sending data to the client (i.e. when the first database-call has returned), you have no way to send an error response, because the response-headers have already been sent. Client-sideIf you need partial/incremental updates of the page and you are probably much better off using a framework like Angular, Ember, React or Vue. Handlebars is just a template-engine: It compiles a template to a function that generates an output string from an input object. It does not know about DOM and HTML. And it should not know about those things. |
I am using at the client side. So just like it combines different partials to form whole, it should have the corresponding concept for partial data to form the whole instead of expecting the data as a whole but the templating as parts. The ask is can handlebar take more than one data pointer similar to more than one template agnostic or what is done in the render. Once this is possible, then the usage could be enhanced to now say, you templated the whole using the parts. If a part needs to be templated again, one could ask the template engine to re-template only the part instead of the whole and get back the content. This in turn can be fed into the browser to actually render. In abstraction Similar concept in Data Template Request As Opposed to keeping everything as part without parent child Similar concept in Data Template Request Now we use other frameworks to combine in any way we want. |
The use case of partials is not to separate the whole template into three parts. You could just as well achieve this by creating multiple templates and executing them separately. The goal of partials is the same as the goal of functions in JavaScript: extract part of the template either to reuse it in other places, or simply to increase readability. I think you could have used something like this: https://jsfiddle.net/nmvuheLz/ But again: Once upon a time, Handlebars was a good choice to build a single-page app. It isn't anymore. The use case of Handlebars more shifted to the non-DOM rendering. It would be interesting to know which framework you use, though. |
In object orientation there is association which are composition and aggregation I am not sure why you keep bringing DOM here. Rendering is generic to render the template if I have used the wrong word then we can rename this to "output of a template run with data". The problem would exist even then. Currently, I am not using any framework. I have written the same using jquery and handlebar. I now doing exactly as you said, have a naming convention to create the composition relationship, get the data pass to the correct template, render the content and then attach to the parent. I have closed the related feature. This is a relatively simple feature to support more than one input field . Are you saying this too is long term? |
I was asking about the DOM because I felt you were going to use such a combination of jquery and handlebars and I wanted to strongly advise against it. When your application gets bigger, this is a dead end. You are always rerendering the whole page, it will be difficult to debug, because you're DOM-elements will disappear in the dev-tools even thoughbyou updating a completely part of the page. And you have to build things yourself, that you are getting for granted in other frameworks. |
Maybe I still don't understand what you actually expect from the feature. What I understood is that if you have a partials:
and a template {{>partial1}}
{{>partial2}}
{{>partial3}} then template({x: 1}, {x: 2}, {y: 3}) should evaluate to
If that isn't it, please correct me. But if this is what you mean, then I wouldn't put it on the roadmap at all, for the following reasons:
More explanation for 1 and 2:
{{> partial1 ...}}
{{> partial2 ...}}
{{> partial3 ...}} But you can also call partials in the loop of an What would you expect if you provide multiple parameters to this template: https://jsfiddle.net/jaLo28s4/
Each of these operations is something you could easily do in a preprocessing step, but including them in the core would make Handlebars more complicated. |
Not sure where to start. Let me take the one that makes logical sense to prove the point. In the example provided. The expected code should be From var input = { arr: ['x','y','z'], deepObject: {a: {a: {a: {b:1}}}}} document.getElementById('output').innerHTML = template(input); To const arrayInput = ['x','y','z']; const deepObject = {a: {a: {a: {b:1}}}}; document.getElementById('output').innerHTML = template(arrayInput, deepObject); Inside the template, there should be a way to access that there are multiple object passed and not just one object. With regard to the combination
This is again left to the caller on that single object. If in the first input they would like to do composite, next input they like array etc thats still left to the caller.
Interesting as the other feature that I wanted to request was the runtime options should be specific to the input number. Can you point to this documentation on how to pass the run time option to the passed input namely
Shall create a feature request for this. How are you saying that its not backward compatible with runtime-options in the documentation I don't see the template having more than one input. I do agree that the simplicity would be lost but what is the plan to take it to the next level. |
Runtime options are described in https://handlebarsjs.com/api-reference/compilation.html#handlebars-compile-template-options The resulting template function can be called as This could be presented more visibly in the documentation.
You can achieve exactly this by calling template like template({object1: object1, object2: object}) and using {{object1.property_x}}
{{object2.property_y}} in the template. This is event better, because you can name the objects like you want and you are not restricted to the "object"+counter notation. You can also use the template(mainObject, { data: { object1: object1, object2: object2}}) and in the template call {#with $object1}}...{{/with}}
{{#with $object2}}
{{property_y}}
{{$object1.property_x}}
{{/with}} I haven't tested this and I am not sure if it is documented, but it should work.
We actually do have a no-new-feature policy at the moment (#1277). If anyone suggests features In some cases, features appear to be simple to implement and difficult to implement otherwise (like #1584). I think the multiple-input scheme would make things much more complicated in the core, and I have shown multiple ways to achieve the same goal. Unless a lot of other people also want something like that, I would like to close this issue. |
Thanks for continuing the conversation. object: { var1Name: var1, var2Name: var2 } template(object); Though this can be done. I don't agree to this one size fits all. |
Thanks for the answer. I will close this now |
For rendering a form view the following is required at a minimum if the view is a dynamic view.
The actual schema of the fields namely a string to textbox mapping e.g FirstName which is a String rendered as TextBox, let's call this schemaData.
The data to go into the field namely for the same FirstName the value of Steve, let's call this data.
Any reference data namely for a field called State which is of type Reference a Select Box with the data coming from a State reference collection having different values for TX, NY, NJ etc let's call this statesRefData.
Though these can be rendered as partials. The data to the parent template should be a combined one as opposed to multiple data sets namely
Template(schemaData, data, statesRefData)
Within the template one should be able to reference the input param and the fields within the input param.
The text was updated successfully, but these errors were encountered: