What's the difference style of writing in .net's MVC2,MVC3 and Razor? - asp.net-mvc-3

If anybody has written application in .net's MVC2 and MVC3, there is a change of syntax in writing a code like
<%= %>
has been replaced by
<%: %>
or
#
so this mean that:
<%= %> == <%: %> OR <%= %> == #
are equals?

<%= %>
Writes out the string exactly as is.
<%: %>
Html Encodes the string and then writes it out.
#
Html Encodes the string and then writes it out if you're using the Razor view engine.

Related

Tell <%= ... %> not to add a space

I use the following code in a template to render a date:
<% if job.end_month do %>
<%= "#{job.end_month}/" %>
<% end %>
<%= "#{job.end_year}" %>
This results in 3/ 2014 and not in 3/2014. How can I force Phoenix not to add a space after "#{job.end_month}/"?
One solution would be to create an external helper that did this for you:
e.g.
def job_string(%Job{end_month: nil}), do: job.end_year
def job_string(job), do: "#{job.end_month}/#{job.end_year}"
Then just use that in your view:
<%= job_string(job) %>
Alternatively if you don't want to do all that and would prefer a messier sort of look:
<% if job.end_month do %>
<%= "#{job.end_month}/#{job.end_year}" %>
<% else %>
<%= "#{job.end_year}" %>
<% end %>

Syntax error using form field in Sinatra

I keep getting the following syntax error in my index.html.erb file. I'm using Ruby 2.1.2. and Sinatra 1.4.5. Any comments are appreciated.
syntax error, unexpected ')' #_out_buf = ''; #_out_buf.concat(( form_for(#user) do |f| ).to_s)
<%= form_for(#user) do |f| %>
<div class="field">
<%= f.label :email %><br />
<%= f.text_field :email %>
</div>
<div class="actions"><%= f.submit "Sign Up"%></div>
<% end %>
When opening a block in an ERB template like you do with your form_for call, you can't use the <%= tag here. While this is required in Rails, it is only because they patch ERB in a rather horribly way to implement this.
If you do not use Rails, you have to use this:
<% form_for(#user) do |f| %>
<%# ... %>
<% end %>
Finally, unless you use an external library to implement this, note that form_for is not available in standard Sinatra.

Why is HTML stored in a variable not displaying as HTML?

I have created my layout in application.html.erb.
I want to include a few dynamic fields provided from the view. In application.html.erb file I have:
<p style="margin-left: 45px">
<%= yield ( :article_heading ) %>
</p>
Then in static_page/home.html.erb I have:
<% provide(:article_heading, 'Contact NùRùmie') %>
The HTML is not being properly parsed. Instead of producing the accent marks, it returns the string exactly.
x = "this is some <p>html content</p>"
if in your view you do
<%= x %>
then you'll get exactly that string, but this will be interpreted as html code if you do
<%= x.html_safe %>

Facebooker2 returning users UID but not name

I am just using the standard Facebooker2 setup.
<%= fb_connect_async_js %>
<% if current_facebook_user %>
<%= "Hello #{fb_name(current_facebook_user, :useyou => false)}!" %>
<%= fb_logout_link("Logout", request.url) %><br />
<% else %>
<%= fb_login_and_redirect('/login', :scope => 'user_about_me') %>
<% end %>
What displays on the page is:
Hello <fb:name uid="73648576" useyou="false"></fb:name>
I am sure there's something very basic wrong here, but I've followed the instructions, done tutorials, wetc. and continue to get this result.
Any help most appreciated!
Not sure why fb_name isn't working, but you could use the other method as specified in the readme. In this case, you need to call the fetch method on current_facebook_user first:
<% if current_facebook_user %>
<% current_facebook_user.fetch %>
<%= "Welcome #{current_facebook_user.first_name} #{current_facebook_user.last_name}!" %>
Ref: http://www.engineyard.com/video/21045039 (around 9:36)

Rails how can I put <%= link_to ...%> into an instance variable?

I have this (and other) navigation links:
<li><%= link_to "Actors with commas", actor_path('search')+'/commas' %></li> |
These need to be put into the header part of my templates and they change based on the view template.
So in application.html.erb I have this:
<div class='submenu'>
<%= #app_menu %>
</div> <!-- end submenu -->
And in the view template I have:
#app_menu =
The question is, how do I put the top line (with the link_to, etc) into that instance variable and have Rails parse it properly with the HTML and without errors. Right now if I include <ul><li> it prints them out literally. And, of course, if I include <%= link_to... I get all sorts of errors.
Or is there perhaps a better way?
What I ended up doing was creating partials for each menu, so one partial _actors_menu.html.erb might look like this:
<%= image_tag "submenu-separation.png", :alt => 'separator' %> <li><%= link_to "Actors with commas", actor_path('search')+'/commas' %></li> <%= image_tag "submenu-separation.png", :alt => 'separator' %>
<li><%= link_to "Actors with Ampersand (&)", actor_path('search')+'/acute' %></li> <%= image_tag "submenu-separation.png", :alt => 'separator' %>
Then I put #app_menu = "actors_menu" on each actor view. And so on for the other pages.
But I'm wondering if there is a smarter way to do all this?
Also, where and how would I set a global variable for the app_menu so as not to repeat it in every template?

Resources