Swift Vapor Leaf在变量中传递html [英] Swift Vapor Leaf deliver html in a variable

查看:38
本文介绍了Swift Vapor Leaf在变量中传递html的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在Swift Vapor App内创建一个复杂的html表.

问题是:Leaf似乎不支持计算#(somevar + = 1)之类的变量,也不支持串联诸如#(somevar1 + somevar2)之类的字符串变量

因此,我决定在App内创建我的复杂表,并将其转移到变量内的html模板中.(在php中,我一直都在这样做)

在模板中,我将变量称为

 #(表) 

但是事实证明,由于叶子转义了所有变量,因此我得到了普通的html代码.

但是有#raw()函数可以像这样打印纯HTML.

我愿意

 <!DOCTYPE html>< html lang ="de">< head>< title>服务器</title></head>< body>< ..>< form action ="parameters" method ="post">//这是东西:leaf在字符串'table'中获取一个html表.//如果我像那样做,则线索不会将#(table)识别为叶子变量.#raw(#(table))< button type ="submit">保存</button></form></body></html> 

只是发现#raw()不会查找变量,而只是打印出{}之间的未转义内容.因此,我仍然进入网站#(table)",而不是我的复杂html表.

所以现在我被卡住了.如何将应用程序生成的html代码作为html获取到模板中?

可能我做错了.我如何在叶模板中获取html代码?还是我必须将整个页面然后发送给我自己,作为带有标题的HTTP流发送??

预先感谢,汤姆

解决方案

更新

由于Vapor 3,在创建Vapor Leaf项目时,默认未将 #raw(< var>)标记嵌入-template = web 中.我必须在 configure.swift 文件中手动注册它:

 ///叶子添加标签services.register {容器->中的LeafTagConfigvar config = LeafTagConfig.default()config.use(Raw(),as:"raw")//#raw(< myVar>)将其打印为叶vars中的原始html返回配置} 

希望这可以帮助

I need to create a complex html-table inside a Swift Vapor App.

Problem is: Leaf doesn't seem to support counting up variables like #(somevar += 1) nor concatenating string variables like #(somevar1 + somevar2)

So I decided to create my complex table inside the App and transfer it to the html template inside a variable. (in php I'm used to doing this all the time)

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.

But there is the #raw() function to print out plain html as such.

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 {}. So I still get in the website "#(table)" instead of my complex html-table.

So now I'm stuck. How do I get App-generated html code into the template as html?

Probably I am doing this all wrong. How do I get html code inside a leaf template at all? Or do I have to send the whole page then myself, as a http stream with a header…?

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. I had to register it manually in my configure.swift file :

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

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 :

Hope this can help

这篇关于Swift Vapor Leaf在变量中传递html的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