Simple pure Rack example

118 views
Skip to first unread message

Bert Fitié

unread,
Dec 2, 2009, 5:40:21 AM12/2/09
to musta...@googlegroups.com

Hi,

Thanks for the clear Sinatra example. I would like to see also an example of a simple pure rack app.

Given the following simple app.rb started by config.ru:

class App
def call env
response = Rack::Response.new
response.header['Content-Type'] = 'text/html'
response.write "<b>Hello World!</b>"
response.finish
end
end

how would I separate out the 'response.write "<b>Hello World!</b>"' into a content method in a view and '<b></b>' in a template?

-- Bert

Chris Wanstrath

unread,
Dec 5, 2009, 3:57:36 PM12/5/09
to musta...@googlegroups.com
Check out the Mustache class documentation. You can view it as
comments in the code or online at:
http://defunkt.github.com/mustache/

The most basic example is as follows:

class App
def call(env)
view = Mustache.new
view[:content] = 'Hello world!'
view.template = '<b>{{content}}</b>'

response = Rack::Response.new
response.header['Content-Type'] = 'text/html'
response.write view.render
response.finish
end
end

I've been meaning to write a Mustache Usage Guide which walks you
through setting up Mustache in your own project from scratch, complete
with view and template paths. Would that be more helpful?

--
Chris Wanstrath
http://github.com/defunkt

Bert Fitié

unread,
Dec 6, 2009, 6:01:00 AM12/6/09
to musta...@googlegroups.com
Thanks for the example

>
> I've been meaning to write a Mustache Usage Guide which walks you
> through setting up Mustache in your own project from scratch, complete
> with view and template paths. Would that be more helpful?
>

Such a usage guide would indeed be very helpful (at least for me) because Mustache (and the Google Template System) represent a new philosophy of dealing with templates.


-- Bert Fitié


PS

In my local copy of the documentation from http://github.com/defunkt/mustache/, I "IRBified" part of the section "Dict-Style Views". It made it a little bit easier for me:

Given this template (winner.mustache)
...

We can fill in the values at will:

≫ require 'mustache'
=> true
≫ class Winner < Mustache; end
=> nil
≫ view = Winner.new
=> #<Winner:0x10175a7b8>
≫ view[:name] = 'George'
=> "George"
≫ view[:value] = 100
=> 100
≫ puts view.render
Hello George
You have just won $100!
=> nil
≫ view[:name] = 'Tony'
=> "Tony"
≫ puts view.render
Hello Tony
You have just won $100!
=> nil



Chris Wanstrath

unread,
Dec 7, 2009, 11:26:52 AM12/7/09
to musta...@googlegroups.com
On Sun, Dec 6, 2009 at 3:01 AM, Bert Fitié <be...@analytag.com> wrote:

> Such a usage guide would indeed be very helpful (at least for me) because Mustache (and the Google Template System) represent a new philosophy of dealing with templates.

Great, I've made a bug for this:

http://github.com/defunkt/mustache/issues/#issue/25

Cheers,

Chris

Bert Fitié

unread,
Dec 8, 2009, 9:04:35 AM12/8/09
to musta...@googlegroups.com

I seem to have a problem setting the template extension to 'tpl'. In the documentation it reads: "All settings can be overridden at the class level". I tried with two classes, but the app continues to look for 'mustache' templates:

(1)

class App < Sinatra::Base
register Mustache::Sinatra
Mustache.template_extension = 'tpl' # doesn't work
set :views, 'templates/'
set :mustaches, 'views/'

(2)

class App
module Views
class Home < Mustache
Home.template_extension = 'tpl' # doesn't work
def title

-- Bert

Chris Wanstrath

unread,
Dec 8, 2009, 11:14:49 AM12/8/09
to musta...@googlegroups.com
On Tue, Dec 8, 2009 at 6:04 AM, Bert Fitié <be...@analytag.com> wrote:

> I seem to have a problem setting the template extension to 'tpl'. In the documentation it reads: "All settings can be overridden at the class level". I tried with two classes, but the app continues to look for 'mustache' templates:

Do you receive an error? If so what is it.

Chris

Bert Fitié

unread,
Dec 8, 2009, 2:08:08 PM12/8/09
to musta...@googlegroups.com
Errno::ENOENT at /
No such file or directory - templates/home.mustache
Ruby /Library/Ruby/Gems/1.8/gems/sinatra-0.9.4/lib/sinatra/base.rb: in read, line 285
Web GET localhost/


As such the message is correct; I renamed the file to home.tpl

I have saved a PDF of the ShowExceptions page. Do you want me to send it to you (off-list)?


>
> Chris

-- Bert

Chris Wanstrath

unread,
Dec 10, 2009, 1:36:35 PM12/10/09
to musta...@googlegroups.com
On Tue, Dec 8, 2009 at 11:08 AM, Bert Fitié <be...@analytag.com> wrote:

> Errno::ENOENT at /
> No such file or directory - templates/home.mustache
> Ruby    /Library/Ruby/Gems/1.8/gems/sinatra-0.9.4/lib/sinatra/base.rb: in read, line 285
> Web     GET localhost/

I believe this is because Sinatra, not Mustache, handles the loading
in a Sinatra app:
http://github.com/sinatra/sinatra/blob/0.9.4/lib/sinatra/base.rb#L284-285

Sinatra wants you to name your templates with the extension of their
rendering engine.

You might be able to get around this by emulating the Sinatra plugin's
behavior and calling Mustache directly.

Bert Fitié

unread,
Dec 11, 2009, 2:40:06 AM12/11/09
to musta...@googlegroups.com

OK, I understand; I stay with the .mustache extension (I don't mind really).

Does this mean that the "template_extension" option in Mustache is useless now?

-- Bert

Chris Wanstrath

unread,
Dec 11, 2009, 12:01:19 PM12/11/09
to musta...@googlegroups.com
On Thu, Dec 10, 2009 at 11:40 PM, Bert Fitié <be...@analytag.com> wrote:

> OK, I understand; I stay with the .mustache extension (I don't mind really).
>
> Does this mean that the "template_extension" option in Mustache is useless now?

No, it just doesn't work when you're using the Sinatra plugin.

It still works if you're using Mustache on your own. You can even
write your own Mustache integration for Sinatra and have it work. But
I think it's simplest in this instance to go with the flow.

Cheers,
Reply all
Reply to author
Forward
0 new messages