3 ms·
As far as I'm aware, there's not a true templating system around for Compojure. There's Enlive, but that's kind of weird. From what I can see, you write all you
by icey 17y ago
As far as I'm aware, there's not a true templating system around for Compojure. There's Enlive, but that's kind of weird. From what I can see, you write all your HTML and then perform operations on the HTML completely from Clojure. If you were looking at the HTML you wouldn't know that it was going to be modified by another program. It's one of the reasons that I haven't spent more time working in Compojure - it assumes that everyone wants to spend all their time in Clojure.
The problem (to me) is that s-exprs just make sense for representing HTML. Every time I've tried to think of how a "perfect" templating language would look for Clojure, all options have seemed silly next to just using Lisp. I guess that's part of its charm (and its curse).
[Enlive: http://github.com/cgrand/enlive http://github.com/cgrand/enlive]
- jacquesm 17y agoThat makes integrating clojure/compojure in to the workflow of any regular business that produces websites hell on wheels. I can just imagine the look the average designer would give if you passed them a chunk of lisp and asked them to style this according the house style manual.
- icey 17y agoThere are a growing faction of people (of which I'm not quite part of yet, but I'm starting to warm up to it) that says that developers should be emitting HTML and passing it off to designers who will style it using nothing but CSS (and asking the developers to modify the HTML where needed). There is definitely something attractive about transforming HTML as code, but yeah, I really wouldn't want to hand a designer a chunk of lisp to work with.
- jimbokun 17y agoTranslating a chunk of Lisp into a chunk of HTML for a designer to work with, then translating it back, should be a straight forward problem to solve, no?
- jules 17y agoNo, because the Lisp template language isn't just another syntax for HTML: it contains control structures and other constructs not supported by HTML.
- ajuc 17y agoLike any templating engine I've seen - there are "for each", "if then else" etc. It's just matter of convention - designer should know what he can change, and what he cannot - lisp-encoded html is almost like normal html, and has the adventage of being compiled - some static checking can be done. When you extract data producing into functions only called b name in code that is emiting html it will lokk just like html template, and it has all the clojure power when it's needed.
- vdm 17y agoSomething I've heard come up in Clojure projects is StringTemplate. I haven't done this myself as Compojure does what I need. http://www.stringtemplate.org/ http://www.stringtemplate.org/ Example of using StringTemplate with Clojure: http://www.reddit.com/r/Clojure/comments/ablcc/string_interpolation_in_clojure/c0hr4i1 http://www.reddit.com/r/Clojure/comments/ablcc/string_interp...
- papaf 17y agoClojure is powerful enough that its easy to write a "true" templating library along the lines you describe. I wrote a very simple templating tool (47 loc) that allowed clojure to be evaluated within PHP style tags -- I didn't use it because I felt it doesn't add anything other than familiarity i.e. it was a step backwards. If you write your own html then compojure is, without question, the way to go. S-expressions require less typing than html and you can write functions or define snippets that build webpages up without the need to repeat yourself. Its an end to copy and paste. Also, stylesheets are your friend and can do most of the work of presentation. If you work with designers who require full access to the HTML then Enlive is a good option -- as you said there is no template code in with the html! Surely this is a step forwards from custom comment fields or custom tags?
- icey 17y agoRolling your own templating library would certainly work, but then you're stuck maintaining your own templating library. I like the concept of Enlive, but I have concerns about troubleshooting it. I feel like you're asking for trouble any time you have to look at multiple files just to figure out what's going on in one page. Troubleshooting a page that's been modified with Enlive would require you to have the HTML open, and then mentally translate all the operations so that you can see what changes what. I'm just not smart enough to be able to hold entire documents in my head when things approach even moderate complexity, and I want to be able to scan a document to see what's going on; not read the HTML document and then go through the list of operations to see how that document is being modified.
- yummyfajitas 17y agoThere must be java templating libraries. Why not just call one of them from clojure, perhaps wrapping as needed?
- radu_floricica 17y agoBut you can use pretty much any java templating engine. I've been using Apache Velocity for many years, and making it clojure-friendly took only a couple of hours. If anybody is curious I can put up some code.