简体   繁体   中英

Custom Leaf tags in Vapor 4

I'm following this tutorial:

https://theswiftdev.com/custom-leaf-tags-in-vapor-4/

In the absence of clarity, I tried putting the struct PathTag in configure.swift, then in the routes.swift.

struct PathTag: LeafTag {

    static let name = "path"

    func render(_ ctx: LeafContext) throws -> LeafData {
        let value = ctx.request?.url.path ?? ""
        return .string(value)
    }
}

I'm using Vapor 4, and I have GET and POST routes working OK on localhost:8080, but leaf is new. He says "We can use the this newly created path tag to construct a URL based on the current path with some additional query parameters like this: #path()?foo=bar."

Unsure what he means, I tried the following in the browser:

http://localhost:8080/path?foo=bar

Let's say you have a Vapor app running on localhost and you have a route set up, such as:

http://localhost:8080/a/route/to

In the .leaf file that gets rendered for this route, if you put:

<A href="#path()/somewhere">Click Me</A>

then when you click on the link, it will take you to:

http://localhost:8080/a/route/to/somewhere

I keep all my custom tags in a separate .swift file and just put the code to register it in configure.swift .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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