syntax reference.

In general all syntax starts with a { character and ends with the next available } character. This will either be an evaluation or a command, if it is not a command, templar will attempt to evaluate the expression.

Command Quick links

EVALUATIONS

Method evaluations

{<evaluate>.<method>.<anotherMethod>}

Evaluations are the simplest of the templar syntax, it will do the following:

  1. Lookup the <evaluate> in the templarContext
  2. Call the <method> method on the <evaluate> object
  3. Call the <anotherMethod> method on the object returned from the <evaluate>.<method> call
  4. Render the above output

Note that for each of the methods that is trying to be evaluated, the following prefixes will be looked up: get, is, has and finally just the method.

For example, if the method to be evaluated is {person.name}, the person object will be looked up in the context, retrieved (if it exists), and then the following method lookups will be attempted (in order):

  1. getName,
  2. isName,
  3. hasName, and finally
  4. name

Function evaluations

You may also evaluate functions (see the function reference for a list of functions available):

{fn:size[<array>]}
  1. Lookup the <array> in the templarContext
  2. Render the size or length of the object

You may also evaluate objects where the actual name of the object is not known until runtime (i.e. render time). In a rather contrived example, let's say that you wanted to loop through all of the objects in a session and print out the key:value pairs. In this case we use the dollar ($) notation:

{loop session.sessionObjects.keySet as key}
	{\t}KEY: {key}, VALUE: {session.sessionObjects.$key}{\n}
{endloop}

In effect this looks for a $key object in the templarContext with the name of key which is then called on the previous object.

COMMANDS

{-- ... - comments  

Comments start with {-- and continue up to the first instance found of }.This means that you CANNOT have a } character in the comment section - sorry.

{-- 
We all know that comments in code
are 
a good idea.

Comments can be multi-lined or they could be all on one line.  The only 
character that is not allowed is the '}' character - as this ends the comment.

Apologies
---------

We understand that not allowing the end brace character in a comment
means that you cannot comment out large block of templar templates,
hence the apology.

}

