knockout / knockout

Knockout makes it easier to create rich, responsive UIs with JavaScript
http://knockoutjs.com/
Other
10.45k stars 1.52k forks source link

Custom HTML elements in templates #514

Closed andrewdavey closed 12 years ago

andrewdavey commented 12 years ago

This is an idea for a future version of knockout, or perhaps a plugin, which would reduce the amount of boilerplate HTML in templates. For example, when using Twitter Bootstrap this is the HTML for a form field:

<div class="control-group">
    <label class="control-label">First name</label>
    <div class="controls">
        <input type="text" data-bind="value: firstName" />
    </div>
</div>

It would be interesting if I could instead write:

<controlGroup label="First name">
    <input type="text" data-bind="value: firstName" />
</controlGroup>

And somehow register a custom HTML element with the template engine so it knows to expand controlGroup elements.

Thoughts?

mbest commented 12 years ago

You could probably create custom bindings combined with a custom binding handler to do this. I don't think this is something we'll want to include in Knockout though.

rniemeyer commented 12 years ago

This sounds kind of interesting, but probably best left to a plugin. As Michael said, you could use custom bindings that expand out into more complex HTML or you could even use a custom binding provider that looks for certain elements and replaces them with additional elements before doing the binding.

SteveSanderson commented 12 years ago

Interesting idea, and you can in fact do roughly that with a custom binding. For example,

... where "controlgroup" is a custom binding that adds markup around its contents.

I'm not sure how custom elements (e.g., ) would best be implemented. I guess theoretically, KO could let you register the names of some custom elements and then bind them with a particular binding even if they don't have any data-bind attribute. You could implement that with a custom binding provider.

On 6 June 2012 14:55, Andrew Davey < reply@reply.github.com

wrote:

This is an idea for a future version of knockout, or perhaps a plugin, which would reduce the amount of boilerplate HTML in templates. For example, when using Twitter Bootstrap this is the HTML for a form field:

<div class="control-group">
   <label class="control-label">First name</label>
   <div class="controls">
       <input type="text" data-bind="value: firstName" />
   </div>
</div>

It would be interesting if I could instead write:

<controlGroup label="First name">
   <input type="text" data-bind="value: firstName" />
</controlGroup>

And somehow register a custom HTML element with the template engine so it knows to expand controlGroup elements.

Thoughts?


Reply to this email directly or view it on GitHub: https://github.com/SteveSanderson/knockout/issues/514

SteveSanderson commented 12 years ago

Oh, apologies to Michael and Ryan - I posted my response directly from email so I didn't realise you guys had already answered.

andrewdavey commented 12 years ago

Thanks for the responses guys. I agree this would work best as a separate plugin for knockout.

I modified the native template engine yesterday with moderate success. I altered renderTemplateSource to scan the template nodes and replace the custom elements with their expanded forms. If I get some time I'll try to at least publish some sample code and a write-up.