Jostraca Example: Replace Regular Expressions
example index
www.jostraca.org
info@jostraca.org

Description:

This template shows how to use the @replace-regexp directive to make your template more readable. The basic idea is to replace insertions of the form <%=foo%> with the form: ${foo}, for any variable foo. Notice how the regular expression back reference $1 is used to insert the value of the submatch and how the shortcut properties $<o> and $<c> are used to escape the open and close script markers <% and %> in the @replace-regexp directive.

Generate Command:

> jostraca ReplaceRegExp.jtm
(you can also use generate.bat or generate.sh)

Generated Files:

ReplaceRegExp.txt
.jostraca/ReplaceRegExpWriter.java
.jostraca/ReplaceRegExpWriter.class

Experiments:

Try adding a regular expression replacement for "if" statements.

Template Contents:

<% @conf 
main.JostracaVersion = 0.4
main.TemplateScript  = java
%>

<% init!
_setFullFileName( "ReplaceRegExp.txt" );
String foo = "oof";
String bar = "rab";
%>
<% @replace-regexp \${(.*?)} $<o>=$1$<c> %>

the value of foo is: ${foo}
the value of bar is: ${bar}
 

Generated File Contents:

Note: only one generated file is shown




the value of foo is: oof
the value of bar is: rab
 

   example index
   Copyright © 2000-2002 Richard Rodger License.