How to Render a Partial in Sinatra View (Haml in Haml)

How to render a partial in sinatra view (haml in haml)?

EDIT: !!! OUTDATED !!! Read Jason's answer below!

What are you trying works in rails! Sinatra has no partial method. An implementation of partial on Sinatra looks like this (source gist) from github:

module Haml
module Helpers
def partial(template, *args)
template_array = template.to_s.split('/')
template = template_array[0..-2].join('/') + "/_#{template_array[-1]}"
options = args.last.is_a?(Hash) ? args.pop : {}
options.merge!(:layout => false)
if collection = options.delete(:collection) then
collection.inject([]) do |buffer, member|
buffer << haml(:"#{template}", options.merge(:layout =>
false, :locals => {template_array[-1].to_sym => member}))
end.join("\n")
else
haml(:"#{template}", options)
end
end
end
end

Including this method, you may call partial in your .haml files, like

= partial("partial_name")

If you want to render a view in an other view syntax is

= render(:haml,:'rel_path_to_view',:locals => {:optional => option})

Notice the syntax differences between rails and sinatra regarding render method!

Haml partial with Sinatra

You're not doing anything "wrong", when calling a view you use the file's basename, if it has an underscore you use an underscore; if not, you don't. I don't really see much benefit in using underscores or in leaving off the underscore. That said, I'm the maintainer of Sinatra Partial, and since some other people wanted it it's in there.

enable :partial_underscores

partial :review # will look for the _review.haml file.

If you wish to turn it off for a particular call:

# will render the non_underscored_partial.haml file.
partial :non_underscored_partial, :underscores => false

HAML Pass partial dynamically to layout in Sinatra app

Ah it was so simple afterall.. With the help of this question, I realized now of course that everything is wrapped with layout.haml and I simply needed to place the appopriate yield statement.

layout.haml

!!!
%html
%head
= partial :head
%body
= partial :header
= yield
= partial :footer

And call the template as usual:

get '/test' do    
haml :"test/index"
end

Render haml with locals and partials within partials to html file

I found other solution to do this.
Instead of using Haml::Engine, once we are already in rails and we can use render itself, i created a to_html function in the model and include the application helper to get the helper_methods that i used in the partial. Then used the render to print the result to a file:

def to_html
ActionView::Base.send :include, ActionView::Helpers::ApplicationHelper

File.open(File.join(Rails.root, "public", 'test.html'), "w") do |file|

file.print ActionView::Base.new(Rails.configuration.paths["app/views"].first).render(
:partial => 'metric_box_sets/metric_box_set',
:format => :html,
:locals => { :metric_box_set => self}
)
end
end

This way i can generate all html snippets that i needed based on the views that i already done.

Using haml partial to loop through and display

I would put the loop in haml. This is why I like haml compared with erb. It doesn't look like crap to put a bit of programming in the view.

-# index.haml
- @multipleLines.each do |oneLine|
.oneLine
= oneLine.someData
= oneLine.someOtherData

Also I would prefer to pass multipleLines in as a local to the view instead of having an instance variable. It makes your code more solid. If you still want to have one line in its own partial you can call render :partial something from inside the loop in index.haml.

Rendering HAML partials from within HAML outside of Rails

It's best to combine haml & sass with a tool for building static websites. Here's some links:

  • StaticMatic
  • Serve
  • Haml Jekyll -- a fork of Jekyll that supports haml.
  • Middleman

I'm using jekyll for my blog, but if you're not building a blog it's probably not appropriate for your needs.

How can I use a local (or per view) variable in Sinatra with Haml partials?

You pass variables into Sinatra haml partials like this:

page.haml

!!!
%html{:lang => 'eng'}
%body
= haml :'_header', :locals => {:title => "BOOM!"}

_header.haml

   %head
%meta{:charset => 'utf-8'}
%title= locals[:title]

In the case of a page title I just do something like this in my layout btw:

layout.haml

%title= @title || 'hardcoded title default'

Then set the value of @title in routes (with a helper to keep it short).

But if your header is a partial then you can combine the two examples like:

layout.haml

!!!
%html{:lang => 'eng'}
%body
= haml :'_header', :locals => {:title => @title}

_header.haml

   %head
%meta{:charset => 'utf-8'}
%title= locals[:title]

app.rb

helpers do
def title(str = nil)
# helper for formatting your title string
if str
str + ' | Site'
else
'Site'
end
end
end

get '/somepage/:thing' do
# declare it in a route
@title = title(params[:thing])
end

HAML partial - error rendering the partial

Replace with:

= render :partial => "/influencers/disclosures/shared/list"

How can I render partial in a coffee script filter, with haml?

Try escape_javascript

$('div#belterik').append("#{ escape_javascript render('belteri, :belteriClickCounter => window.belteriClickCounter)}")

which is also aliased as 'j' so you can have:

$('div#belterik').append("#{ j render('belteri', :belteriClickCounter => window.belteriClickCounter)}")


Related Topics



Leave a reply



Submit