gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-31 08:22:11 26 4
gpt4 key购买 nike

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

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com