使用RazorEngine创建纯文本模板

时间:2018-06-28 15:27:43

标签: razor 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

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

1 个答案:

答案 0 :(得分:0)

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
相关问题