首页 > 解决方案 > 使用 RazorEngine 创建纯文本模板

问题描述

空白(尤其是换行符)存在很多问题,其中剃刀表达式与纯文本混合。对付他们是相当困难的。

例如 - 不幸的是,整个句子将被换行符分割:

@if(something)
{
@This is some text
}
and this rest of this text

另一个例子——我使用了一些自定义助手来有条件地渲染文本。但是当条件不满足时,会呈现空行。

@Html.IfNotNull("Some text 1",@Model.prop1)
@Html.IfNotNull("Some text 2",@Model.prop2)  //false
@Html.IfNotNull("Some text 3",@Model.prop3)

呈现给:

Some text 1

Some text 3

但是我需要:

Some text 1
Some text 3

有什么建议么?也许有更适合我的情况的模板引擎?

标签: razorrazorengine

解决方案


Razor 从字面上解释你的空间,所以你总是可以把它们写在同一行:

@if (true) { <text>This is some text</text> }and this rest of this text

@Html.IfNotNull("Some text 1",@Model.prop1) @Html.IfNotNull("Some text 2",@Model.prop2) @Html.IfNotNull("Some text 3",@Model.prop3)

输出:

This is some text and this rest of this text

Some text 1  Some text 3

推荐阅读