ASP.NET MVC Spark view engine - asp.net-mvc

What pros(positive sides) of using Spark view engine for ASP.NET MVC project. Why it better then default view engine?

One important thing about Spark View engine is that its syntax is very similar to HTML syntax, that way your views will be clean and you will avoid "tag soup" that is in WebForms View engine.
here is an example:
Spark:
<viewdata products="IEnumerable[[Product]]"/>
<ul if="products.Any()">
<li each="var p in products">${p.Name}</li>
</ul>
<else>
<p>No products available</p>
</else>
WebForms:
<%var products = (IEnumerable<Product>)ViewData["products"] %>
<% if (products.Any()) %>
<ul>
<% foreach (var p in products) { %>
<li><%=p.Name %></li>
</ul>
<%} } %>
<% else { %>
<p>No products available</p>
<% }%>

It avoids the HTML tag soup you see a lot. Consider Spark:
<ul>
<li each='var p in ViewData.Model.Products'>
${p.Name}
</li>
</ul>
as opposed to the classic html tag soup variant:
<ul>
<% foreach(var p in ViewData.Model.Products) { %>
<li>
<%= p.Name %>
</li>
<% } %>
</ul>
The Spark syntax is much cleaner.

I really like the Bindings features.
http://sparkviewengine.com/documentation/bindings
You can specify something in the bindings and use nice xml markup for it in your views.
We have bindings for all the html helpers we use in our views eg.
<textbox for=""/>
<dropdown for="" items=""/> etc etc...

Related

elegant way to add css class in rails partials

I have partials which display in sidebar like below
__side_bar_partials.html.erb
<div class="col-sm-3">
<div class="account-left">
<ul>
<li class="active">Menu1</li>
<li>Menu2</li>
<li>Menu3</li>
<li>Menu4</li>
<li>Menu5</li>
</ul>
</div>
</div>
now what i need when i render this partial i want to add css class active added on selected menu
right now i render it with normal way
<%= render "profile_sidebar"%>
what i need to include in above code so it will add active class on selected menu
Basically you have 2 choices.
First one is to check the current page and highlight the right menu choice.
<div class="col-sm-3">
<div class="account-left">
<ul>
<li class="<%= 'active' if current_page?(menu1_path) %>">Menu1</li>
<li class="<%= 'active' if current_page?(menu2_path) %>">>Menu2</li>
<li class="<%= 'active' if current_page?(menu3_path) %>">>Menu3</li>
<li class="<%= 'active' if current_page?(menu4_path) %>">>Menu4</li>
<li class="<%= 'active' if current_page?(menu5_path) %>">>Menu5</li>
</ul>
</div>
</div>
But since you are looking for more elegant way, I would suggest second choice: doing a separate helper for your list elements. The solution with helper is perfectly described here.
Hope it helps.

Dashing - reorder/sorting widgets

is it possible to reorder/move widgets by given property on the dashboard ?
i am adding widgets dynamically to the dashboard by pushing data from a job to the erb file:
<div class="gridster">
<ul>
<% settings.servers.each do |data| %>
<li data-row="1" data-col="1" data-sizex="1" data-sizey="1">
<div data-id="<%=data['webHost']%>" data-title="<%=data['name']%>" data-version="<%=data['Version']%>" >
</li>
<% end %>
</div>
You can organize your widgets changing data-row and data-col.
If there are some property on your data that can be used to sort them the widgets, you could use it on data-row and data-col
Ex.:
<% settings.servers.each do |data| %>
<li data-row="<%= data['row'] %>" data-col="<%= data['col'] %>" data-sizex="1" data-sizey="1">
<div data-id="<%=data['webHost']%>" data-title="<%=data['name']%>" data-version="<%=data['Version']%>" >
</li>
<% end %>

Rails object deleted in development, but apparently not in production. Why?

