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
有什么建议么?也许有更适合我的情况的模板引擎?
解决方案
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
推荐阅读
- vue.js - 如何将 Bootstrap-Vue 输入的旧值传递给它调用的方法?
- java - java:如果在构造函数中没有参数作为参数,是否有必要调用 super()?
- java - Flink 计时器未按时执行
- java - jackson/json 模式 - 用外部 json 模式生成器模块替换 ISO8601Utils
- google-cloud-platform - 在 GCP 中使用 IP 地址终止虚拟机
- dart - 我无法通过 Flutter onPress 传递其他页面
- hbase - HBase:如何将列转换为列,而不是列
- join - 根据条件左连接
- xaml - 双重同时更改属性只会触发一个 propertyChanged 事件,第二个被忽略
- grafana - 有没有办法使用即时查询创建 Grafana 模板变量?