inserting blocks of text in haml - ruby

In my Jekyll blog I use the include tag to put the contents of a file into the document. However if I attempt to do this with a HAML based document the indentation of the included text is wrong. :preserve does not work because it requires indentation. Is there a way to specify a block of text without depending on indentation?
%html
%body
- preserve do
<strong>included text from file</strong>
- end

It seems that in current version of Haml :text filter was removed. Now you can use :plain filter. No processing is performed for the text inside this block. You can paste multiline blocks of text or HTML code there. HTML will appear on the page unescaped.
:plain
Some text <b>or HTML</b>.

For text, use filters:
:text
some text
:erb
<%= render :partial ... %>
http://haml-lang.com/docs/yardoc/file.HAML_REFERENCE.html#filters

Related

How to call js and css files inside a html.erb file

I'm quite new to Ruby and ERB and for this case I'm using only Ruby and not rails.
E:\ruby
-app.rb
-plan.html.erb
-check.css
-track.js [js + jquery framework]
Inside app.rb I've the following lines
text = File.open(("final.html"), "w+")
text.puts ERB.new(File.read("plan.html.erb")).result binding
I'm not sure how to call the .js and .css files inside the .html.erb file. Kindly let me know if I've to post the .html.erb file in case that would be helpful to debug further, thanks.
You can include the JavaScript in the .html.erb file in the same way you load the text file. The simplest (code wise) solution is doing something along the lines of this:
plan.html.erb
<script>
<%= File.read('some/file.js') %>
</script>
However if you are expecting a <script src="some/file.js"></script> as result you'll have to create your own helper or use an existing one from some light weight web framework. A simple example might be:
lib/html_helpers.rb
require 'builder'
module HtmlHelpers
def javascript_include_tag(path)
Builder::XmlMarkup.new.script('', src: path)
#=> %{<script src="#{html_escaped_path}"></script>}
end
end
plan.html.erb
<% require 'html_helpers' %>
<% include HtmlHelpers %>
<%= javascript_include_tag('some/file.js') %>
Keep in mind that the first solution doesn't escape any HTML characters. Meaning that if your script contains </script> everything following that tag will be interpreted as HTML.

Middleman: choosing information from data files in frontmatter

I am using Middleman static page generator and I would like to pull information from data files based on selection made in frontmatter.
Example
I have data file located at data/cta.yaml with different variants of Call-To-Action text that can be repeated on various pages, meaning that each CTA text can be used on more than one page.
data/cta.yaml:
basic: This is default CTA
special: Something special here
other: Some other CTA
Then I have layout.erb:
<body>
<%= yield %>
<p class="cta">No data yet</p>
</body>
And test.html.erb:
---
title: Some page for testing
cta: It works with layout if I do not reference 'data/cta.yaml'
---
Some page content.
If I want to use, let's say, first CTA text, I could use <p class="cta"><%= data.cta.basic %></p> either in layout.erb layout file or remove it from layout and move it directly to the end of test.html.erb template file. Or, I could drop data file altogether and simply type CTA text for each page in frontmatter. However, I would prefer to keep CTA text in data file and all HTML in layout.erb and then be able to "choose" information from cta.yaml in test.html.erb frontmatter.
I tried to change
<p class="cta"><%= data.cta.basic %></p>
in layout.erb to
<p class="cta"><%= current_page.data.cta %></p>
and then in test.html.erb frontmatter:
---
title: Some page for testing
cta: data.cta.basic
---
but that resulted in verbatim data.cta.basic text instead of "This is default CTA" from cta.yaml data file.
Question
Is it possible at all to use frontmatter to select which text from data file should be used for given page?
As I mentioned in my comment, the frontmatter is parsed before the ERB, that’s the reason you are seeing data.cta.basic instead of the correct cta.
You could add a helper to achieve this though.
Here’s my helper
module CtaHelpers
def page_cta
cta = current_page.data.cta
data.cta.send(cta)
end
end
Here’s my test.html.erb file:
---
cta: special
---
<p class="cta"><%= page_cta %></p>
The test.html.erb file is calling the helper that determines from the Frontmatter which CTA to use, so the output is:
Something special here

Using an if statement in Middleman erb view file

I am trying to figure out how to selectively show / hide a block of html in a Middleman erb view layout file based on a variable in the YAML.
So, in my markdown file I've got
---
DisableChooser: false
---
#some markdown
Then in the layout file, there is
<% if current_page.data.DisableChooser == "true" %>
<%= partial "layouts/sidebar" %>
<% end %>
Originally I just wanted it if it was set, but it doesn't seem to check the value no matter what I put there (except 0 or 1, which it seems to choose with - as in if 0 / if 1)
I imagine I'm doing something stupid, I just can't figure out what.
It seems that simply making the YAML in quotes works! (though it seems happy not to do so with strings that are output to HTML)
---
DisableChooser: "false"
---
#some markdown

Slash at the end of the line in haml - should I clean it?

I'm wondering why in the source code of my project I sometimes see a / at the end of the line (Haml).
%meta{:content => "text/html; charset=utf-8", "http-equiv" => "Content-Type"}/
Maybe it's due to some HTML to Haml conversion, but why?
I cannot find any doc about that. Just wondering if I should clean this.
It creates a void self-closing tag.
The forward slash character, when placed at the end of a tag
definition, causes Haml to treat it as being an empty (or void)
element. Depending on the format, the tag will be rendered either
without a closing tag (:html4 or :html5), or as a self-closing tag
(:xhtml).
See the HAML documentation: http://haml.info/docs/yardoc/file.REFERENCE.html#empty-void-tags-

HAML -> Backbone Template, Unescaping HTML Parameters

I'm using HAML to generate templates for a Backbone.js app. I need to be able to insert <%= blah %> as an html attribute a la:
%a{:href => "myresources/<% id %>"} My Resource
and have it output
<a href='myresources/<%= id %>' >My Resource</a>
in the html template. Unfortunately, HAML escapes the html parameters leaving me with
<a href='#myresources/<%= id %>'>My Resource</a>
According to the HAML Reference the '!' operator can be used for unescaping strings, but not within the HTML attributes.
Also, I'd use plaintext to render the anchor tag, but since the anchor tag is the root for this particular view, I lose all of the benefits of using HAML.
Any help?
Update
I didn't mention, but I'm using LiveReload to actually watch my file system and run the haml compiler, and there was a setting in LiveReload to disable HTML escapes in tag attributes. < head slap > If anyone else runs into this issue outside of LiveReload, you can also set the :escape_attrs option to false when configuring your HAML setup.
You can configure HAML to not escape tag attributes using the escape_attrs option in your HAML configuration. See HAML Options.
You can try using html_safe which is a method on String objects. This will escape the html characters in the variable statement (< for example) and will leave the intact for underscore to evaluate at runtime:
%a{:href => "myresources/<% id %>".html_safe} My Resource
Found on answer to Interpolate inside html attributes with Underscore.js

Resources