Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Tapestry Quicky: ConditionalComment

DZone's Guide to

Tapestry Quicky: ConditionalComment

· Java Zone
Free Resource

What every Java engineer should know about microservices: Reactive Microservices Architecture.  Brought to you in partnership with Lightbend.

Here's a quicky I just put together for a client to generate IE conditional comments. This isn't a feature supported by Tapestry's JavaScriptSupport(for libraries; it is support for CSS files).

Fortunately, this is something that comes together in Tapestry in almost no code:

package com.fivoosh.components;
 
import org.apache.tapestry5.BindingConstants;
import org.apache.tapestry5.MarkupWriter;
import org.apache.tapestry5.annotations.Parameter;
 
/**
 * A component that wraps its body in an IE conditional comment.
 */
public class ConditionalComment {
 
    @Parameter(required = true, allowNull = false, defaultPrefix = BindingConstants.LITERAL)
    String condition;
 
    void beginRender(MarkupWriter writer) {
        writer.writeRaw(String.format("<!--[if %s]>", condition));
    }
 
    void afterRender(MarkupWriter writer) {
 
        writer.writeRaw("<![endif]-->");
 
    }
}

This is what it looks like in a template:

<t:conditionalcomment condition="lte IE 9">
    <t:remove>
        Fix IE8+9 lack of support for html5 elements and media queries
    </t:remove>
    <script src="${context:js/html5shiv.js}" type="text/javascript"/>
    <script src="${context:js/respond.js}" type="text/javascript"/>
    <script src="${context:js/media.match.js}" type="text/javascript"/>
</t:conditionalcomment>

And here's the markup it produces:

<!--[if lte IE 9]>
            
            <script type="text/javascript" src="/assets/ctx/z79f6b0bd/js/html5shiv.js"></script>
            <script type="text/javascript" src="/assets/ctx/zf8f53e99/js/respond.js"></script>
            <script type="text/javascript" src="/assets/ctx/z8aa0bee8/js/media.match.js"></script>
        <![endif]-->

And that's the whole point ... the URLs for the referenced JavaScript libraries are now Tapestry assets, and will have access to Tapestry's asset pipeline as unique resources, including GZip compression and minimization.

It is always important to me that Tapestry not get in the way: frameworks that lock you down and prevent you from accomplishing your goals should not be used. Tapestry has a long history, and there are certainly many areas that could have been implemented differently, or simply not implemented at all, but it's nice that most edge cases, like this one, have a simple solution.


Microservices for Java, explained. Revitalize your legacy systems (and your career) with Reactive Microservices Architecture, a free O'Reilly book. Brought to you in partnership with Lightbend.

Topics:

Published at DZone with permission of Howard Lewis Ship, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}