On 06/01/2011 03:40 PM, Moe221 wrote:

Hi there,
i'm evaluating Velocity to find out whether it meets my requirements to
generate text by using custom templates. I looked at the user guide and
wrote a little hello world application but there are requirements for which
I don't know if I have to write my own methods or if there are velocity
tools that would do the job for me:

Is there a way to automatically add line breaks when you merge the template
with the context (without cutting words after e.g. 80 characters)?

Is there a way to use text shifting (indentation) in combination with line
breaks without cutting words after e.g. 80 characters)?
Long text: $magic.doMagic( $text )
-->
Long text: Some very
         Very long text,
         more text …

Is there a way to automatically add line breaks (without cutting words after
e.g. 80 characters) and add a certain String (e.g. "100. ") at the beginning
of each line:
Funny Velocity user request: $magic.doMagic2( $text )
-->
Funny Velocity user request: this
100. is just a test, a test, a test, a
100. test, a test …

Is there a way to cut the string of a variable after 9 characters and add a
"*" to the string, if the string has more than 10 characters?
#set( $test = "textwithmorethan10char" )
Test: $magic.doMagic3( $test)
-->
Test: textwithm*
Thanx in advance!


Natively, no, this is not the job of the velocity core.

But it would be fairly simple to write a velocity tool that does that for you, similar to
http://velocity.apache.org/tools/devel/javadoc/org/apache/velocity/tools/generic/DisplayTool.html

DisplayTool does have a method for your last request, see
http://velocity.apache.org/tools/devel/javadoc/org/apache/velocity/tools/generic/DisplayTool.html#truncate%28java.lang.Object,%20int,%20java.lang.String,%20boolean%29

You will have to enable the tools, then you would have to write:

$displaytool.truncate($test, 10, '*', false)


Alternatively, you can do that as well directly in the template, but it would be a bit harder since you don't have all the power of Java. For example, line numbering has been done in:
https://github.com/xwiki/xwiki-platform/raw/master/xwiki-platform-core/xwiki-platform-web/src/main/webapp/templates/code.vm

--
Sergiu Dumitriu
http://purl.org/net/sergiu/

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to