Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can I use the @model type directive in my template? #12

Closed
dalenewman opened this issue Apr 3, 2020 · 4 comments
Closed

Can I use the @model type directive in my template? #12

dalenewman opened this issue Apr 3, 2020 · 4 comments

Comments

@dalenewman
Copy link

It might just be me, but I get an error saying "The name 'model' does not exist in the current context" whenever I have a @model directive in my strongly typed template. It runs without it, but I'd rather have it there than not. I've been through the gamut trying fixes outlined in this and this stack overflow post.

@adoconnection
Copy link
Owner

adoconnection commented Apr 3, 2020

I might be wrong, but as I can see @model is not exactly a part of RazorEngine, it is ASP.NET that makes RazorEngine to know of this directive. I actually like this behavior. It let templates be flexible and use any keywords as directives @anything that are directly tied to TemplateBase, not only to Model.

Have a look:
If I use this template:

@model string
Hello @Model

RazorEngineCore (me) adds two directives:

@inherits RazorEngineCore.RazorEngineTemplateBase
@using System.Linq

@model string
Hello @Model

And then RazorEngine (Microsoft) will turn it in following code:
Notice how @model string turned into Write and WriteLiteral

#pragma checksum "vadrkp0x.pba" "{ff1816ec-aa5e-4d10-87f7-6f4963833460}" "7292c67fd2ce79b6a8a3b20c73e811dee742b3ad"
// <auto-generated/>
#pragma warning disable 1591
namespace TemplateNamespace
{
using System.Linq;
   public class Template : RazorEngineCore.RazorEngineTemplateBase
    {
        public async override global::System.Threading.Tasks.Task ExecuteAsync()
        {
            WriteLiteral("\r\n");
            Write(model); // HERE - default RazorEngine instance thought @model is a variable name
            WriteLiteral(" string\r\nHello ");
            Write(Model);
            WriteLiteral("\r\n");
        }
    }
}

Why it is so important for you to have @model directive in template?
I only see it is useful as a reference for devs

@dalenewman
Copy link
Author

dalenewman commented Apr 4, 2020

I only find including the @model directive useful because it lets my editor (Visual Studio 2019) know the type and subsequently offer intellisense. What I will do is leave it in my templates (for editing purposes) but remove it before processing it with your library.

Thanks so much for your response and your open source work!

@adoconnection
Copy link
Owner

you welcome

@adoconnection
Copy link
Owner

@dalenewman this is how you can use IntelliSense in your templates: #13

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants