RazorEngine(MVC之外) - 可以在不明确指定模型类型的情况下注册模板?

时间:2022-09-26 19:03:33

Is it possible to register templates without specifying the model type?

是否可以在不指定模型类型的情况下注册模板?

Currently it works like so (taken from: documentation):

目前它的工作原理如下(取自:文档):

//Code to get template string from file xxx.cshtml into template variable
Razor.Compile(template, typeof(SomeModel), "complex");

However I would like to precompile the templates taking the model type definition from the @model or @inherits directive in the view. Is this possible?

但是,我想从视图中的@model或@inherits指令中预先编译模板类型定义的模板。这可能吗?

2 个解决方案

#1


1  

Razor.Compile doesn't require a model type to be specified when compiled.

Razor.Compile在编译时不需要指定模型类型。

public class ActualType { public string Firstname {get;set;}}

Razor.Compile("@Model.Firstname", "outputname");
Razor.Run<ActualType>(model, "outputname");

Should run for you just fine.

应该为你奔跑就好了。

The @model declaration isn't available yet. Currently there's a patch for it on the list of patches page ( http://razorengine.codeplex.com/SourceControl/list/patches ), however it only adds design time support and not runtime. The current reason for this is because the MVC RazorEngine already does this and we didn't want confusion as to whether this razor engine is the same as the MVC razor implementation (a lot of people think that)

@model声明尚不可用。目前在补丁列表页面(http://razorengine.codeplex.com/SourceControl/list/patches)上有一个补丁,但它只增加了设计时支持而不是运行时。目前的原因是因为MVC RazorEngine已经这样做了,我们不想混淆这个剃刀引擎是否与MVC剃刀实现相同(很多人都认为)

We're looking at implementing it for v2.2 when we both have time to work on it.

当我们都有时间研究它时,我们正在考虑为v2.2实现它。

As long as the model you pass in matches the required parameters in use by the template you might be good to go.

只要您传入的模型与模板使用的必需参数匹配,您就可以开始使用。

It actually shouldn't be too difficult to modify the patch to force the template to use the @model declaration as it's template<t>. I could work on it tonight possibly if it's a serious requirement. I'm curious as to why you don't want to include it in the compilation declaration unless you're worried about changing models or you don't know the model beforehand...but in the end you always know the type before you pass it in.

实际上,修改补丁以强制模板使用@model声明作为模板 实际上应该不会太困难。如果这是一个严肃的要求,我今晚可以继续努力。我很好奇为什么你不想把它包含在编译声明中,除非你担心改变模型或者你事先不知道模型......但最后你总是知道你之前的类型传递它。

#2


0  

Looking at the source, yes.

看来源,是的。

The modelType parameter specifies a default type, and itself defaults to dynamic if omitted.

modelType参数指定默认类型,如果省略,则默认为dynamic。

However, if the view explicitly specifies its base type, that should override it.

但是,如果视图显式指定了其基本类型,那么应该覆盖它。

Note that I haven't tried it; I'm just deducing from the source.

请注意,我没有尝试过;我只是从源头上推断出来的。

#1


1  

Razor.Compile doesn't require a model type to be specified when compiled.

Razor.Compile在编译时不需要指定模型类型。

public class ActualType { public string Firstname {get;set;}}

Razor.Compile("@Model.Firstname", "outputname");
Razor.Run<ActualType>(model, "outputname");

Should run for you just fine.

应该为你奔跑就好了。

The @model declaration isn't available yet. Currently there's a patch for it on the list of patches page ( http://razorengine.codeplex.com/SourceControl/list/patches ), however it only adds design time support and not runtime. The current reason for this is because the MVC RazorEngine already does this and we didn't want confusion as to whether this razor engine is the same as the MVC razor implementation (a lot of people think that)

@model声明尚不可用。目前在补丁列表页面(http://razorengine.codeplex.com/SourceControl/list/patches)上有一个补丁,但它只增加了设计时支持而不是运行时。目前的原因是因为MVC RazorEngine已经这样做了,我们不想混淆这个剃刀引擎是否与MVC剃刀实现相同(很多人都认为)

We're looking at implementing it for v2.2 when we both have time to work on it.

当我们都有时间研究它时,我们正在考虑为v2.2实现它。

As long as the model you pass in matches the required parameters in use by the template you might be good to go.

只要您传入的模型与模板使用的必需参数匹配,您就可以开始使用。

It actually shouldn't be too difficult to modify the patch to force the template to use the @model declaration as it's template<t>. I could work on it tonight possibly if it's a serious requirement. I'm curious as to why you don't want to include it in the compilation declaration unless you're worried about changing models or you don't know the model beforehand...but in the end you always know the type before you pass it in.

实际上,修改补丁以强制模板使用@model声明作为模板 实际上应该不会太困难。如果这是一个严肃的要求,我今晚可以继续努力。我很好奇为什么你不想把它包含在编译声明中,除非你担心改变模型或者你事先不知道模型......但最后你总是知道你之前的类型传递它。

#2


0  

Looking at the source, yes.

看来源,是的。

The modelType parameter specifies a default type, and itself defaults to dynamic if omitted.

modelType参数指定默认类型,如果省略,则默认为dynamic。

However, if the view explicitly specifies its base type, that should override it.

但是,如果视图显式指定了其基本类型,那么应该覆盖它。

Note that I haven't tried it; I'm just deducing from the source.

请注意,我没有尝试过;我只是从源头上推断出来的。