Skip to content

Question: Is there a better way to render blocks? #330

@wombat2k

Description

@wombat2k

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

public extension Template {
    func render(block: String, _ context: Context) throws -> String {
        let context = context
        var append = false
        var collectedTokens = [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)
            }
        }

        let parser = TokenParser(tokens: collectedTokens, environment: context.environment)
        let nodes = try parser.parse()
        return try renderNodes(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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions