michaelchance / tlc

Tag Line Commands
BSD 3-Clause "New" or "Revised" License
8 stars 1 forks source link

testing, sync and callback api #1

Open tunnckoCore opened 9 years ago

tunnckoCore commented 9 years ago

Hi!

It looks good, currently. But that

var doc = cheerio.load(htmlString)
tlc.run(doc, {'message':'Hello World'})
console.log(doc.html())

isn't correct way. Expose api to retrieve the result synchronously or pass it in callback.

E.g.

var doc = cheerio.load(htmlString)
var result = tcl.renderSync(doc, data)

console.log(result) //=> the compiled html string
// <div id="someElement">
//    <h1 data-tlc="bind $var &apos;.message&apos;; apply --append;">Hello World</h1>
//</div>

or callback

var doc = cheerio.load(htmlString)
var result = tcl.render(doc, data, function (err, result) {
  console.log(result) //=> the compiled html string
  // <div id="someElement">
  //    <h1 data-tlc="bind $var &apos;.message&apos;; apply --append;">Hello World</h1>
  //</div>
})
tunnckoCore commented 9 years ago

And again, if you want to provide also the html (DOM-like, as currently) you can just return object from .renderSync that returns {document: doc, result: compiledHtmlString}

and in .render() you can follow that signature

var doc = cheerio.load(htmlString)
var result = tcl.render(doc, data, function (err, result, doc) {
  console.log(doc.html()) // as now
  console.log(result) //=> the compiled html string
  // <div id="someElement">
  //    <h1 data-tlc="bind $var &apos;.message&apos;; apply --append;">Hello World</h1>
  //</div>
})

But it would be better getting DOM to be with different method to not mess up the render* methods with bloat stuff.

tunnckoCore commented 9 years ago

Also you dont need cheerio and multiline in the dependencies field.

Or even, it would be better to force to just pass html string, and you to use cheerio, without so many variants for incoming input - just to be string is enough.