How to get Intellisense in your templates

May 31, 2011 at 10:04 PM

Didn't think I would get it to work but in the end I got Intellisense working :-) I hate working without it especially in non compile-time compiled templates.

I saw some issues/discussions about it so I thought I'd post how I got it working (without engine source changes!)

I experimented with the current RazorEngine assembly. (I have MVC 3 installed as well).

I added a base template class (all in namespace SpotlightUpdateMail):

    public class SpotlightModel<T> : TemplateBase<T>
    {
        public T Model
        {
            get { return base.Model; }
        }
    }

So only redeclaring Model property. And registered it with Razor:

    Razor.SetTemplateBase(typeof(SpotlightModel<>));

And in the template I specified as first line:

@inherits SpotlightUpdateMail.SpotlightModel<SpotlightUpdateMail.SpotlightUnusedCodeReminder>

And SpotlightUnusedCodeReminder is a class that inherits from ModelBase which in turn inherits from DynamicObject (which I think is needed by RazorEngine)

Hope this is somehow usefull to any reader :-)

Regards, Jacco

May 31, 2011 at 10:12 PM

Forgot to mention I added the templates to my console application project as cshtml. This makes the mvc 3 editor is used.

May 31, 2011 at 11:33 PM

Hm, didn't test well. Somewhere along the line I had to trim the @inherits line... to prevent exceptions.

Coordinator
Jun 1, 2011 at 3:03 PM

Yeah, the parser doesn't parse @inherits or @model. You can apply the patch I uploaded to allow @model statements.

Jun 8, 2011 at 4:59 PM

@Jacco: could you go in some more detail on how to enable intellisense  ?
@BuildStarted: where can I find the patch? Or do I need to download source code and compile myself? 

Aug 14, 2011 at 7:59 AM
Edited Aug 14, 2011 at 8:00 AM

thanks works better than mine !

I found two ways to get intellisense in the template. 

Both require a small hack to allow it but I am quite okay with it since I can hide it away in my GetResource call ( I set my template.cshtml to EmbeddedResource and then return a string  in the following GetResource call);

        public static String GetResource()
        {
            var contents = String.Empty;
            var resource = String.Format("{0}.{1}.cshtml""<TemplateNamespace>""<Template>"); // whereever the file was embedded 
            var fileStream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resource);
            using (var reader = new StreamReader(fileStream))
            {
                contents = reader.ReadToEnd();
            }
            var start = contents.IndexOf("@*start*@");
            return contents.Substring(start + 9);
        }

Note my hack is removing the beginning of my returned template string (which will break things if left out should you follow the remaining steps) starting at the token @*start*@. 

1.) Start your file with the following text put the datatype of your choice in there.

@model <Fully qualified datatype> @*start*@

or

2.)

@using Ellevate.T4Razor.Model.Data
@functions  {
    public ModelData Model { getset; }
    public String Test { get; } // Remember you would have to have a base class that cares about properties other than Model.
}@*start*@

/////  Razor Engine Code
            Razor.SetTemplateBase(typeof (BaseTemplate<>));
            ModelData modelData= new ModelData();
 
            var template = MyTemplate.GetResource();
            Razor.Parse(template, modelData, "template");
            Razor.Run("template");