adoconnection / RazorEngineCore

.NET6 Razor Template Engine
MIT License
576 stars 85 forks source link

Request: support @model directive #14

Open sergeyt opened 4 years ago

sergeyt commented 4 years ago

I've noticed it is not supported yet, right?

adoconnection commented 4 years ago

@model is controlled by RazorEngineCore itself so you dont need to use this directive and need to remove it from your template.

@using is supported too. Most likely you will need to explicitly link assembly https://github.com/adoconnection/RazorEngineCore/wiki/@Inject-and-referencing-other-assemblies#linking-assemblies (I was unable to parse @using-s and do it programatically in a reasonable time)

adoconnection commented 4 years ago

To keep intellisense suggestions use @Inherits instead of @model https://github.com/adoconnection/RazorEngineCore/wiki/Switch-from-RazorEngine-cshtml-templates#template

sergeyt commented 4 years ago

@adoconnection thanks for clarifications. Checked that @using works.

Note @inherites cannot be used for classes without parameterless constructor. So @model would be nice to support (e.g. just ignore it on compile time).

adoconnection commented 4 years ago

@sergeyt wont it be hidden surprice, if someone will put @model TypeA directive and then figure out it was ignored and replaced by RazorEngineCode?

sergeyt commented 4 years ago

@adoconnection it can be a documented limitation/behavior. @model is for VS intellisense wheen @inherits is not applicable

garryxiao commented 3 years ago

@adoconnection, parse the usings is little bit reasonable. Is it possible for me to do some work here? :) @sergeyt, I mention something under https://github.com/adoconnection/RazorEngineCore/discussions/50 may help.

adoconnection commented 3 years ago

@garryxiao sure, the only thing is I would like it to be done using Razor tools, sinse razor somehow parse them there should be a way to get usings collection. But I was not able to find out how.

There is always a plan B to use regex or something on a template string, I would like to avoid that.

304NotModified commented 3 years ago

Please include this in the wiki, or make the wiki public so I could add it :)

adoconnection commented 3 years ago

@304NotModified wiki is open now :)

304NotModified commented 3 years ago

Done! https://github.com/adoconnection/RazorEngineCore/wiki/@model-and-Visual-Studio-IntelliSense

304NotModified commented 3 years ago

Note @inherites cannot be used for classes without parameterless constructor.

Is this really true? I tried it and don't see the issue? (check IntelliSense, compiling and running?)

it can be a documented limitation/behavior. @model is for VS

I really prefer support for parsing @model and documenting this behavior. Maybe also with a better error message? If we use now @model MyModel, we get this error:

RazorEngineCore.RazorEngineCompilationException
Unable to compile template: j2rpxc2v.45u(7,7): error CS0103: The name 'model' does not exist in the current context
   at RazorEngineCore.RazorEngine.CreateAndCompileToStream(String templateSource, RazorEngineCompilationOptions options)
   at RazorEngineCore.RazorEngine.Compile[T](String content, Action`1 builderAction)

Any leads where to start to get this implemented?

benmccallum commented 3 years ago

I'm having an issue where I'm trying to use the example for @Include and Template, combined with the example for strongly-typed models using @inherit Base.

The problem is that the templating approach shows you should use a MyTemplateBase, but that's not generic, so I can't put @include MyTemplateBase<MyModel> at the top as it doesn't accept T. I tried making it generic, but got stuck here: image

benmccallum commented 3 years ago

It seems like maybe this wrapper is only required when the model is an anonymous type, based on here: https://github.com/adoconnection/RazorEngineCore/blob/a86a24bab26aadf9059d28c001a8c51898823680/RazorEngineCore/RazorEngineCompiledTemplate.cs#L94-L97

So in my case I can probably just remove that code. Will see how I go

adoconnection commented 3 years ago

It seems like maybe this wrapper is only required when the model is an anonymous type, based on here: So in my case I can probably just remove that code. Will see how I go

Right, without AnonymousTypeWrapper Razor will not be able to read properties of supplied anonymous object

seppo498573908457 commented 1 year ago

On the topic: The model is handled by the template, but if it happens @model directive is present, an exception is thrown at compile. Is there a way to ignore this directive and keep on compiling?