I have a bunch of College objects. I print them all out here: http://www.collegeanswerz.com/colleges. (excluding the alphabetical tab. for that I hand coded the HTML)
Take the Rank tab as an example. (The Size and Table tabs are analogous to this code.)
<div class="tab-content">
<div id="national_universities2" class="tab-pane active">
<h3>National Universities</h3>
<nav class="list">
<ol>
<% #national_university_rank.each do |school| %>
<li value="<%= school.us_news_ranking %>"><%= link_to school.name, '/'+school.url %></li>
<% end %>
</ol>
</nav>
<br />
<p class="usnews">Ranks according to <%= link_to "US News", "http://colleges.usnews.rankingsandreviews.com/best-colleges" %>.</p>
</div>
<div id="liberal_arts_colleges2" class="tab-pane">
<h3>Liberal Arts Colleges</h3>
<nav class="list">
<ol>
<% #liberal_arts_college_rank.each do |school| %>
<li value="<%= school.us_news_ranking %>"><%= link_to school.name, '/'+school.url %></li>
<% end %>
</ol>
</nav>
<br />
<p class="usnews">Ranks according to <%= link_to "US News", "http://colleges.usnews.rankingsandreviews.com/best-colleges" %>.</p>
</div>
</div>
There are a couple of colleges that I deleted using rails console. They aren't showing up in the development version of this web page, but they are in the production version. Why is this?
I assume you've figured this out already, but it's probably because you deleted them in development which means the development database which is not the same as the production database (in a typical rails setup). Each environment has its own database. If they look the same it's because you (or a previous developer) cloned databases from one environment to another for testing or other purposes.
If you want to remove them from production, you need to get on that machine (wherever it's hosted) and run the same steps you ran in development to remove those records.

Html.Encode importance

I'm working through the NerdDinner MVC tutorial and came across this and was wondering about.
On page 62 of the pdf they have the following:
<asp:Content ID="Main" ContentPlaceHolderID="MainContent" runat="server">
<h2>Upcoming Dinners</h2>
<ul>
<% foreach (var dinner in Model) { %>
<li>
<a href="/Dinners/Details/<%=dinner.DinnerID %>">
<%= Html.Encode(dinner.Title) %>
</a>
on
<%= Html.Encode(dinner.EventDate.ToShortDateString())%>
#
<%= Html.Encode(dinner.EventDate.ToShortTimeString())%>
</li>
<% } %>
</ul>
</asp:Content>
They then state that instead of using an <a> tag that you can use the Html helper like so:
<%= Html.ActionLink(dinner.Title, "Details", new { id=dinner.DinnerID }) %>
The question is: Isn't it still important to Html.Encode the dinner.Title from the Model when using this approach? If not, why not? If so, is there a way to use the Html.ActionLink and still use Html.Encode?
Html.ActionLink already calls Encode, internally (see the source). You don't want to do it twice.

Please help me clean up my ASP.NET MVC tag soup

I'm creating a menu for my web site. It's got a hierarchical structure.
Here's what I want:
Great Grandparent
Grandparent
Parent
Sibling1
Sibling2
Sibling3
Self
Child1
Child2
Child3
HTML:
<ul>
<li>Great Grandparent
<ul>
<li>Grandparent
<ul>
<li>Parent
<ul>
<li>Sibling1</li>
<li>Sibling2</li>
<li>Sibling3</li>
<li><strong>Self</strong>
<ul>
<li>Child1</li>
<li>Child2</li>
<li>Child3</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
Here's the code from my ASPX page. This works:
<% For Each Ancestor As Node In ViewData("Ancestors")%>
<ul>
<li><%=Html.RouteLink(Html.Encode(Ancestor.Name), "IdOnly", New With {.id = Ancestor.Id})%>
<% Next%>
<ul>
<% For Each Sibling As Node In ViewData("Siblings")%>
<li><%=Html.RouteLink(Html.Encode(Sibling.Name), "IdOnly", New With {.id = Sibling.Id})%></li>
<% Next%>
<li><strong><%=Html.Encode(Model.Name)%></strong>
<% If Model.Children.Count > 0 Then%>
<ul>
<% For Each Child As Node In ViewData("Children")%>
<li><%=Html.RouteLink(Html.Encode(Child.Name), "IdOnly", New With {.id = Child.Id})%></li>
<% Next%>
</ul>
<% End If%>
</li>
</ul>
<% For Each Ancestor As Node In ViewData("Ancestors")%>
</li>
</ul>
<% Next%>
That ASPX page seems pretty hard to read. How can I clean that up?
The better solution would be to create a html helper if it doesn't already exist.
Take a look at this question to get ideas on how to do this
ASP.NET MVC HtmlHelper extensions for YUI controls (Yahoo User Interfaces)?
One thing I would recommend is checking the count of items supposed to be in "li" tags before you write the parent tags, other wise you could end up with empty "ul" tags which will not validate. If that doesn't make sense I'll elaborate later when I have some more time. As far as the formatting goes, perhaps some more indentation might make it easier to read.

Resources