|
Re: [JET2] Using abbreviate XPATH form in non java files [message #644369 is a reply to message #644347] |
Fri, 10 December 2010 17:28 |
Paul Elder Messages: 849 Registered: July 2009 |
Senior Member |
|
|
Johan:
The correct form is ${$myVar} - it looks ugly, but that's the way it is.
As for passing variables, JET2 has a very simple variable model - all variables are global. There are a couple of approaches to make this more rational:
1) the c:includes tag has a passVariables attribute. If you pass a comma separated list of variable names (without the $), the called template will see only those. At the end of the template, all variables are restored to their pre-invocation state.
2) markup your model with <c:set>. In general, I find that doing any calculation inside a template that is generating content just obscures things. Instead, I advocate doing calculations prior to the template invocation, and storing those values as 'temporary' attributes on your model elements. Here's an example:
Imagine you have a model something like this:
<root>
<class name="foo">
<property name="firstProp" type="int"/>
<property name="secondProp" type="boolean"/>
</class>
</root>
Now suppose that you want to generate getter and setter methods from each <property> element. You need to calculate a bunch of different values:
* the name of the getter (for secondProp, you'd probably want isSecondProp)
* the name of the setter.
* the name of the field represented the backing property
* the name of the parameter in the setter
You could do all of this in the template that generates the getter/setter code, but, to me, that just obscures the template. Instead, I typically traverse the model like this:
<c:iterate select="/root/class" var="class">
<c:iterate select="$class/property" var="property">
<%-- calculate values needed for getter/setter generation --%>
<c:set name="setter" select="$property" >set${uppercaseFirst(@name)}</c:set>
<c:set name="getter" select="$property" ><c:choose select="@type"><c:when test=" 'boolean' ">is</c:when><c:otherwise>get</c:otherwise></c:choose>${uppercaseFirst(@name)}</c:set>
... and so on ...
</c:iterate>
</c:iterate>
Then, in my template, I write nice simple code like this:
<%-- relying on current object being a class,
the loop with change the current object to a property
--%>
<c:iterate select="property">
/**
* Return the value of property ${@name}
*/
public ${@type} ${@getter}() {
return ${@fieldName};
}
/**
* Set the value of property ${@name}
* @param ${@argName}
*/
public void ${@setter}(${@type} ${@argName}) {
this.${@fieldName} = ${@argName};
}
</c:iterate>
Let me know if this works for you.
Paul
|
|
|
|
Powered by
FUDForum. Page generated in 0.05144 seconds