Is it possible to compile HTML markup to templatable javascript on Coldfusion server-side?

梦想的初衷 提交于 2019-12-13 03:55:28

问题


I have been looking at Javascript templating engines trying to find a way to have standard templates available on/offline.

Currently I'm kind of stuck at dustjs/linkedIn which would require to server-side pre-compile a template into a jsfile - difficult as I'm using Coldfusion8.

There are sparse links available on running Javascript in Coldfusion (example). So:

Question:
Is there an easy to use Javascript in Coldfusion? I'm not talking about toScript our <cfoutput><script>alert("hello")></cfoutput>, I want to run a javascript function like the dustjs compiler on the server in a cfscheduled task to take HTML templates and compile them from this:

  Hello {name}! You have {count} new messages.

to this:

(function() {
    dust.register("demo", body_0);

    function body_0(chk, ctx) {
    return chk.write("Hello ").reference(ctx.get("name"), ctx, "h").write("! You have ").reference(ctx.get("count"), ctx, "h").write(" new messages.");
    }
    return body_0;
})();

which is done in the dustJS compiler.js file (I think...). If it's not possible, what are other options besides creating a "coldfusion compiler" which changes markup to compiled javascript?

Thanks for some toughts!


回答1:


Take a look at CFGroovy. Although targeting groovy, it's really a way for CF to use the generic scripting features of Java (JSR-223) You ought to be able to start Rhino through this and run your code. In fact, Ben Nadel has an article on doing this.

Althernatively, you could put node on your server and use cfexecute to run node to compile your templates.

If you make it work, I'd consider not compiling on a schedule. Perhaps try one fo the following:

  • Compile as part of your build, if you have a defined build process.
  • Compile on demand, so your script URLs might be script.cfm?script=whatever.js and have CF check dates and either compile the template or
  • Use the DirectoryWatcher event gateway to monitor your script folder and compile-on-change.

I like the third option best, as it'll only compile on change, which is probably what you want.



来源:https://stackoverflow.com/questions/12742741/is-it-possible-to-compile-html-markup-to-templatable-javascript-on-coldfusion-se

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!