Search code examples
asp.net-mvc-4spark-view-engine

C# Code Block in Spark View Engine?


Can the Spark View Engine handle C# Code blocks like Razor can? For example, I have the following in Razor:

@{
   var currentController = ViewContext.RouteData.Values["controller"] as string ?? "Home";
   var currentAction = ViewContext.RouteData.Values["action"] as string ?? "Index";
   var currentPage = (currentController + "-" + currentAction ).ToLower();
}

What would the equivalent of that be in the Spark View Engine? I am then using the code like so in Razor, and I want to do same in Spark.

<li @(currentPage == "home-index" ? "class = current" : "")>
    @Html.ActionLink("Home", "Index", "Home")
</li>

Solution

  • use the # to indicate a line is code only, so:

    # var currentController = ViewContext.RouteData.Values["controller"] as string ?? "Home";
    # var currentAction = ViewContext.RouteData.Values["action"] as string ?? "Index";
    # var currentPage = (currentController + "-" + currentAction ).ToLower();
    

    also, if all you're doing is declaring variables, you could instead just do:

    <var currentController="ViewContext.RouteData.Values['controller'] as string ?? 'Home'" />
    

    your usage would look like:

    <li class="current?{currentPage == "home-index"}">
        ${Html.ActionLink("Home", "Index", "Home")}
    </li>