How to Pass Data from Using Post/Form Leaf Template

Vapor pass data from postgres to a leaf template

You're correct in that you need to handle the future but you should use flatMap since the render call returns a future. So your code should look like:

func routes(_ app: Application) throws {
app.get("all") { req -> EventloopFuture<View> in
return Todo.query(on: req.db).all().flatMap { todos in
let context = TodoContext(todos: todos)
return req.view.render("index", context)
}
}
}

How to pass ResultSet from MySql for Swift to leaf template contest

You should be able to fix this by manually setting the type of the dictionary array to Node:

drop.get("report") {req in
let data = try mysql.execute("select * from things")
let dataNode = Node.array(data.map({ return Node.object($0) }))
return try drop.view.make("report", ["data":dataNode])
}

Thymeleaf: pass input text as parameter in form action

This doesn't work like this. Like all server side technologies Thymeleaf templates are executed before being sent to the user, so it can't know what the user will enter and insert it into the action attribute.

You could do something similar with JavaScript, but it would be much easier to use a regular HTML form. That requires you not to use a path variable, but a request parameter in your controller:

@PostMapping("/postEndpoint")
public String pidUserSubmit(@RequestParam(name = "myid") String myid) {
log.debug("*** MY ID: {}", myid);
return "redirect:/someOtherPage";
}

<form th:action="@{/postEndpoint}" th:object="${mymodelobject}">
<input name="myid" type="text" th:value="*{myid}">
</form>

(Don't forget to use th:object with an object that has a property myid, if you want to use the *{...} syntax.)



Related Topics



Leave a reply



Submit