rendering partials within filters in middleman - ruby-on-rails

I'd like to display a form within a :php filter in my Middleman Haml source file. Here's what I've tried:
:php
$someVar="#{= "my/partial"}"
It doesn't work. How do I render a partial within a filter? Simply outputting the value of a Ruby variable with the Haml code (rendered to HTML) would also be acceptable.

Related

Why naming convention of views files in rails are considered as action.html.erb only?

Why naming convention of views files in rails are considered as action.html.erb only ( instead of action.erb.html ) ?
What will happen if we write views files as action.erb.html ?
What will happen if we write views files as action.erb.html?
The first thing that will happen is that the Ruby syntax highlighting in your editor will stop working as the file extension is now .html instead of .erb. On almost every file system in use the file extension is the rightmost part of the file name.
The second thing that will happen is that Rails will no longer be able to lookup the template and even if it could it would no longer process it through ERB as it no longer has the .erb file extension.
.html is just a segment of the file name that lets the rails template resolver distinguish between templates for different formats when looking up a template for a given request format. Its not really technically part of the extension. For example:
show.html # just HTML - no processing
show.html.erb # a HTML ERB template
show.html.slim # a HTML Slim template
show.html.haml # a HTML Haml template
show.xml.erb # a XML ERB template
show.xml.slim # a XML Slim template
show.xml.haml # a XML Haml template
show.json.erb # a JSON ERB template
show.json.jbuilder # a JSON jBuilder template
TRDL; changing the file extension is a dumb idea. Especially when you consider that Rails actually supports multiple template engines such as jbuilder, Slim and Haml in addition to ERB.

CoffeeScript in Rails does not correctly render partial

$('#some').append('<%= render 'my_partial' %>')
And it renders it as text. It always escapes html. I have tried in all ways to avoid escaping.
$('#some').append('<%= j render('my_partial') %>')
$('#some').append('<%= raw render('my_partial') %>')
Result is the same.
If you want your coffee (or any files) to be interpreted as ERB, then you need to append the file name with .erb ie app.coffee.erb.
But a better question is why? Doing so will tightly couple your coffee to both rails and ruby making re-use much harder - much like putting css inline in HTML.
There are two better options IMO:
Use data attributes in HTML and access them with javascript
If you only have a single variable to pass, try something like <%= javascript_tag "my_func(#{#my_var})" %>. This will invoke a javascript function with that variable.

Not rendering Rails 4 partial correctly

I've upgraded a Rails 3 app to 4 and I'm trying to render some really simple partial thats not working correctly.
I'm using HAML and I have a layouts folder with a application.html.haml file inside. Within this file I call several partials that make up the template for the entire page. these partials reside in a application folder. For instance, I call:
= render "chromeframe"
which works perfectly. However, below this I have:
`= render "header"
which contains a lot of haml html code for the header of the page. My problem is this isn't getting rendered correctly and all I'm getting from that call is"
<header id="header">
<h1>Dummy</h1>
</header>
Still fairly new to Rails but I had this working perfectly in Rails 3 so I'm totally thrown by this problem. Any suggestions, I'm sure it's staring me in the face.
Thanks
Looks like you have a dummy _header.html.xxx or header.html.xxx anywhere in your views, that is found by render. It doesn't have to be a haml file.
Look at your deveopment.log. It should say Rendered ...header... (xx ms).
It tells you, where the partial is found.

Where does HAML load its templates from?

I am dabbling in some existing code and I am able to render some HAML like this:
.content_container
%strong{:class => "code", :id => "message"} Hello World!
But when the page loads, this HTML is rendered in an existing layout with a lot of the elements already defined.
I looked in config/settings/environment.rb which I was suggested to do by a HAML tutorial, but there was no mention of any other HAML code there.
Any idea how I can overwrite the header or find where the template is predefined?
It sounds like the template is being rendered with a layout. look in <app_root>/app/views/layouts/ for your missing HTML.
HAML likes files in the views directory that have a haml extension.
The docs say:
...all view files with the ".html.haml" extension will be compiled using Haml.

stuck with haml templates

I want to transform some haml (*.html.haml files) into xhtml. The haml command says "Usage: haml [options] [INPUT] [OUTPUT]". So I tried it with the following response:
Exception on line 1: undefined method `content_for' for #<Object:0xb730af2c>
I noticed that there are different formats which are all called haml. I noticed one which uses angle brackets a lot. Do I need some kind of preprocessing?
Here is a sample html.haml file that I want to transform:
- content_for :head do
= stylesheet_link_tag 'jquery.autocomplete'
= javascript_include_tag 'jquery.autocomplete'
- javascript_behaviour '$("input#user_full_name").autocomplete("project_roles/auto_complete_for_user_full_name")'
Note: I know how to google so I am looking for specific advice.
You are using a rails helper, content_for, so when you invoke haml using the command line program it doesn't know what does it stands for.
So either add the haml gem to your gemfile and save the template in a .html.haml template and try to render it or replace the content_for with the html result generated by rails.
What you wanted is to convert Haml to Erb - the Ruby templating language which looks like HTML. Haml doesn't do that, so it tried to render your haml template into xhtml instead of converting it to another template.
You could have used the haml2erb gem to do the conversion. It would go like this:
Dir["**/*.haml"].each do |filename|
File.open(filename.gsub(/haml$/, 'erb'),'w') do |f|
f.puts Haml2Erb.convert( File.read(filename) )
end
end

Resources