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.
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.
HAML Rails nested partials not working
Use content_for
to "assign" rendered output to specific content regions:
shared/_display_panel.html.haml
.row
.col-sm-8.col-sm-offset-2
.panel.panel-default
.panel-heading
/ Page heading goes here (eg. @form_heading)
.panel-body
= content_for(:panel_body)
_form.html.haml
= simple_form_for @job do |f|
= f.error_notification
= render 'shared/error_messages', object: f.object
.form-inputs
= f.input :title
= f.input :summary
= f.input :body
.form-actions
= f.button :submit, 'Save Job', class: 'btn-success'
new.html.haml
- content_for :display_panel do
= render 'form'
= render 'shared/display_panel'
Even if you didn't know about content_for
, the cleaner solution is to "push" the name of a partial into your view using a "locals" variable:
shared/_display_panel.html.haml
.row
.col-sm-8.col-sm-offset-2
.panel.panel-default
.panel-heading
/ Page heading goes here (eg. @form_heading)
.panel-body
= render partial_to_render
new.html.haml
= render 'shared/display_panel', partial_to_render: 'form'
# equivalent to
# render partial: 'shared/display_name', locals: { partial_to_render: 'form' }
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 partials in haml and passing variable to it
You're mixing together two different ways of rendering partials.
Use either this (legacy) form...
render partial: 'dog', locals: {dog: dog}
or this newer form:
render 'dog', dog: dog
or this even better newer form, which you should prefer:
= render dog
but not the version you've written. You've taken the render 'dog'
part from the second way, and the locals: { dog: dog }
part from the first way. That passes a single local called locals
, which has a value of a hash.
The third way is the preferred way. If you have an ActiveModel object, you can simply call render <object>
and it will automatically choose the right partial for you based on the name of the model.
This works for collections of ActiveRecord models as well. You should be dropping your @dogs.each
loop completely, and simply be calling
= render @dogs
This will automatically invoke = render dog
for every dog in the collection. You just need to move your .dog
inside the _dog
partial.
When javascript loop inserts HAML partials, does server begin rendering partials before loop finishes?
Found it, server renders all inserted partials at one time.
Tested with looping though, setting console logs, inserting partials and setting timeouts.
Partials render all at once but even before console log shows message. Have more questions now but will have to look online for more info.
// Lets Begin
console.log("Now... Lets begin js.erb");
var data = "<%= escape_javascript(@data.to_json.html_safe) %>";
handler = JSON.parse(data);
console.log(handler);
console.log("this was the hanlder");
// Javasscript now has the data!! Lets begin to manipulate it.
$("#button").fadeOut("slow");
// Loop to test output
for (i in [1,2,3,4,5,6]){
setTimeout(function (){
console.log("Here we go");
},5000)
$("#table").fadeIn("slow");
$("#table").append("<%= escape_javascript(render 'table') %>");
setTimeout(2000)
};
Use HAML (for rendering partials) in a non-ruby environment
This is not a Guard issue. The render
method is defined by Rails, that's why you get the error.
That said you can find how to define it in https://stackoverflow.com/a/6131411/304055 (the question you already linked).
Related Topics
Set the Display Precision of a Float in Ruby
How to Get Ruby to Parse Time as If It Were in a Different Time Zone
How to Remove Repeated Spaces in a String
Strong Parameters Require Multiple
Get Server File Path with Paperclip
Capistrano 3 Execute Within a Directory
Aptana 3 Ruby Debugger - Exception in Debugthread Loop: Undefined Method 'Is_Binary_Data'
How to Add Child Nodes in Nodeset Using Nokogiri
How Does MACports Install Packages? How to Activate a Ruby Installation Done via MACports
Using Soap and Other Standard Libraries in Ruby 1.9.2
Can a Ruby Script Tell What Directory It's In
How to Add Confirm Message with Link_To Ruby on Rails
How to Update a Single Attribute Without Touching the Updated_At Attribute
Ruby CSV Parsing String with Escaped Quotes
Rails 4 User Roles and Permissions