You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by sebb <se...@gmail.com> on 2009/06/13 12:13:46 UTC

Best way to handle optional attributes in 1.6.2

I'm generating HTML using a Velocity style sheet with version 1.6.2

The source contains lots of elements which are used to generate table
cell entries:

<property name="Name" required="">Description</property>

The "required" attribute is supposed to be "Yes" or "No", with a
default of "No".

The VSL currently has:

<td>
#if($items.getAttributeValue("required") != "")
$items.getAttributeValue("required")
#else
No
#end
</td>

This works OK provided that the "required" attribute is present,
however I would like to treat a missing attribute the same way as the
empty string.

The above code used to work in Velocity 1.5, but now I get

<td>$items.getAttributeValue("required")</td>

instead of

<td>No</td>

The following works, but seems rather messy:

<td>
#if($items.getAttributeValue("required") &&
$items.getAttributeValue("required") != "")
$items.getAttributeValue("required")
#else
No
#end
</td>

Is there a better way to handle optional attributes?

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
For additional commands, e-mail: user-help@velocity.apache.org


Re: Best way to handle optional attributes in 1.6.2

Posted by sebb <se...@gmail.com>.
I see, thanks.

On 15/06/2009, Nathan Bubna <nb...@gmail.com> wrote:
> Your original VTL amounted to #if ( null != "" ) when there was no
>  attribute value.   The fact that this evaluated as false was a bug,
>  since null doesn't equal the empty string.  Several little, obscure
>  comparison bugs like this were fixed prior to 1.6.2.  I don't recall,
>  though, under which commit/JIRA Issue this fix was registered in the
>  changelog.
>
>
>  On Mon, Jun 15, 2009 at 3:19 PM, sebb<se...@gmail.com> wrote:
>  > Thanks for the suggestions which work fine.
>  >
>  > There seems to have been a change in behaviour since version 1.5.1,
>  > but I could not find it in the release notes. Can anyone confirm this?
>  >
>  > On 14/06/2009, Nathan Bubna <nb...@gmail.com> wrote:
>  >> That should be:
>  >>
>  >>  #if( "$!items.getAttributeValue('required')" != "" )
>  >>
>  >>  Or just $!items.getAttributeValue('required') if you don't mind
>  >>  showing empty strings.
>  >>
>  >>  Or if you really want to clean up the look:
>  >>
>  >>  public class AltTool {
>  >>   public Object empty(Object val, Object alt) {
>  >>     return (val == null || val.toString().length() == 0) ? alt : val;
>  >>   }
>  >>  }
>  >>  context.put("alt", new AltTool());
>  >>
>  >>  $alt.empty($item.getAttributeValue('required'), 'No')
>  >>
>  >>
>  >>  On Sat, Jun 13, 2009 at 5:39 AM, Jude
>  >>  Robinson<do...@gmail.com> wrote:
>  >>  >> Is there a better way to handle optional attributes?
>  >>  >
>  >>  > "!$items.getAttributeValue('required')" != ""
>  >>  >
>  >>  > ---------------------------------------------------------------------
>  >>  > To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
>  >>  > For additional commands, e-mail: user-help@velocity.apache.org
>  >>  >
>  >>  >
>  >>
>  >>  ---------------------------------------------------------------------
>  >>  To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
>  >>  For additional commands, e-mail: user-help@velocity.apache.org
>  >>
>  >>
>  >
>  > ---------------------------------------------------------------------
>  > To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
>  > For additional commands, e-mail: user-help@velocity.apache.org
>  >
>  >
>
>  ---------------------------------------------------------------------
>  To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
>  For additional commands, e-mail: user-help@velocity.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
For additional commands, e-mail: user-help@velocity.apache.org


Re: Best way to handle optional attributes in 1.6.2

Posted by Nathan Bubna <nb...@gmail.com>.
Your original VTL amounted to #if ( null != "" ) when there was no
attribute value.   The fact that this evaluated as false was a bug,
since null doesn't equal the empty string.  Several little, obscure
comparison bugs like this were fixed prior to 1.6.2.  I don't recall,
though, under which commit/JIRA Issue this fix was registered in the
changelog.

