Inline Helper Example

Jan 19, 2011 at 12:50 PM

I'm having a problem getting the inline helper example working. I get a TemplateCompilationException and the Errors list contains the following:

{c:\Users\dev\AppData\Local\Temp\cyfsewxw.0.cs(25,18) : error CS1002: ; expected}

My References are as follows:

Microsoft.CSharp, RazorEngine.Core, RazorEngine.Templates, System, System.Core, System.Data, System.Data.DataSetExtensions, System.Web.Razor, System.Xml, System.Xml.Linq

Here's the code...

    using System;
    using RazorEngine;
    class Program
        static void Main(string[] args)
            string template =
  @"@helper MyMethod(string name) {
      Hello @name
  @MyMethod(Model.Name)! Welcome to Razor!";
            string result = Razor.Parse(template, new { Name = "World" });

Jan 19, 2011 at 1:12 PM

Can you try:

@"helper MyMethod(string name) {
    <text>Hello @name</text>
@MyMethod(Model.Name)! Welcome to Razor!";

The Razor parser can't define between a code block and a markup block if there are no markup tags present at the beginning of the markup section. The <text> element allows it to switch to "markup" mode for that section, and the <text> tags are automatically removed for you.

Jan 19, 2011 at 3:45 PM

That did it. Thanks!

Jan 21, 2011 at 8:20 PM
Edited Jan 21, 2011 at 8:23 PM

I've upgraded to v2.0 and the inline helper no longer works. I get a TemplateCompilationException with the following message in the errors collection:

{c:\Users\dev\AppData\Local\Temp\v5ureqby.0.cs(23,20) : error CS0234: The type or namespace name 'Template' does not exist in the namespace 'RazorEngine' (are you missing an assembly reference?)}

The code is exactly the same as above with the inclusion of the <text> tag.

Jan 21, 2011 at 9:04 PM

Ah, that'll be corrected in 2.1