dustjs icon indicating copy to clipboard operation
dustjs copied to clipboard

[discussion] dust.renderTo

Open sethkinast opened this issue 9 years ago • 8 comments

@jimmyhchan would like a function called dust.renderTo:

dust.renderTo = function(tmpl, ctx, el){}

sethkinast avatar May 26 '15 21:05 sethkinast

I don't like it much and seems to be an request out of needing to statically analyze rather than a reasonable feature.

There are a few things to consider though:

  1. While we are supposed to be agnostic of what we actually output, the percentage of people using Dust as an HTML outputter is likely very high.
  2. It's backwards compatible. You don't have to use the function if you don't need it.
  3. We could just break it out into it's own module, so that people that don't want to use HTML specific functions don't get the extra weight.

prashn64 avatar May 27 '15 04:05 prashn64

If it's its own module that's effectively a monkeypatch.

I think that it doesn't seem that useful because it's basically just sugar for one possible usecase, and it doesn't even work on the server like the rest of Dust.

sethkinast avatar May 27 '15 04:05 sethkinast

I actually like the idea of a renderTo method. Though it is just sugar, I think it's really nice sugar.

dust.renderTo(tmpl, ctx, el).then(() => attachEventListeners);

That would be pretty cool.

smfoote avatar May 27 '15 14:05 smfoote

Use cases:

  • DRYing Sugar: developers repeat the callback block often.
  • an abstracted (centralized) method to attachHTML so that it can be tweaked by the environment: e.g. use innerHTML vs $.html vs other fanciness
  • Not thinking in strings makes it safer and more forward facing towards tornado where we start expecting DOM nodes instead of strings

Issues:

  • IE8 innerHTML needs the html5shiv

TODO: flush this out better

jimmyhchan avatar May 27 '15 15:05 jimmyhchan

@smfoote Dust won't support Promises like that for quite a long time.

Although that means that renderTo probably needs a callback arg too, gross.

@jimmyhchan html5shiv is the implementor's problem, not Dust's.

sethkinast avatar May 27 '15 18:05 sethkinast

I know it won't, but it would be pretty great.

smfoote avatar May 28 '15 02:05 smfoote

What about chaining, so I do the render as normal and then "pipe" it somewhere?

Keep the existing dust.render semantics as is, and add:

dust.render(tmpl, ctx, function(err, out) { ... optional ...}).toElement('#foo');

sethkinast avatar May 28 '15 18:05 sethkinast

That seems reasonable

smfoote avatar May 28 '15 18:05 smfoote