I've found another solution combining your idiom and my method.

# begin
require 'haml'

template = %{
----%html
------%head
--------%title
------%body
--------%div
}.strip.gsub(/^\s{4}/, '')

Haml.Engine.new(template).render

# end

Of course, you feel free to make a helper method to improve this
awkwardness.

# begin
class String
--def adtemp(number_of_spaces) # adjust_template
----self.strip.gsub(/^\s{#{number_of_spaces}}/, '')
--end
end

new_template = %{
----%html
------%head
--------%title
------%body
--------%div
}.adtemp(4)

# end

Of course, we can improve it further. We need to add the adtemp method
in the HAML code and use it in a begin-rescue block which will watch
over the Inconsistent Indentation error.

On Jan 29, 4:13 pm, "P.A." <shama...@hotmail.com> wrote:
> Brilliant! That's exactly what I want. The problem was to organize
> controller, view and spec code in a single file. And now it's done. If
> you interested in that code what I got, please see below. (It might be
> problems with an indentation in email.)
>
> # gems:
> # - rack;
> # - sinatra;
> # - haml;
> # - rspec;
> # - rack-test.
>
> # controller.rb
>
> # application
>
> require 'sinatra'
> require 'haml'
>
> class Controller < Sinatra::Base
>   set :environment, ($*[0].to_sym if $*[0].respond_to? :to_sym)
> || :test
>
>   template :layout do
>     <<-haml.gsub(/^\s{6}/, '')
>       %html
>         %head
>           %title
>         %body
>           %div
>             = yield
>     haml
>   end
>
>   template :test do
>     <<-haml.gsub(/^\s{6}/, '')
>       %div Works!
>     haml
>   end
>
>   get '/' do
>     content_type 'text/html; charset=utf-8'
>     haml :test
>   end
> end
>
> # run code
>
> if Controller.development?
>   Controller.run!
> end
>
> # spec
>
> require 'spec'
> require 'rack/test'
>
> describe 'Sinatra application' do
>   include Rack::Test::Methods
>
>   def app
>     Controller
>   end
>
>   it 'should response to "/"' do
>     get '/'
>     last_response.status.should == 200
>   end
>
>   it 'should return a text/html document' do
>     get '/'
>     last_response.headers['content-type'].should == 'text/html;
> charset=utf-8'
>   end
>
>   it 'should return a standart html template' do
>     get '/'
>     template = /<html>.*<\/html>/m
>     last_response.body.should =~ template
>   end
>
>   it 'should return a standart html template with a partial included'
> do
>     get '/'
>     template = /<html>.*<div>Works!<\/div>.*<\/html>/m
>     last_response.body.should =~ template
>   end
> end
>
> # how it works
>
> # spec
> $ spec controller.rb
>
> # app
>
> $ ruby controller.rb development
>
> Thank you for the help.
>
> On Jan 28, 11:30 pm, Bradley Grzesiak <listro...@gmail.com> wrote:
>
> > You could do this:
>
> > # dashes are actually spaces, for clarity in email
> > template = <<-EOF.gsub(/^\s{4}/, '')
> > ----%html
> > ------%head
> > --------%title
> > ------%body
> > EOF
>
> > I tweeted about this last August. 
> > =Phttp://twitter.com/listrophy/status/3382558239
>
> > :brad
>
> > On Thu, Jan 28, 2010 at 1:35 PM, Nathan Weizenbaum <nex...@gmail.com> wrote:
> > > The problem is that, as the error message says, your indentation is
> > > inconsistent. Once you strip your template, it looks like this:
> > >https://gist.github.com/c50eed9d9965aae844bc. The first indentation Haml
> > > sees is for "%head", which is indented 8 spaces. It then expects 8 spaces 
> > > to
> > > be the amount you'll indent by in the future, but "%title" is only 
> > > indented
> > > by 2 spaces beyond "%head". Thus, it raises an error.
>
> > > On Thu, Jan 28, 2010 at 7:49 AM, P.A. <shama...@hotmail.com> wrote:
>
> > >> Hi.
>
> > >> I have next code.
>
> > >> # begin
> > >> require 'haml'
>
> > >> template = %{
> > >> ------%html
> > >> --------%head
> > >> --------%body
> > >> }.strip
>
> > >> Haml::Engine.new(template).render
> > >> # end
>
> > >> It works fine and render the template.
>
> > >> But if I add the %title tag inside the %head...
>
> > >> # begin
> > >> require 'haml'
>
> > >> template = %{
> > >> ------%html
> > >> --------%head
> > >> ----------%title
> > >> --------%body
> > >> }.strip
>
> > >> Haml::Engine.new(template).render
> > >> # end
>
> > >> ...I get the error: "Inconsistent indentation: 10 spaces were used for
> > >> indentation, but the rest of the document was indented using 8
> > >> spaces."
>
> > >> What is the problem of this error? How can I resolve it?
>
> > >> Thanks.
>
> > >> Debian GNU/Linux 5.0.3;
> > >> Ruby 1.9.2;
> > >> Haml 2.2.17.
>
> > >> --
> > >> You received this message because you are subscribed to the Google Groups
> > >> "Haml" group.
> > >> To post to this group, send email to h...@googlegroups.com.
> > >> To unsubscribe from this group, send email to
> > >> haml+unsubscr...@googlegroups.com <haml%2bunsubscr...@googlegroups.com>.
> > >> For more options, visit this group at
> > >>http://groups.google.com/group/haml?hl=en.
>
> > >  --
> > > You received this message because you are subscribed to the Google Groups
> > > "Haml" group.
> > > To post to this group, send email to h...@googlegroups.com.
> > > To unsubscribe from this group, send email to
> > > haml+unsubscr...@googlegroups.com <haml%2bunsubscr...@googlegroups.com>.
> > > For more options, visit this group at
> > >http://groups.google.com/group/haml?hl=en.
>
> > --
> > Bradley Grzesiak
> > co-founder, bendyworks llchttp://bendyworks.com/
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Haml" group.
To post to this group, send email to h...@googlegroups.com.
To unsubscribe from this group, send email to haml+unsubscr...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/haml?hl=en.

Reply via email to