我需要在 Swift Vapor 应用程序中创建一个复杂的 html 表格。
问题是:Leaf 似乎不支持像 #(somevar += 1) 这样的变量计数,也不支持像 #(somevar1 + somevar2) 这样的连接字符串变量
所以我决定在 App 中创建我的复杂表格并将其传输到变量中的 html 模板。 (在 php 中,我一直习惯这样做)
在模板中我会这样调用变量
#(table)
但事实证明,我得到的是纯 html 代码,因为 leaf 转义了所有变量。
但是有 #raw() 函数可以打印出纯 html。
我也是
<!DOCTYPE html>
<html lang="de">
<head><title>Server</title></head>
<body>
<..>
<form action="parameters" method="post">
// here is the thing: leaf gets a html table within the string 'table'.
// If I do it like that lead doesn't recognize #(table) as a leaf variable.
#raw( #(table) )
<button type="submit">Save</button>
</form>
</body>
</html>
只是发现 #raw() 不查找变量,而只是打印出 {} 之间未转义的内容。所以我仍然进入网站“#(table)”而不是复杂的 html 表格。
所以现在我卡住了。如何将 App 生成的 html 代码作为 html 放入模板中?
可能我做错了。我如何在叶模板中获取 html 代码?还是我必须自己发送整个页面,作为带有 header 的 http 流……?
提前致谢, 汤姆
最佳答案
更新
自 Vapor 3 以来 #raw(<var>)
默认情况下,标记未嵌入 --template=web
在创建 Vapor Leaf 项目时。我必须在我的 configure.swift
中手动注册它文件:
/// Leaf add tags
services.register { container -> LeafTagConfig in
var config = LeafTagConfig.default()
config.use(Raw(), as: "raw") // #raw(<myVar>) to print it as raw html in leaf vars
return config
}
https://docs.vapor.codes/3.0/leaf/custom-tags/
此外,如果有人正在寻找可用标签的完整列表,您可以简单地搜索 TagRenderer
在 vapor 依赖项中,如果你想创建自定义标签,这是要遵守的协议(protocol):
希望对你有帮助
关于html - Swift Vapor Leaf 在变量中传递 html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44524372/