In ASP.NET MVC what are the differences between "<%=", "<%:" "<%#" - asp.net-mvc-3

I've tried to perform a search on the above query but probably due to the angle brackets, I didn't find anything on the matter.
Could anyone please explain what the differences are between <%=, <%: <%#?
I seem to recall that <%# is preferred over <%= but I am not sure why.

The following article describes them pretty well.
<%=: Rendering Code Syntax
<%: %>: HTML encoded renedring (same usage as <%=)
<%# %>: Data Binding Syntax - works with server side controls in classic WebForms applications, inapplicable in MVC

<%= xxx %> Inserts the text in xxx into the page at that location. (more info)
<%: xxx %> Same as above except it html encodes the text for your convenience - (Except if xxx is an HtmlString which indicates it is already encoded)
<%# xxx %> Same as the first one too except xxx is only evaluated when DataBind() is called on the form (not really applicable in MVC) (more info)
Martin

Related

What does <%anything%> do?

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.

In ASP.NET WebForms, what is the difference between <%:, <%=, and <%#?

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

rendering Ruby statements inside of rails views?

So in my DB I want stored a ruby/HTML statement in the Database Table such as -
p This site is owned from 2000 - #{Time.now.year} by Acme Widget Co. /p
Or let's say that I want people to be able to include in other code snippits - e.g.
A client wants to run in a show view in the middle of a paragraph some magical partial such as #{render 'my_magical_code'}
On the view I have this being rendered as <%= raw(#page.content) %>
But its like a double rendering and rails will just put on the page #{render 'my_magical_code'} or #{Time.now.year}
So can / how do I solve this?
Thanks!
There are work arounds but I think the most proper way to solve your problem is to store your information in a better way. Add a migration to your model with a year_founded and company rows. You can then call these in your views rather than storing the HTML. Eg. This site is owned from <%= #object.year_founded %> to <%= Time.now.year %> by <%= #object.company %>

Commenting something out in Ruby ERB within the UI?

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 %>

HTML Encoding Strings - ASP.NET Web Forms VS Razor View Engine

I'm not quite sure how this works yet... trying to find documentation.
In my existing app I've got two different ways of rendering strings in my View
<%: model.something %>
<!-- or -->
<%= model.something %>
The first one is html encoded, and the second one is not.
Is there something similarly short in Razor? All I can find is this, which is the encoded version.
#model.something
I guess the best approach would be to use the Raw extension-method: #Html.Raw(Model.Something)
#Model.Something automatically HTML encodes. If you want to avoid HTML encoding (and you want this only if you are absolutely sure what you are doing) you could use #MvcHtmlString.Create(Model.Something) (basically everything that implements IHtmlString won't be encoded). Phil Haack blogged about the Razor view engine syntax.

Resources