Discussion:
Unicode in Velocity
Gary M. Catlin
2007-07-30 20:40:09 UTC
Permalink
How do I assign and output a unicode character in velocity? I need to
output the value of "f00b" prior to a block of calculated output.

TIA
mailmur
2007-08-01 06:36:28 UTC
Permalink
http://koti.mbnet.fi/akini/java/unicodereader/
http://koti.mbnet.fi/akini/java/java_utf8_xml/

Velocity 1.5+ has UnicodeFileResourceLoader loader
implementation. You can save .vm files as UTF-8 (with
or without BOM) text files. It saves you all the
troubles.

Only issue then is to make sure you send HttpResponse
headers accordingly.
Content-Type: text/html; charset=UTF-8
Post by Gary M. Catlin
How do I assign and output a unicode character in
velocity? I need to
output the value of "f00b" prior to a block of
calculated output.
TIA
____________________________________________________________________________________
Pinpoint customers who are looking for what you sell.
http://searchmarketing.yahoo.com/
Gary M. Catlin
2007-08-08 12:19:32 UTC
Permalink
Thank you for the reply, but not sure if that will help. My solution
needs to be at a higher level. I need to "set" a variable with a unicode
value, and to output this value before and after a block of output:

#set ($v = "\uf00b")

$v
$myBlockOutput
$v

The problem has been assigning the unicode value. The standard 'escape'
does not seem to be working. We are using Velocity 1.5.

Thank you.
Post by mailmur
http://koti.mbnet.fi/akini/java/unicodereader/
http://koti.mbnet.fi/akini/java/java_utf8_xml/
Velocity 1.5+ has UnicodeFileResourceLoader loader
implementation. You can save .vm files as UTF-8 (with
or without BOM) text files. It saves you all the
troubles.
Only issue then is to make sure you send HttpResponse
headers accordingly.
Content-Type: text/html; charset=UTF-8
Post by Gary M. Catlin
How do I assign and output a unicode character in
velocity? I need to
output the value of "f00b" prior to a block of
calculated output.
TIA
____________________________________________________________________________________
Pinpoint customers who are looking for what you sell.
http://searchmarketing.yahoo.com/
---------------------------------------------------------------------
Nathan Bubna
2007-08-08 16:08:20 UTC
Permalink
Post by Gary M. Catlin
Thank you for the reply, but not sure if that will help. My solution
needs to be at a higher level. I need to "set" a variable with a unicode
#set ($v = "\uf00b")
Unfortunately, this was broken. It has been fixed in Velocity 1.6 though.
https://issues.apache.org/jira/browse/VELOCITY-520

If you aren't interested in building 1.6 from source yourself (it has
no releases yet), then you could create a "tool" that creates
arbitrary unicode characters for you. Since i was curious of how to
do this myself, i wrote one for you:

public class UnicodeTool
{
public static String get(String code)
{
if (code != null && code.startsWith("\\u"))
{
code = code.substring(2, code.length());
}
return get(Integer.valueOf(code,16));
}

public static String get(int codePoint)
{
return String.valueOf(Character.toChars(codePoint));
}
}

Just put an instance in the context as "unicode" and use it in the
template like this:

#set( $v = $unicode.get('f00b') )
Post by Gary M. Catlin
$v
$myBlockOutput
$v
The problem has been assigning the unicode value. The standard 'escape'
does not seem to be working. We are using Velocity 1.5.
Thank you.
Post by mailmur
http://koti.mbnet.fi/akini/java/unicodereader/
http://koti.mbnet.fi/akini/java/java_utf8_xml/
Velocity 1.5+ has UnicodeFileResourceLoader loader
implementation. You can save .vm files as UTF-8 (with
or without BOM) text files. It saves you all the
troubles.
Only issue then is to make sure you send HttpResponse
headers accordingly.
Content-Type: text/html; charset=UTF-8
Post by Gary M. Catlin
How do I assign and output a unicode character in
velocity? I need to
output the value of "f00b" prior to a block of
calculated output.
TIA
____________________________________________________________________________________
Pinpoint customers who are looking for what you sell.
http://searchmarketing.yahoo.com/
---------------------------------------------------------------------
---------------------------------------------------------------------
Gary M. Catlin
2007-08-08 19:40:15 UTC
Permalink
Thank you, Nathan. I'll let you know how it works.

gmc
Post by Nathan Bubna
Post by Gary M. Catlin
Thank you for the reply, but not sure if that will help. My solution
needs to be at a higher level. I need to "set" a variable with a unicode
#set ($v = "\uf00b")
Unfortunately, this was broken. It has been fixed in Velocity 1.6 though.
https://issues.apache.org/jira/browse/VELOCITY-520
If you aren't interested in building 1.6 from source yourself (it has
no releases yet), then you could create a "tool" that creates
arbitrary unicode characters for you. Since i was curious of how to
public class UnicodeTool
{
public static String get(String code)
{
if (code != null && code.startsWith("\\u"))
{
code = code.substring(2, code.length());
}
return get(Integer.valueOf(code,16));
}
public static String get(int codePoint)
{
return String.valueOf(Character.toChars(codePoint));
}
}
Just put an instance in the context as "unicode" and use it in the
#set( $v = $unicode.get('f00b') )
Post by Gary M. Catlin
$v
$myBlockOutput
$v
The problem has been assigning the unicode value. The standard 'escape'
does not seem to be working. We are using Velocity 1.5.
Thank you.
Post by mailmur
http://koti.mbnet.fi/akini/java/unicodereader/
http://koti.mbnet.fi/akini/java/java_utf8_xml/
Velocity 1.5+ has UnicodeFileResourceLoader loader
implementation. You can save .vm files as UTF-8 (with
or without BOM) text files. It saves you all the
troubles.
Only issue then is to make sure you send HttpResponse
headers accordingly.
Content-Type: text/html; charset=UTF-8
Post by Gary M. Catlin
How do I assign and output a unicode character in
velocity? I need to
output the value of "f00b" prior to a block of
calculated output.
TIA
____________________________________________________________________________________
Pinpoint customers who are looking for what you sell.
http://searchmarketing.yahoo.com/
---------------------------------------------------------------------
---------------------------------------------------------------------
---------------------------------------------------------------------
Continue reading on narkive:
Loading...