Using ErlyWeb templates with MochiWeb
Dave Bryson - daveb - May 06, 2008Here’s a very simple way to add dynamic templates (views) to a MochiWeb based web front-end. I wouldn’t want to build an entire web application this way, but it’s a quick way to add a simple web interface to your Erlang application.
First I pulled the simple template code from ErlyWeb (erltl.erl) and compiled it with my code.
For this example, here’s a template index.et that shows a list of nodes the Erlang application is connected to.
index.et
<html> <body> <h2>Available nodes</h2> Total online: <% integer_to_list(length(Data)) %> <ul> <% [index(A) || A <- Data] %> </ul> </body> </html> <%@ index(N) %> <li><% atom_to_list(N) %></li>
Now compile it with “erltl:compile(”index.et)” and put in the same path as your mochiweb app (below).
Next setup the Mochiweb application:
-module(test_templates_web).
-export([start/1, stop/0, loop/2]).
start(Options) ->
{DocRoot, Options1} = get_option(docroot, Options),
Loop = fun (Req) ->
?MODULE:loop(Req, DocRoot)
end,
mochiweb_http:start([{name, ?MODULE}, {loop, Loop} | Options1]).
stop() ->
mochiweb_http:stop(?MODULE).
loop(Req, DocRoot) ->
"/" ++ Path = Req:get(path),
case Req:get(method) of
'GET' ->
%% This call returns a list of nodes the Erlang application is connected to -
%% a list like this: [node1@here.com,node2@here.com]
NodeData = nodes(),
%% Pass the data to the template
Outty = index:render(NodeData),
%% Send the output (String) back to the browser
Req:ok({"text/html",Outty});
_ ->
Req:not_found()
end.
Inside the ‘GET’ above, I pass the data to the compiled template, then send it back to the browser.
Obviously I’ve glossed over a few details, but hopefully you get the idea. It may not be as “elegant” as a Rails app, but it’s blazing fast!
Categories: Blogs Dave Bryson
Comments
No comments so far, you could be the first.Add comment
Erlang on Twitter
» MQuotient (MQuotient Solutions): Great start to seminars at MQ. Started with Fundamentals of statistics, Concurrency with Erlang, Python tricks and Advanced Git
» a2zmax (這いよれ!あずまっくす): RT @matsumotory: erlang入れるのに、久々にepel使ってる
» matsumotory (MATSUMOTO, Ryosuke): erlang入れるのに、久々にepel使ってる
» csanz (Christian Sanz): earlang community is kinda weak ATM http://t.co/pJCU5TSY
» dooridho (Ridho Septiansyah): Hha kirimlah boy nak jadiin DP :D “@doni_erlang: Mantap” boy,haha RT @dooridho: @doni_erlang boy poto kemaren gimano :D”
» doni_erlang (Dony Erlangga): Mantap” boy,haha RT @dooridho: @doni_erlang boy poto kemaren gimano :D
» dooridho (Ridho Septiansyah): @doni_erlang boy poto kemaren gimano :D
» 9renpoto (Keisuke Kan): EndingがErlangに見えるぐらいに視力落ちてる
» doni_erlang (Dony Erlangga): Otak lo yg lemot bro haha RT @rudyprasetio: Lemot amatt ni…
» dtoader (Dragos Toader): Building Erlang OTP R15B from source in cygwin http://t.co/JhSzPZLY
Statistics
Number of aggregated posts: 10504
Number of comments: 2145
Most recent article: May 21, 2012
Latest comments
» sunshine on We Who Value Simplicity Have Built Incomprehensible Machines: How to Get Free Credit Scores Having good credit is imperative in today’s world. There are so many things you…
» cheap stickers printings on A Forgotten Principle of Compiler Design: I really like your way of expressing the opinions and sharing the information. It is good to move as chance…
» jamesmathew on This is Why You Spent All that Time Learning to Program: This could be a good achievement for the small area to have a personal news channel and could really help…