On Mon, Jun 15, 2009 at 3:19 PM, sebb<se...@gmail.com> wrote:
> Thanks for the suggestions which work fine.
>
> There seems to have been a change in behaviour since version 1.5.1,
> but I could not find it in the release notes. Can anyone confirm this?
>
> On 14/06/2009, Nathan Bubna <nb...@gmail.com> wrote:
>> That should be:
>>
>>  #if( "$!items.getAttributeValue('required')" != "" )
>>
>>  Or just $!items.getAttributeValue('required') if you don't mind
>>  showing empty strings.
>>
>>  Or if you really want to clean up the look:
>>
>>  public class AltTool {
>>   public Object empty(Object val, Object alt) {
>>     return (val == null || val.toString().length() == 0) ? alt : val;
>>   }
>>  }
>>  context.put("alt", new AltTool());
>>
>>  $alt.empty($item.getAttributeValue('required'), 'No')
>>
>>
>>  On Sat, Jun 13, 2009 at 5:39 AM, Jude
>>  Robinson<do...@gmail.com> wrote:
>>  >> Is there a better way to handle optional attributes?
>>  >
>>  > "!$items.getAttributeValue('required')" != ""
>>  >
>>  > ---------------------------------------------------------------------
>>  > To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
>>  > For additional commands, e-mail: user-help@velocity.apache.org
>>  >
>>  >
>>
>>  ---------------------------------------------------------------------
>>  To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
>>  For additional commands, e-mail: user-help@velocity.apache.org
>>
>>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
> For additional commands, e-mail: user-help@velocity.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
For additional commands, e-mail: user-help@velocity.apache.org


Re: Best way to handle optional attributes in 1.6.2

Posted by sebb <se...@gmail.com>.
Thanks for the suggestions which work fine.

There seems to have been a change in behaviour since version 1.5.1,
but I could not find it in the release notes. Can anyone confirm this?

On 14/06/2009, Nathan Bubna <nb...@gmail.com> wrote:
> That should be:
>
>  #if( "$!items.getAttributeValue('required')" != "" )
>
>  Or just $!items.getAttributeValue('required') if you don't mind
>  showing empty strings.
>
>  Or if you really want to clean up the look:
>
>  public class AltTool {
>   public Object empty(Object val, Object alt) {
>     return (val == null || val.toString().length() == 0) ? alt : val;
>   }
>  }
>  context.put("alt", new AltTool());
>
>  $alt.empty($item.getAttributeValue('required'), 'No')
>
>
>  On Sat, Jun 13, 2009 at 5:39 AM, Jude
>  Robinson<do...@gmail.com> wrote:
>  >> Is there a better way to handle optional attributes?
>  >
>  > "!$items.getAttributeValue('required')" != ""
>  >
>  > ---------------------------------------------------------------------
>  > To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
>  > For additional commands, e-mail: user-help@velocity.apache.org
>  >
>  >
>
>  ---------------------------------------------------------------------
>  To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
>  For additional commands, e-mail: user-help@velocity.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
For additional commands, e-mail: user-help@velocity.apache.org


Re: Best way to handle optional attributes in 1.6.2

Posted by Nathan Bubna <nb...@gmail.com>.
That should be:

#if( "$!items.getAttributeValue('required')" != "" )

Or just $!items.getAttributeValue('required') if you don't mind
showing empty strings.

Or if you really want to clean up the look:

public class AltTool {
  public Object empty(Object val, Object alt) {
    return (val == null || val.toString().length() == 0) ? alt : val;
  }
}
context.put("alt", new AltTool());

$alt.empty($item.getAttributeValue('required'), 'No')

On Sat, Jun 13, 2009 at 5:39 AM, Jude
Robinson<do...@gmail.com> wrote:
>> Is there a better way to handle optional attributes?
>
> "!$items.getAttributeValue('required')" != ""
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
> For additional commands, e-mail: user-help@velocity.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
For additional commands, e-mail: user-help@velocity.apache.org


Re: Best way to handle optional attributes in 1.6.2

Posted by Jude Robinson <do...@gmail.com>.
> Is there a better way to handle optional attributes?

"!$items.getAttributeValue('required')" != ""

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
For additional commands, e-mail: user-help@velocity.apache.org