2009/10/12 Johannes Kuhs <[email protected]>

> I was wondering if there is a way to inherit the value of an attribute and
> to extend it with new content. Basically, I’m looking for something like
> inherit=”true” for list-attributes but for normal attributes.
>
> Following is a quick example of what I would like to do:
>
> Initialization in /WEB-INF/tiles-defs.xml:
>
> <definition name=" test.definition" template="/layouts/classic.jsp">
>    <put-attribute name="title" value="Initial title value" />
> </definition>
>
> Inherit and extend in /test.jsp, note inherit=”true” in the <putAttribute>
> tag (which does not work but I think it shows what I want to achieve):
>
> <tiles:insertDefinition name="test.definition">
>  <tiles:putAttribute name="title" inherit=”true”>
>      --This text gets appended to the initial value of the title attribute
>  </tiles:putAttribute>
> </tiles:insertDefinition>
>
> Now, when inserting the attribute with the following code:
>
> <tiles:insertAttribute name="title"/>
>
> The following title should be displayed:
> Initial title value--This text gets appended to the initial value of the
> title attribute
>


You can do it with a preparer. For example, add an "auxiliar" attribute in
which you put the text to add, something like:

<tiles:insertDefinition name="test.definition"
preparer="my.package.MyPreparer">
 <tiles:putAttribute name="title-aux">
     --This text gets appended to the initial value of the title attribute
 </tiles:putAttribute>
</tiles:insertDefinition>

And in your preparer:

public void execute(TilesRequestContext tilesContext,
            AttributeContext attributeContext) {
        String auxValue =
attributeContext.getAttribute("title-aux").getValue().toString();
        String originalTitle =
attributeContext.getAttribute("title").getValue().toString();
        attributeContext.putAttribute("title", new Attribute(originalTitle +
auxValue, null);
    }

HTH
Antonio

Reply via email to