Jostraca Example: Replacements
example index
www.jostraca.org
info@jostraca.org

Description:

This template gives some examples of how the "replace" and "replace-regexp" directives can be used to make your templates more readable and user-friendly.

Generate Command:

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

Generated Files:

Replacements.txt
.jostraca/ReplacementsWriter.java
.jostraca/ReplacementsWriter.class

Experiments:

Try adding some of your own replacements.

Template Contents:

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

<% @init
_setFullFileName( "Replacements.txt" );

String person = "Bob";
%>
<% @replace Person $<o>=person$<c> %>

Hello Person!
Bye Person!

Watch out when the replacement is contained 
in another word, like 'Personal'.


<%-- 
How to make a repeated area of text more readable.
Note: longer replacements take precedence, 
so that //-foreach-Name-// does not become //-foreach-Bob-// 
--%>

<% String[] names = new String[] { "Bob", "Fred", "John" }; %>
<% @replace //-foreach-Name-// '$<o> for( int nameI = 0; nameI < names.length; nameI++ ) { $<c>' %>
<% @replace Name               $<o>=names[nameI]$<c> %>
<% @replace //-end-//          '$<o> } $<c>' %>

//-foreach-Name-//
Hi Name
//-end-//


<%-- How to define a custom template language --%>
<% @replace-regexp #if\s(.*?)\n $<o>if($1){$<c> %>
<% @replace        #else        $<o>}else{$<c> %>
<% @replace        #end         $<o>}$<c> %>

#if "Bob".equals( person ) 
  Hi Bob!
#else
  this will not appear
#end

 

Generated File Contents:

Note: only one generated file is shown




Hello Bob!
Bye Bob!

Watch out when the replacement is contained 
in another word, like 'Bobal'.










Hi Bob

Hi Fred

Hi John








  Hi Bob!


 

   example index
   Copyright © 2000-2002 Richard Rodger License.