Skip to content

Instantly share code, notes, and snippets.

@yokolet
Created September 9, 2011 00:35
Show Gist options
  • Save yokolet/1205198 to your computer and use it in GitHub Desktop.
Save yokolet/1205198 to your computer and use it in GitHub Desktop.
(ns demo.gemstoclojure
(:use ring.adapter.jetty)
(:use clojure.contrib.io)
(:use clojure.contrib.classpath))
(import '(org.jruby.embed ScriptingContainer LocalContextScope))
(def c (ScriptingContainer. LocalContextScope/THREADSAFE))
(println (classpath))
(println (pwd))
;; Using $LOAD_PATH to load haml gem
;(def gempath [(str (pwd) "/src/haml-3.1.2/gem")])
;(. c setLoadPaths gempath)
;(. c runScriptlet "require 'rubygems'; require 'haml'")
;; Using classpath to load haml gem
(. c runScriptlet "require 'rubygems'; require 'haml-3.1.2/gem/haml'")
(def engineclass (. c runScriptlet "Haml::Engine"))
(def template
"%html
%head
%title
Hello Clojure!
%body
%h2
Hello Clojure from Haml!")
(def engine (. c callMethod engineclass "new" template Object))
(defn app [req]
{:status 200
:headers {"Content-Type" "text/html"}
:body (. c callMethod engine "render" String)})
(defn -main []
(let [port (Integer/parseInt (System/getenv "PORT"))]
(run-jetty app {:port port})))
@delonnewman
Copy link

Very cool.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment