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'm trying to duplicate the functionality of Django-render-block. Basically I want the ability to only render a specific block in a template. I'm relatively new to Swift, so wasn't expecting much, but to my surprise, I got it to work like so
publicextensionTemplate{func render(block:String, _ context:Context)throws->String{letcontext= context
varappend= false
varcollectedTokens=[Token]()
for token in tokens {
if token.contents.description.starts(with:"block \(block)"){
append = true
}
if token.contents.description =="endblock \(block)"{
break
}
if append {
collectedTokens.append(token)}}letparser=TokenParser(tokens: collectedTokens, environment: context.environment)letnodes=try parser.parse()returntryrenderNodes(nodes, context)}}
Obviously this is very crude and relies on a textual representation of the token, but it does exactly what I need and as a bonus it worked with custom tags. Is there a better more elegant way to achieve this?
The text was updated successfully, but these errors were encountered:
I'm trying to duplicate the functionality of Django-render-block. Basically I want the ability to only render a specific block in a template. I'm relatively new to Swift, so wasn't expecting much, but to my surprise, I got it to work like so
Obviously this is very crude and relies on a textual representation of the token, but it does exactly what I need and as a bonus it worked with custom tags. Is there a better more elegant way to achieve this?
The text was updated successfully, but these errors were encountered: