RazorCandle; razor from command line

RazorCandle is an opensource command line utility to render razor templates and save the result to the disks that I built last week. The reason why I needed this is a matter for another post.

RazorCandle is deliver as a single file executable that you can download right on the github page.

How it works

The command line options are:

RazorCandle.exe source  [destination] [/M]  [/V]

source         Specifies the source razor file.
[destination]  Specify the output file. By default is the same name as the
               source with the html extension.
[/?]           Show Help
[/M]           Json model as string to the model.
[/V]           Verbose mode. Show result in the output

Sub Templates

I have implemented sub templates, so for instance you can invoke a subtemplate as follows:

<html>
    <head>
    <title>Hello</title>
    </head>
    <body>
    @Html.Partial("Nested.cshtml")
    </body>
</html>

Relative directories

You have to keep in mind that all the code that is executed inside a razor template has as a current directory the directory where the template is located, even for subtemplates.

So you can do things like:

<html>
    <head>
    <title>Hello</title>
    </head>
    <body>
    @Html.Partial("..\\relative\\Nested.cshtml")

    @foreach (var file in System.IO
             .Directory.GetFiles("subtemplates\\", "*.cshtml"))
    {
       <script type="text/javascript" src="@file"></script>
    }

    @foreach (var file in System.IO
             .Directory.GetFiles("scripts\\", "*.js")
                       .Select(f => f.Replace("\\", "/")))
    {
       <script type="text/javascript" src="@file"></script>
    }

    </body>
</html>

Template Model

RazorCandle also supports models. If you have a template like:

<html>
    <head>
    <title>Hello</title>
    </head>
    <body>
        <p>hello @Model.FirstName @Model.LastName</p>
    </body>
</html>

You can render it as:

RazorCandle.exe myfile.cshtml /M="{FirstName: 'Alberto', LastName: 'Perez' }"

The string in the M parameter is a JSON.

What’s next?

A .Net version of middleman?

Fork me on github!


blog comments powered by Disqus
  • Categories

  • Archives