Using an if statement in Middleman erb view file - ruby

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

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.

How I set another syntax inside a tag? With editor sublime text 3

I have difficulty understanding how to define a new sytanx inside a tag, I did not understand with some small examples.
I would like to modify the syntax HTML (Rails) with file .html.erb and what should I add code to define a javascript syntax inside a tag <% content_for :js do %> until close <% end %> ??
Thank you for your attention.
If I understand you right, you want to make the code that is between <% content_for :js do %> and <% end %> highlighted like JS code.
In this case, you have to use include: "scope:scope.name", like so:
%YAML 1.2
---
# See http://www.sublimetext.com/docs/3/syntax.html
file_extensions:
- test
scope: source.test
contexts:
main:
- match: "\\$.*"
scope: comment.test
- match: "<% *content_for :js do *%>"
push: javascript
javascript:
- meta_scope: embeded.js
- match: "<% *end *%>"
pop: true
- include: 'scope:source.js'
I use a context (JavaScript) for more clarity.
In the main context, the two first lines basically match comments (start with a $) just to check that the syntax was working. Then I check for <% content_for :js do %> and if it matches one, it pushes the context javascript (so it's going to stop reading the main one).
So, now, we're in the JavaScript context, the name I give to the scope is embeded.js (meta_scope), and then, if it matches <% end %>, the pop: true says: stop reading this context, go back to where you were before, so it goes back to main.
The last line is the line that tell sublime that in this context, we're using the syntax highlighting javascript.
Here's something to be aware of:
As soon as it finds a <% content_for :js do %>, the everything is highlighted, even if there is no <% end %>. The <% end %> just stop the sublime from highlighting the code using the source.js definition.
I hoped it helped you, if you have any trouble about this, you can find me on the sublime text forum

Using content_for and yield_content in Ramaze

Sinatra has sinatra/contrib to asist with this, what's the equivalent in Ramaze? I'm using Erubis by the way. Also, a quick Google search shows up really old pages that insist setting variables in the controllers and using them in the views.
Edit 1:
Taken from the gem documentation (http://www.sinatrarb.com/contrib/content_for.html)
You call content_for, generally from a view, to capture a block of markup giving it an identifier:
# index.erb
<% content_for :some_key do %>
<chunk of="html">...</chunk>
<% end %>
Then, you call yield_content with that identifier, generally from a layout, to render the captured block:
# layout.erb
<%= yield_content :some_key %>
I don't think Ramaze can do this natively. But you could quite easily do this manually, write a helper to do this, or even fill-in a Hash instance.
You might also want to look at partials if you need to render small chunks of HTML in loops.
You could also combine render_partial, store results in a hash, and yield it's content in the layout.
If the use case is something like rendering a sidebar, you probably want to write a helper so you take the logic out of your views.
A trivial example is here : https://github.com/Ramaze/ramaze/wiki/Adding-a-dynamic-sidebar-in-a-layout

Frontmatter in Padrino

In Middleman, there is a feature called frontmatter. Basically, you can put some YAML in front of your view and access it in the view and the layout like this:
---
my_list:
- one
- two
- three
---
<h1>List</h1>
<ol>
<% current_page.data.my_list.each do |f| %>
<li><%= f %></li>
<% end %>
</ol>
Is there a similar thing in Padrino?
Such feature does not come with padrino, where you usually would let these values be filled by your controllers.
Depending on your use case, you might use the content_for output helper (http://www.padrinorb.com/guides/application-helpers).
This might help if e.g. want to define the title (as in html/head/title) -- which is usually set by the layout template -- in the "nested" template of your page.

inserting blocks of text in haml

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

Resources