You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I tried to define a custom tag that accepts a body, but the body is provided to the tag as [Syntax], which is not something that converts to the required return type of LeafData.
How can I capture the body, and return if necessary?
For context, I'm trying to create requireRole(role) tag, that returns the body if the role matches the role of the current user;
In the tag class I retrieve the body using ctx.requireBody(), which, as mentioned, returns an array of Syntax elements.
finalclassRequireRoleTag<A>:LeafTagwhere A:RoleAuthenticatable{init(userType:A.Type){}func render(_ ctx:LeafContext)throws->LeafData{try ctx.requireParameterCount(1)letbody=try ctx.requireBody()
guard let requiredRole = ctx.parameters[0].string else{throw"role is not a string"}
guard let req = ctx.request,let role =getRole(req: req)else{return.trueNil
}
if role == requiredRole {
// This doesn't work
return body
}return.trueNil
}privatefunc getRole(req:Request)->String?{leta= req.auth.get(A.self)return a?.role.description
}}
The text was updated successfully, but these errors were encountered:
+1, this feels like it should be straightforward as #requireRole is essentially syntactic sugar over #if.
A slightly more complex case that I was hoping to solve with a custom tag is injecting common variables without needing to set them in the view model. For example...
When building re-usable templates such as navigation, there are many use-cases for wanting common data available without needing to add all that common context to the view model/controller. I guess this could all be bundled up into some common view model type, but that's still extra code that's required.
I tried to define a custom tag that accepts a body, but the body is provided to the tag as
[Syntax]
, which is not something that converts to the required return type ofLeafData
.How can I capture the body, and return if necessary?
For context, I'm trying to create
requireRole(role)
tag, that returns the body if the role matches the role of the current user;In the tag class I retrieve the body using
ctx.requireBody()
, which, as mentioned, returns an array ofSyntax
elements.The text was updated successfully, but these errors were encountered: