string - 如何在 Koreander 模板中取消转义 html(字符串)?
问题描述
我正在使用lukasjapan/koreander库及其 ktor 支持版本,当我从 kor 文件中调用 ViewModel(Res class) topbar 变量时,它会转义 html 字符串。
这就是我调用 ktor 来提供网页的方式:
suspend fun main() = coroutineScope<Unit> {
System.setProperty(SimpleLogger.DEFAULT_LOG_LEVEL_KEY, "TRACE")
embeddedServer(
Netty,
port = 80,
module = Application::module
).apply { start(wait = true) }
}
data class Res(val topbar: String)
fun Application.module() {
install(KoreanderFeature)
routing {
get("/") {
val text = javaClass.getResource("/templates/topbar.kor").readText()
val resource = Res(Koreander().render(text, Any()))
call.respondKorRes("/templates/index.kor", resource)
}
}
}
这是我的 topbar.kor 文件:这只是为了测试,所以我把它做得很小。
.navbar-custom
%ul.list-unstyled.topnav-menu.float-right.mb-0
%li.d-none.d-sm-block
%form.app-search
这是我的 index.kor 文件:
%html
%head
%body
%p.hello Hello World!
$topbar / <- problem here, this has escaped html, browser interpret it as text
解决方案
我从 David Eriksson @官方kotlinlang-slack那里得到了答案
我们可以使用:unsafehtml
过滤器来有效地绕过 HTML 转义:
%html
%head
%body
%p.hello Hello World!
:unsafehtml $topbar
推荐阅读
- sql-server - Azure 本地管道 CMD 错误:migrate.exe 返回错误“用户'NT AUTHORITY\ANONYMOUS LOGON'登录失败”
- python-3.x - 在 django 中使用默认的外键模型
- clips - 剪辑专家系统
- reactjs - DRF“未经授权:
" 当 React FE 中存在 Authorization 标头时 - sql - 按日期按子查询对条目进行聚类/计数
- python - InvalidArgumentError:默认 MaxPoolingOp 仅在设备类型 CPU 上支持 NHWC
- c - 从 txt 文件读入结构数组
- html - 如何使用css设置滚动条图像?
- android - 如何让协程等待下载(Unity android)?
- python - 使用 pandas Dataframe 将列转换为行索引