简体   繁体   English

Swift Vapor Leaf 在变量中提供 html

[英]Swift Vapor Leaf deliver html in a variable

I need to create a complex html-table inside a Swift Vapor App.我需要在 Swift Vapor App 中创建一个复杂的 html 表。

Problem is: Leaf doesn't seem to support counting up variables like #(somevar += 1) nor concatenating string variables like #(somevar1 + somevar2)问题是:Leaf 似乎不支持像 #(somevar += 1) 这样的变量计数,也不支持像 #(somevar1 + somevar2) 这样的连接字符串变量

So I decided to create my complex table inside the App and transfer it to the html template inside a variable.所以我决定在 App 中创建我的复杂表,并将其传输到变量内的 html 模板中。 (in php I'm used to doing this all the time) (在php中我一直都习惯这样做)

In the template I'd call the variable like在模板中,我将变量称为

#(table) 

but turns out, I'm getting the plain html code as leaf escapes all variables.但事实证明,当叶子转义所有变量时,我得到了纯 html 代码。

But there is the #raw() function to print out plain html as such.但是有 #raw() 函数可以打印出普通的 html。

So I do所以我愿意

<!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>

only to find out that #raw() does not look for variables but just prints out unescaped what is between the {}.只是发现#raw() 不查找变量,而只是打印出未转义的 {} 之间的内容。 So I still get in the website "#(table)" instead of my complex html-table.所以我仍然进入网站“#(table)”而不是我复杂的 html-table。

So now I'm stuck.所以现在我被困住了。 How do I get App-generated html code into the template as html?如何将 App 生成的 html 代码作为 html 放入模板中?

Probably I am doing this all wrong.可能我做错了。 How do I get html code inside a leaf template at all?如何在叶子模板中获取 html 代码? Or do I have to send the whole page then myself, as a http stream with a header…?还是我必须自己发送整个页面,作为带有标题的http流......?

Thanks in advance, Tom在此先感谢,汤姆

Update 更新

Since Vapor 3 the #raw(<var>) tag was not embedded by default in --template=web when creating your Vapor Leaf project. 从Vapor 3开始,在创建Vapor Leaf项目时,默认情况下未在--template=web嵌入#raw(<var>)标记。 I had to register it manually in my configure.swift file : 我必须在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/ https://docs.vapor.codes/3.0/leaf/custom-tags/

Also if someone is seeking for the complete list of available tags, you can silmply search for TagRenderer in vapor dependencies, which is the protocol to conform if you want to create a custom tag : 此外,如果有人正在寻找可用标签的完整列表,您可以在蒸汽依赖TagRenderer中轻松搜索TagRenderer ,如果您要创建自定义标签,这是符合的协议:

可用标签

Hope this can help 希望这可以提供帮助

Assuming you are not taking in user input to create this table, you should be able to use this: 假设您没有接受用户输入来创建此表,您应该能够使用:

#raw(table)

This does the same thing as #(table) , but it doesn't escape the HTML, so it will properly render. 这与#(table)的作用相同,但它不会转义HTML,因此它将正确呈现。 Just make sure you are not making yourself vulnerable to XSS attacks if you do this. 如果您这样做,请确保您不会让自己容易受到XSS攻击。

In vapor 4 it is:在蒸气 4 中是:

#unsafeHTML(variable) #unsafeHTML(变量)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM