How can I render a variable properly without confusing the Razor engine?
I am trying to render CSS using the Razor view engine (yes, I know it was designed for xml-style languages).
My template looks like:
#@Model.ID {
top: @Model.Toppx;
left: @Model.Leftpx;
}
Of course this fails because Toppx
and Leftpx
arent properties of the model, Top
and Left
are. I cant put a space (eg @Model.Top px;
) because while this works from a templating perspective, its invalid CSS and Firefox ignores it.
Other templating languages (Freemarker, Velocity) would support it like: ${Model.Top}px;
and even the usual ASP.NET view engine supports it like: <%=Model.Top%>px;
How can I get this behaviour in Razor? I tried:
@:<text>@Model.Top</text>px
but that wouldnt compile.
I also tried: @Model.Top@:开发者_运维问答px;
but that didnt work either.
Note: I am using the standalone Razor (found at http://razorengine.codeplex.com/) with Mono 2.10
Thanks
Try explicitly expressing the code block with brackets:
#@(Model.ID) {
top: @(Model.Top)px;
left: @(Model.Left)px;
}
精彩评论