{if ... - condition  

{if <something>}
	<something> is true and this will be output
{else}
	<something> is false and this will be output
{endif}

<something> can either be a function, or an evaluation, e.g:

{if fn:true[object.method]}
	fn:true[object.method] is true and this will be output
{else}
	fn:true[object.method] is false and this will be output
{endif}
{if some.object.methodThatReturnsABoolean}
	some.object.methodThatReturnsABoolean is true and this will be output
{else}
	some.object.methodThatReturnsABoolean is false and this will be output
{endif}

You may of course chain functions, so long as the result is coercible to a Boolean

{if fn:equals[fn:size[array], 10]}
	this will be output if the size of the array is exactly 10
{else}
	this will be output if the size of the array is not exactly 10
{endif}

{set ...} - set a variable into the context  

The format of this command is as follows: {set __something__ as __somethingElse__}, where __something__is what you want to place in the context, and __somethingElse__ is the key which it will be bound to.

{set "world" as hello}

We will be using this example later on - you can see that it has been placed into the context by using the {dumpcontext} command - which renders the following (at runtime):

TemplarContext[{header-h1:templar}, {header-tagline:an easy to use, ultra light-weight, templating engine}, {header-h2:Welcome to templar}, {github-org:synapticloop}, {hello:world}, {sidebar-h3:templar}, {head-title:templar by synapticloop}, {github-repo:templar}, {sidebar-tagline:an easy to use, ultra light-weight, templating engine}]

You can also set the evaluation of an object or function into the context.

{set fn:length["some-string"] as someStringLength}

Once again dumping the context

TemplarContext[{header-h1:templar}, {header-tagline:an easy to use, ultra light-weight, templating engine}, {header-h2:Welcome to templar}, {someStringLength:11}, {github-org:synapticloop}, {hello:world}, {sidebar-h3:templar}, {head-title:templar by synapticloop}, {github-repo:templar}, {sidebar-tagline:an easy to use, ultra light-weight, templating engine}]

{loop ...} - loop over an iterable  

The format of this command is as follows: {loop __something__ as __somethingElse__}, where __something__is what you want to loop over, placing __somethingElse__ into the context for each iteration. This williterate over it rendering the inner content between start and {endloop} token.

{loop object.array as something}
  // render something here
{endloop}

An example taken from h2zero:

{loop table.finders as finder}
	{if fn:null[finder.selectClause]}
		{\t}private static final String SQL_{finder.staticName} = SQL_SELECT_START
	{else}
		{\t}private static final String SQL_{finder.staticName} = "{finder.selectClause}"
	{endif}

	{if fn:notNull[finder.whereClause]} + " {finder.whereClause}"{endif}
	{if fn:notNull[finder.orderBy]} + " order by {finder.orderBy}"{endif};{\n}
{endloop}
{\n}

{import ...} - import a file into the current one  

Import a file into the current file and parse the file for any templar declarations.The file to import can also be from the classpath (if you are deploying this as a complete jar - {import classpath:/some/classpath/file/here.templar}

{import src/imports/commands/import.templar}

This file was imported from syntax-reference.html with the above templar declaration.

{static ...} - import a file into the current one  

Import a file into the current file and DO NOT parse the file for any templar declarations. The file to import can also be from the classpath (if you are deploying this as a complete jar - {static classpath:/some/classpath/file/here.templar}

{import src/imports/commands/static.templar}

This file was imported from syntax-reference.html with the above line

{statichtml ...} - import a file into the current one and escape HTML characters 

Import a file into the current file and DO NOT parse the file for any templar declarations. The file to import can also be from the classpath (if you are deploying this as a complete jar - {statichtml classpath:/some/classpath/file/here.templar}

Escape any characters for HTML, i.e.:

  • & is escaped to &amp;
  • < is escaped to &lt;
  • > is escaped to &gt;
{import src/imports/commands/static.templar}

This file was imported from syntax-reference.html with the above line

{pre ... - pre formatted text  

As the name would suggest pre-formatted text - this is the only command that does not end with a }character, instead must end with pre}.

{pre 


anything that goes in here, including any templar syntax is output exactly as shown without any templar parsing.  This
 is especially useful for CSS and JavaScript which has a lot of ' and ' characters.

The content will be output until an end token of 'pre' immediately followed be a '}' character

pre}

The above would render as follows (note the use of the {\n}):

{\n}
{\n}
{\n}
anything that goes in here, including any templar syntax is output exactly as shown without any templar parsing.  This{\n} 
is especially useful for CSS and JavaScript which has a lot of '{' and '}' characters.{\n}
{\n}
The content will be output until an end token of 'pre' immediately followed be a '}' character{\n}
{\n}

{tab} or {\t} - output a tab  

This will output a tab whitespace character

{nl} or {\n} - output a new line character  

This will output a newline whitespace character

{dumpcontext} - dumping the context  

This is useful for debugging as this will render all of the objects in the context, and will look like this:

TemplarContext[{header-h1:templar}, {header-tagline:an easy to use, ultra light-weight, templating engine}, {header-h2:Welcome to templar}, {someStringLength:11}, {github-org:synapticloop}, {hello:world}, {sidebar-h3:templar}, {head-title:templar by synapticloop}, {github-repo:templar}, {sidebar-tagline:an easy to use, ultra light-weight, templating engine}]

We cheated a little bit and added something to the context with the following:

{set "world" as hello}

{dumpfunctions} - dumping the functions  

This is useful for debugging as this will render all of the registered functions and it will look like this:

FunctionLessThanEqual fn:<=[ <#numArgs: 2> ] aliased as (lte)
FunctionNotEqual fn:<>[ <#numArgs: 2> ] aliased as (notEqual, ne, not=)
FunctionInstanceOf fn:instanceOf[ <#numArgs: 2> ]
FunctionAnd fn:and[ <#numArgs: 2> ]
FunctionIndexOf fn:indexOf[ <#numArgs: 2> ]
FunctionPower fn:^[ <#numArgs: 2> ]
FunctionOr fn:or[ <#numArgs: 2> ]
FunctionIsNotNull fn:notNull[ <#numArgs: 1> ] aliased as (isNotNull, !null, isnotnull, notnull, !Null)
FunctionFormatDate fn:fmtDate[ <#numArgs: 1 or 2> ]
FunctionModulus fn:%[ <#numArgs: 2> ]
FunctionAnd fn:&[ <#numArgs: 2> ]
FunctionLength fn:length[ <#numArgs: 1> ] aliased as (size)
FunctionFalse fn:false[ <#numArgs: 1> ]
FunctionMultiply fn:*[ <#numArgs: 2> ]
FunctionAdd fn:+[ <#numArgs: 2> ]
FunctionSubtract fn:-[ <#numArgs: 2> ]
FunctionOdd fn:odd[ <#numArgs: 1> ]
FunctionDivide fn:/[ <#numArgs: 2> ]
FunctionToJson fn:toJson[ <#numArgs: 1> ]
FunctionIsNull fn:null[ <#numArgs: 1> ] aliased as (isnull, isNull)
FunctionEven fn:even[ <#numArgs: 1> ]
FunctionTrue fn:true[ <#numArgs: 1> ]
FunctionLessThan fn:<[ <#numArgs: 2> ] aliased as (lt)
FunctionOr fn:|[ <#numArgs: 2> ]
FunctionEqual fn:=[ <#numArgs: 2> ] aliased as (eq, equal)
FunctionGreaterThan fn:>[ <#numArgs: 2> ] aliased as (gt)
FunctionGreaterThanEqual fn:>=[ <#numArgs: 2> ] aliased as (gt=)

{requires} - require a variable to be available in the context 

This ensures that a variable is available in the context. If the variable is not available, then a RenderException will be thrown