html - Swift Vapor Leaf 在变量中传递 html

标签 html swift vapor leaf

我需要在 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):

Available Tags

希望对你有帮助

关于html - Swift Vapor Leaf 在变量中传递 html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44524372/

相关文章:

javascript - 如何解码 url 中的 html?

c# - 如何绑定(bind)到 Blazor 中集合/列表中的元素?

javascript - CSS 将 Fullpage.js 中的箭头更改为自定义图像?

swift - 无法在 Swift 4 中为 WebKit View 创建 IBOutlet

swift - makeNode 中的上下文是什么(在 : Context) vapor 3?

swift - 使用 Vapor 3 更改主机名和端口

python - 尝试使用 soup.select 和 soup.find_all 提取网址

swift - 尝试在 Swift 中通过 Alpha 隐藏 SearchBar

ios - 未使用的场景从 Storyboard 中临时隐藏

vapor - 如何在 Swift 3 中将 response.body 从 Vapor 转换为 String?