Dear Wiki user, You have subscribed to a wiki page or wiki category on "Tapestry Wiki" for change notification.
The "Tapestry5HowToGetAnHTMLStringFromARenderCommandParameter" page has been changed by uklance: http://wiki.apache.org/tapestry/Tapestry5HowToGetAnHTMLStringFromARenderCommandParameter New page: Sometimes, you may want to specify a parameter in tapestry's markup language (TML) and instead of rendering the HTML directly to the response, you may wish to use it for some other purpose (eg use it in a javascript). The following is an example of a component dose the following: * Accepts a count parameter and a [[http://tapestry.apache.org/tapestry5/apidocs/org/apache/tapestry5/runtime/RenderCommand.html|RenderCommand]] parameter * Adds a wrapper div to the markup writer * Loops through each count and updates a the "current" property which may be referenced in the RenderCommand parameter * Renders the RenderCommand for each iteration through the loop * Uses @AfterRender to get the resultant HTML from the wrapper div * Removes the wrapper div from the markup so that it is not rendered to the response directly Page.tml {{{ <t:tmlToString t:count="5" t:id="tmlToString"> <p:renderMe> <div>foo ${tmlToString.current} bar</div> </p:renderMe> </t:tmlToString> }}} Page.java {{{ @InjectComponent @Property private TmlToString tmlToString; }}} TmlToString.java {{{ public class TmlToString { @Parameter @Property private RenderCommand renderMe; @Property @Parameter(defaultPrefix=BindingConstants.LITERAL, required=true) private int count; @Property private int current; @Inject private JavaScriptSupport javaScriptSupport; private Element wrappingDiv; @BeginRender RenderCommand beginRender() { return new RenderCommand() { public void render(MarkupWriter writer, RenderQueue queue) { wrappingDiv = writer.element("div"); List<RenderCommand> commands = new ArrayList<RenderCommand>(); for (int i = 0; i < count; ++ i) { final int finalI = i; commands.add(new RenderCommand() { public void render(MarkupWriter writer2, RenderQueue queue2) { current = finalI; queue2.push(renderMe); } }); } Collections.reverse(commands); // render commands are pushed to the front of the queue for (RenderCommand command : commands) { queue.push(command); } } }; } @AfterRender void afterRender(MarkupWriter writer) { writer.end(); String html = wrappingDiv.getChildMarkup(); wrappingDiv.remove(); javaScriptSupport.addScript("alert('%s')", html); } } }}} Result {{{ alert('<div>foo 0 bar</div><div>foo 1 bar</div><div>foo 2 bar</div><div>foo 3 bar</div><div>foo 4 bar</div>'); }}} --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
