Partial HAML templating in Ruby without Rails
I've done this before, just for a quick-and-dirty template producer. The easiest way is to just render the HAML inside the parent object:
%p some haml that's interesting
= Haml::Engine.new('%p this is a test').render
%p some more template
You'll more than likely want to build some methods to make this easier--a couple of helper methods. Maybe you write one called render_file that takes a filename as an argument. That method might look something like:
def render_file(filename)
contents = File.read(filename)
Haml::Engine.new(contents).render
end
Then, your template would look more like:
%p some template
= render_file('some_filename.haml')
%p more template
Note, you will probably need to pass self to the original Haml::Engine render so that it knows how to find your render_file method.
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.
Rails error says cannot find a certain partial template, but I did not define any
Assuming the _ingredients_firelds.html.haml above is a typo, so you have a file named _ingredients_fields.html.haml under /views/recipes/, try adding recipes folder to the path like this:
= render '/recipes/ingredients_fields', f: ingredient
You may also have a typo in your code.
EDIT: Ah I see you are using a cocoon gem. So rename your _ingredients_fields.html.haml to ingredient_fields.html.haml. Coocon expects you to have a Ingredient model class, so use singular here.
How can I split a HAML template into different partials/includes in PHP?
Quite an old question, but I've updated the source code of phpHaml to reflect this new functionality!
Check out the commit @github
https://github.com/endorama/phphaml/commit/8d95d5ebff06275db8b14438e566c6e41ec91b7f
How to render partial in HAML that only contains a conditional block
I believe attachments.each_file
on line 1 is expecting a block. It doesn't throw an error in the case of having 0 files, because it never attempts to yield
anything. But in the case that there are files, each_file
is trying to yield
the files to a block and raising the error you're seeing because there is no block to yield to.
Is there another way for you to test if there are any files? Something like !attachments.files.empty?
or attachments.files.count > 0
?
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!
Related Topics
Error Installing Debugger-Linecache in Ruby 1.9.3
Good Cucumber Examples in the Wild
Could Not Find a Valid Gem 'Rails' (>= 0) in Any Repository
How to Call Java API from Ruby 1.8 or 1.9
How to Set Up a Sinatra App Under Apache with Passenger
Manually Retry Job in Delayed_Job
What Is the &: of &:Afunction Doing
How to Find Where Gem Files Are Installed
Ruby Shoes Gui: Continually Updating Paragraphs
Why Does This Nokogiri Xpath Have a Null Return
Mailer Unable to Access Reset_Token in User Model
How to Get the Intersection, Union, and Subset of Arrays in Ruby
File.Open, Open and Io.Foreach in Ruby, What Is the Difference
Having Trouble Installing Any Ruby 1.9.X (With Rbenv) on MAC Osx Due to Psych Yaml Parse Errors
Open_Id_Authentication - "Openidauthentication.Store Is Nil. Using In-Memory Store." Problem
How to List Included Modules in a Ruby Class
M Hartl's Ruby on Rails Tutorial Chapter 5 Custom Title on Home Page