ractivejs / ractive

Next-generation DOM manipulation
http://ractive.js.org
MIT License
5.94k stars 397 forks source link

Is there a Ractive.compile like Handlebars.compile? #674

Closed jimmyye closed 10 years ago

jimmyye commented 10 years ago

Sometimes I just want the html and don't want to new a Ractive.

var source  = $("#template").html();
var template = Handlebars.compile(source);
var context = {...}
var html  = template(context);
jimmyye commented 10 years ago

Or partial as attr value:

<div rel="popover"
    data-html="true"
    data-content="{{>profile}}">
  ...
</div>
martypdx commented 10 years ago

You can creat new Ractive({...}) without el and call .toHTML(). See http://docs.ractivejs.org/latest/ractive-tohtml

jimmyye commented 10 years ago

Sure. Though I think the .compile way is more like others and more compact to use and reuse (other than ractive.reset).

How about "partial as attr value"? Will this be implemented in the future?

martypdx commented 10 years ago

Ractive is more than string parser, so not quite the same, but if you like:

Ractive.compile = function(template){
    var parsed = Ractive.parse(template)
    return function( context ) {
        return new Ractive({
            template: parsed,
            data: context
        }).toHTML()
    }
}

var source  = $("#template").html();
var template = Ractive.compile(source);
var context = {...}
var html  = template(context);

How about "partial as attr value"? Will this be implemented in the future?

Seems reasonable that that should work.

jimmyye commented 10 years ago

Thanks. Looking forward to it. :)

martypdx commented 10 years ago

@jimmyye it looks like partials in attributes now works in edge. @Rich-Harris was that a byproduct of VirtualDom refactor?

jimmyye commented 10 years ago

Tried it, it works! Thanks. :)