Re: [Freemarker-devel] Hexadecimal formatting
Generates text that depends on changing data (like dynamic HTML).
Brought to you by:
revusky
|
From: Daniel D. <dd...@fr...> - 2004-04-29 21:00:16
|
Thursday, April 29, 2004, 4:15:52 PM, Attila Szegedi wrote:
> On Thu, 29 Apr 2004 14:36:47 +0200, Mark van de Veerdonk
> <mar...@se...> wrote:
>
>> Hi,
>>
>>
>> I've been using FreeMarker for a few months now and I'm very happy with
>> it.
>
> Glad to hear that.
>
>> But I needed a way to output integer values as hexadecimal strings
>> and there is no way to do that in the release build of Freemarker (or
>> did I miss anything?). So I decided to create a build-in "hex" myself. I
>> can image other people needing this too. What's the policy on adding new
>> build-ins to new Freemarker versions? Is there a will to keep the number
>> of build-ins steady or are new generic build-ins appreciated?
>>
>
> We appreciate any new built-ins that serve a useful purpose for large
> portion of our users, naturally.
>
>> BTW, I didn't see any documentation on contributing to Freemarker
>> development. Now I haven't got the time to do make any big contributions
>> but I'm willing to share the code of any new build-ins I create and
>> consider to be generic enough to be of value for Freemarker.
>
> Contributing patches is a very good way to start.
>
>>
>> Anyway, anybody wants to use my new build-in, I've included the code
>> below. It's not a complicated build-in. I'm sure you guys(?) know how to
>> get it running. Oh, just an example of what it does:
>> ${31?hex(4)} results in the string "001F".
>
> Well - it's mostly okay, except for two things :-)
> 1. If it's a BigInteger, then BigInteger.toString(int radix) is the API to
> use
> 2. Otherwise, num.longValue() is a better choice to support 64-bit
> integers, too.
>
> Also, I'm not sure if we want to include support for padding zeroes... I'd
> like to hear other opinions on the list regarding padding zeroes support;
If I want to display a number as binary/hexadecimal/octal number,
then it seldom happens that I do *not* want zero padding.
> I don't find it so essential.
As FM is about displaying things, I don't think that dealing with
padding in general is an overkill.
> You can emulate padding zeroes anytime with:
>
> <#assign hexnum = num?hex/>
> <#assign hexnum = "0000"[0...4-hexnum?length]+hexnum/>
>
> A bit verbose, but you can extract it into a <#macro> and pull in from a
> library using <#import> (or configuration auto-imports).
>
> Attila.
>
>>
>>
>> regards,
>> Mark
>>
--
Best regards,
Daniel Dekany
|