Home

Using IronRuby in views

Starting with build 2144, Vici MVC supports pluggable view engines. This means you can write your own view engine and use it with Vici MVC.

As everyone knows by now, Ruby is one of the hottest programming languages around, so deciding on a new view engine for Vici MVC was a no-brainer. Obviously, IronRuby was the best choice because of the tight integration with the .NET Framework.

Adding the IronRuby view engine

  • Download the IronRuby view engine from the download page and reference the DLLs from your project
  • Add the following line to your web.config appSettings:
    • <add key="Mvc.ViewEngine.rb.htm" value="Vici.Mvc.IronRuby.RubyViewEngine, Vici.Mvc.IronRuby"/>
  • Optionally, add Ruby's search path to the appSettings:
    • <add key="IronRuby.SearchPaths" value="C:\IronRuby\Lib\ironruby;C:\IronRuby\Lib\ruby\1.8"/>

That's it. Now you can simply create a template using IronRuby as the scripting language. You only have to name the views "*.rb.htm".

IronRuby Views

For example:

<html>
<body>

<table>
<tr><th>Row</th><th>Description</th></tr>
<% 1.upto(10) do |x| %>
<tr><td><%= x %></td><td>This is a description for row <%= x %></td></tr>
<% end %>
</table>

</body>
</html>

Of course you can use ViewData variables just like in "normal" Vici MVC templates. Simply refer to them by name:

<html>
<body>
<h1>Users</h1>
<table>
<tr><th>Name</th><th>E-mail address</th></tr>
<% users.each do |user| %>
<tr><td><%= user.Name %></td><td><%= user.EMail %></td></tr>
<% end %>
</table>

</body>
</html>

You should make sure you stick to Ruby's naming conventions, meaning that variables should not start with an uppercase character. You're also allowed to use "_" delimited variables. The view engine will map this to mixed case when looking up variables in the viewdata collection.

Vici MVC has a few built-in variables that start with a "@". In Ruby these kind of variables are reserved for instance variables, so these have to be written as _varname.

Examples:

.NET Variable (ViewData)Ruby variable(s)
Useruser
useruser
userNameuserName or user_name
UserNameuserName or user_name
@View_view

More documentation will follow