How do I capture output from tag renderer?

21 views
Skip to first unread message

Phil Gates-Idem

unread,
Jul 8, 2013, 4:57:17 PM7/8/13
to rapt...@googlegroups.com
I want to be able to do something like this:
<os:Section>
    <os:SectionHeader>Options</os:SectionHeader>
    <os:SectionBody>(body)</os:SectionBody>
</os:Section>

I want my SectionHeader and SectionBody tag renderers to be able to capture their output. They will then call _section.setHeader(headerHTML) and _section.setBody(bodyHTML) respectively. My first intuition was to swap out the context but is there a "best practice" approach to doing this?

Patrick Steele-Idem

unread,
Jul 8, 2013, 5:34:03 PM7/8/13
to rapt...@googlegroups.com
You can use the context.captureString(func) method to capture the output written to the current rendering context during the invocation of the provided function. Also, when the Raptor Templates runtime invokes the "render" method for a tag handler, the input argument will contain an "invokeBody" method if the tag has body content. The "invokeBody" method can be called to render the nested content for the current tag to the current rendering context. Therefore, if you want to capture the body content for a tag to a string you can do something similar to the following:

var bodyContent;

if (input.invokeBody) { // invokeBody will only be available if there is nested content
    bodyContent = context.captureString(
        function() {
            // Invoke the function that will render the
            // body of the tag to the current context
            input.invokeBody();      
        });   
} else {
    bodyContent = '';
}

--Patrick

Phil Gates-Idem

unread,
Jul 8, 2013, 5:36:51 PM7/8/13
to rapt...@googlegroups.com
Awesome! Works perfectly. Thanks!
Reply all
Reply to author
Forward
0 new messages