groue / GRMustache.swift

Flexible Mustache templates for Swift
http://mustache.github.com/
MIT License
594 stars 155 forks source link

Post-rendering Lambda? #83

Closed agiletortoise closed 2 years ago

agiletortoise commented 2 years ago

Apologies if I'm missing it, but is there a way to have a lambda/filter/whatever that post-processes the rendering of its nested content. I want to have something like:

{{#section}}{{value}}{{/section}}

But where the section has access to and post-processes the renderer result of its nested content. Right now the Lambda get {{value}} as its string input. I want it to get the post-rendered version to process.

agiletortoise commented 2 years ago

I should probably clarify more, that I do not want what HTMLEscape does, intervening in individual tags, but want the section to have the final rendering of its child content to manipulate, not just modify each sub tag as it is renderered.

groue commented 2 years ago

Hello @agiletortoise,

Please check the Advanced Boxes documentation chapter, and probably one of the render, willRender or didRender initializers.

You will have to read the documentation.

agiletortoise commented 2 years ago

Great, thanks for the pointer...the below seems to accomplish what I need by processing the inner tags, then making the modifications...putting here for future visitors, or if you happen to see any clear problems with this approach.

final class TestHelper : MustacheBoxable {

    var mustacheBox: MustacheBox {
        return MustacheBox(
            value: self,
            filter: Filter(filter),
            render: render)
    }

    private func filter(_ rendering: Rendering) throws -> Rendering {
        // do my thing here...
        return Rendering("New Value")
    }

    private func render(_ info: RenderingInfo) throws -> Rendering {
        let rendering = try info.tag.render(info.context)
        return try self.filter(rendering)
    }

}