In Rails tutorials and vids, it seems totally different from regular ruby. For example, I see <%render%> or <%end%>. When am I supposed to use <%%> and what is it for?
<% code %> is used in erb, which stands for "Embedded Ruby". It's typically used in HTML generating templates. It's brother is <%= code %> which outputs the return value of the expression inside it.
<h1>Hello, <%= #user.name %>!</h1>
# potentially renders: <h1>Hello, Bob</h1>
# potentially renders: <h1>Hello, Sue</h1>
The non-outputting <% code %> version of this tag is useful for executing code, but not writing anything to the template. This is useful for conditionals (as well as other things).
<h1>
Hello
<% if #user.sex == 'male' %>
Mister
<% else %>
Miss
<% end %>
<%= #user.name %>!
</h1>
# potentially renders: <h1>Hello Mister Bob!</h1>
# potentially renders: <h1>Hello Miss Sue!</h1>
In pure ruby, this would be a syntax error. But within an erb template, these tags allow you to control how the template renders by executing ruby to control the template flow, and by writing out the result of ruby expressions.
Rails uses erb by default for it's views, which are mostly html generating templates. So you see this a lot in Rails examples. Just keep in mind that erb is just one option for your templates in Rails. There is a great many options, which may use different syntax entirely.
That is used in HTML code to display ruby code inside the tags.
<%= render 'folder/partial_form' %>
That will render a form partial.
I suggest you have a nice long read through the links below:
http://www.guides.rubyonrails.org
http://guides.rubyonrails.org/layouts_and_rendering.html#using-partials
The <%= %> and <% %> syntax is used to write ruby in a .erb file. Think of ERB as an html file that always allows ruby code to be run. The <% %> syntax runs the ruby but does nothing by default while the <%= %> syntax(notice the =) outputs the result of the ruby code to the screen.
Related
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
In Middleman, I am trying to set up a blog site, using custom layout for the blog. My problem is that the main layout is loading, but the blog layout for my articles is not. The article files are being served in with their plain body.
In source/layouts/ I have two files: layout.erb and article_layout.erb.
My intent is to use article_layout.erb for my blog articles.
In config.rb I have the following:
activate :blog do |blog|
blog.sources = "articles/{category}/{year}-{month}-{day}-{title}.html"
blog.layout = "article_layout"
end
I have also tried moving article_layout.erb to source/articles/ as well as prepending the config.rb file like this: blog.layout = "layouts/article_layout"
Another failed approach is to comment out the above option and configure the layout by adding this line instead: page "/articles/*", layout: "article_layout".
So far none of these approach show a difference. Since the default layout is not being rendered I would expect some sort of error message if the path to the layout cannot be found, but nothing shows up.
I managed to replicate your problem with my own Middleman blog setup.
The docs are unclear on this because there is a broken link in the layout section of Blogging.
You need to use the nested layout feature of Middleman and wrap your custom layout in:
<% wrap_layout :layout do %>
<% end %>
So your article_layout.erb would look something like this:
<% wrap_layout :layout do %>
<div class="article-container">
<article>
<h2 class="article-title"><%= current_page.title %></h2>
<%= yield %>
</article>
</div>
<% end %>
And keep your custom layout in the source/layouts folder.
Here are the docs for Nested Layouts for your reference.
I hope this helps.
This question is proving hard to google, so if there is a duplicate question & answer or definitive reference, please share.
<% %> An embedded code block is server code that executes during the page's render phase. The code in the block can execute programming statements and call functions in the current page class. http://msdn2.microsoft.com/en-gb/library/ms178135(vs.80).aspx
<%= %> most useful for displaying single pieces of information. http://msdn2.microsoft.com/en-us/library/6dwsdcf5(VS.71).aspx
<%# %> Data Binding Expression Syntax. http://msdn2.microsoft.com/en-us/library/bda9bbfx.aspx
<%$ %> ASP.NET Expression. http://msdn2.microsoft.com/en-us/library/d5bd1tad.aspx
<%# %> Directive Syntax. http://msdn2.microsoft.com/en-us/library/xz702w3e(VS.80).aspx
<%-- --%> Server-Side Comments. http://msdn2.microsoft.com/en-US/library/4acf8afk.aspx
<%: %> Like <%= %> But HtmlEncodes the output (new with Asp.Net 4). http://weblogs.asp.net/scottgu/archive/2010/04/06/new-lt-gt-syntax-for-html-encoding-output-in-asp-net-4-and-asp-net-mvc-2.aspx
It is actually described pretty well in msdn.
Check out following link:
http://msdn.microsoft.com/en-us/library/fy30at8h(v=vs.71).aspx
WHen you are writing ERB control structures in HTML markup, is it better to indent things to make the ERB logic clearer, or to make the resulting HTML indentation correct?
E.g.
<ul>
<li>Entries</li>
<% entries.each do |entry| %>
<li><%= entry.title %></li>
<% end %>
</ul>
vs
<ul>
<li>Entries</li>
<% entries.each do |entry| %>
<li><%= entry.title %></li>
<% end %>
</ul>
It's a taste-based preference, but I would go with indentation that makes the code easiest to read, since that's where more of your brain cells are expended. For me, that's the second option, since it is more obvious to me that there is a loop there, and that the loop is inside the list, not just some plain old unlooped <li> tags.
This is quadrupally (can one say that?) true when you think about all the stuff that can happen to the markup between your code and the response: have you ever tried to correctly guess the indentation of a rendered partial? It's pretty much by definition impossible. What about if your markup is minified?
HTML indents only really matter if you spend a lot of time reading your HTML page, but in practice, I only look at the HTML of my pages in my browser's developer tools, which automatically does some cleanup of the markup anyway.
I have a .html.erb page and I am trying to comment something out using traditional HTML comments:
<!--
User Id (testing MySQL call): <%= #User.uid %>
-->
But since its a Ruby reference that I am commenting out, it isn't getting commented, and is generating ruby errors. How could I comment out such a thing? I also tried putting a # before that line, but that didn't work either.
In your ERB tags, to do a comment, use:
<%-# #User.uid %>
You'll still need the HTML comment tags wrapping the other text too.
You can comment out an ERB expression by changing the <%= into a <%#. This will not hide the HTML containing it from view, but you can combine HTML comments with the ERB comment to keep your application from throwing an error and hiding the surrounding HTML bits.
<!--
User Id (testing MySQL call): <%# #User.uid %>
-->
You can also comment a block with =begin and =end like this:
<%
=begin %>
<%= link_to "Sign up now!", signup_path, :class => "signup_button round" %>
<%
=end %>