I'm totally new to js template engines. Handlebars seems to be the popular choice. I don't dislike the syntax for doing conditions, loops and so on, but since I'm perfectly capable of and feel more comfortable using plain old js and I'm not planning to let anyone who doesn't know js touch my templates, I'm asking if Handlebars supports this.
Of course the most popular choice isn't always the best. I'm more of a Mootools guy and jQuery drives me crazy(great library, just not for me). So if Handlebars was jQuery of template engines, what would be Mootools?
One of the central ideas behind handlebars and mustache is they are LOGICLESS by design and intention. They can not now, nor will they ever allow or recommend you embed raw JS in your templates. Some say this is a better design. If you want embedded javascript, almost every other traditional templating engine, such as _.template or jade or EJS is based on (or at least supports) the idea of embedded JS code snippets.
Sorry to resurrect a old issue here. If you have to use handlebars or it's not feasible to change your templating library you can use Helpers from the Handlebars library.
https://handlebarsjs.com/guide/expressions.html#helpers
Registering a helper in handlebar can be a walk around to what you are trying to do. See this link for a complete example: Registering handlebars helpers with node does absolutely nothing
Related
I'm looking for a front-end HTML templating engine and I was wondering if Vuejs has this functionally built into it. If not, can someone recommend me a templating engine?
All I want to do is create HTML pages which derived from a master page.
Thanks
I've used Mustache and have found it great - easy to use and fast. Check it out here: https://github.com/janl/mustache.js/
currently I'm playing a little bit with spine.js
I have red the documentation about views and there they have shown an example with eco as templating engine.
Until now i don't really like the eco engine.
I would prefer to use haml as my templating engine.
Is there a way to add support for haml in spine.js?
Until now i tried to add https://github.com/uglyog/clientside-haml-js in my index.html and then i was able to compile haml templates, in my controller, to html.
But it is not possible to load the templates in the same way as it works wit the eco files.
So how can I integrate haml into spine.js?
Use hem-haml-coffee.
I recently added jhaml support as an equivalent to jeco.
The solution is to add a slug.js file to your application which includes the relevant stuff
You will find a more detailed solution here: https://groups.google.com/group/spinejs/browse_thread/thread/d951e77db33d084a
How do you use CoffeeScript? It need to be compiled, so - you write code in CoffeScript, compile it, and insert real JavaScript on your site?
Doesn't it take a lot of time? Or is there some another way?
P.S. I've seen another way - to insert in development stage coffeescript in text/coffeescript script-tags with coffeescript.js library (about 150k), and compile only for production version and insert real Javascript.
The answer is yes, you compile it and include the generated JavaScript on your side.
If you're using a web framework (rails, django etc) you should take a look at the following list of coffeescript plugins: https://github.com/jashkenas/coffee-script/wiki/Web-framework-plugins. They will compile your coffeescript to javascript when you deploy your app to a server.
Using the coffee-script plugin with the text/coffeescript tags is another option, gzipped and compressed, its only about 39kB, but that can add up if you include it on pages that get many hits and I don't think is a good idea when you can compile the coffeescript to javascript yourself without needing the plugin.
There is the middleman plugin which will lets you work with CoffeeScript during development, then compile and minify it for deployment.
When you are first learning coffeescript, you will almost certainly want to do your compilation manually during development, because you will probably find, as most of us do, that you need to be able to look at the javascript code in order to debug and find out what's going on. The more fluent and comfortable you become with coffescript, the less often you will need to refer to the javascript code.
I think this will probably be true no matter what your level of expertise in javascript. If you are a javascript pro, you will be dependent on looking at stuff you are familiar with, until you start to figure out how coffeescript works. On the other hand, if you are completely or partially unfamiliar with all the quirks and subtleties of javascript, such as prototypes, the javascript approach to scope and globals, and all the rest, you'll find yourself digging into javascript references, and cross-referencing the javascript code with your coffescript code, until you get familiar with it.
I want to have achieve something similar to Java Tiles framework using only client side technologies (no server side includes).
I would like to have one page, eg layout.html which will contain layout definition. Content placeholder in that page would be empty #content div tag.
I would like to have different content injected on that page based on url. Something like layout.html?content=main or layout.html?content=edit will display page with content replaced with main.html or edit.html.
The goal is to avoid duplicating code, even for layout, and to compose pages without server-side templating.
What approach would you suggest?
EDIT: I don't need a full templating library, just a way to compose a pages, similar for what tiles do.
JavaScriptMVC has a view templating system that supports different engines, including a pure JavaScript based one called EJS.
You might also want to look into Mustache especially Mustache for JavaScript.
If you would like to use jQuery, there is a decent templating engine in development as well:
http://github.com/jquery/jquery-tmpl
http://api.jquery.com/jquery.tmpl/
Check this out:
http://layout.jquery-dev.net/
I thinks it's close to what you want.
I am looking at javascriptMVC at the same time.
In the forum they are talking about using jquery layout with it.
I don't know if it fit exactly to what you want to do, but using the GWT seems to be a good approach for rich client-side applications :
You write all your application in Java with the framework, and you compile for obtain HTML and JS files working stand-alone.
What of instead of the standard Django template rendering model which is server side, there were a library that allowed something that looked like a Django template to be rendered with JavaScript in the client's browser? The variables could be filled in by getting a JSON blob from the server and rendering the template would be done entirely on the client side.
Does a library like this already exist?
As I understand it, GWT sort of does this, but behind the scenes and with a ton of programmer overhead.
I think you may be thinking of the recently announced jQuery template plugins. There are of course other solutions out there, this has gotten the most press lately, with a similar syntax to what you describe.
Google Closure has a template library, there is TrimPath, and there is a long list of answers over here.
you probably think of mustache, but you can also check pure templates