====== Templates ======

The templatery plugin offers the ability to write a template, and have it included in other pages with filled in fields.


===== Example =====
==== The anatomy of a template ====

Defining a template is very simple:

  <template example>

This starts a new template. The template can optionally have a name ('example' in this case).

  Hello, my name is: @@name@@

A piece of text with a field called 'name'. 
  
  <if organisation>
  I'm a member of @@organisation@@.
  </if>

A conditional piece of text. If the field 'organisation' is present, this will be displayed. If the field is not defined, the whole piece won't be used.

  </template>

Finishes the template.

==== The actual template ====
The template will end up looking like this:

<template example>
Hello, my name is: @@name@@

<if organisation>
I'm a member of @@organisation@@.
</if>
</template>

==== Using the template ====
**You use the template with**

  {{template>tech:templates#example|name=Brend|organisation=University of Twente}}

This tells the wiki to use the template [[tech:templates#example|tech:templates#example]].

If you do not have a full name, but something like %%{{template>test}}%% the wiki will look in the [[template:]] namespace for the template.

**Looks like this**

{{template>tech:templates#example|name=Brend|organisation=University of Twente}}

===== Using parameters =====

A template can have both named and numbered parameters.

==== Numbered parameters ====

Parameters can be numbered:

<template numbered>
0 = @@0@@, 1 = @@1@@
</template>

{{template>tech:templates#numbered
|Alpha
|beta
}}

You can freely mix numbered and named parameters.

==== Default values ====

Parameters can be given a default value:

<template defaults>
@@a|Default value for a@@  & @@b|Default value for b@@
</template>

{{template>tech:templates#defaults
|a=Alpha
}}
===== Conditionals =====

You can have conditional pieces of template:

<template conditionals>
<if a>
Text if a is set. a = @@a@@ 
</if>
<if !a>
Text if a is not set. a = @@a@@ 
</if>
</template>


**With A set**
{{template>tech:templates#conditionals|a=Alpha}}

**With A unset**
{{template>tech:templates#conditionals}}

==== Inline Conditionals ====

You can use inline conditionals for inclusion in places where normal conditionals don't work (such as in lists, tables, etc.) The system will try to determine when to use an inline conditional by itself. You can force an inline conditional with ''*''.


<template inline_conditionals1>
  * The trick: <if a>Text if a is set. a = @@a@@</if>

Forced inlines conditionals can be used <*if a>inside of paragraphs</if>!
</template>


**With A**
{{template>tech:templates#inline_conditionals1|a=Alpha}}
===== Templates in Templates =====

You can include a template in a template. This allows you to reuse templates.

If you dot this, you use %%{{template>name of template|inside=outside}}%%. The variables assignments are used to remap variables from outside the template to inside the template. Any variable not mentioned is available under it's old name.

<template recursion>
0 = @@0@@
<if 1>
{{template>tech:templates#recursion
|0=1
|1=2
|2=3
|3=4
}}
</if>
</template>

{{template>tech:templates#recursion
|Alpha
|Beta
|Gamma
|Delta
}}

===== Data and Templates =====

Combining templates and data is a good way to display a lot of data while still keeping it readable.

We can combine the data and the template with a 'view':

  <view ?name ?knows>
  template {
    manuals:stratatemplatery
  }
  
  ?p is a: example_person
  ?p entry title: ?name
  ?p Knows [ref]: ?knows
  
  group {
    ?p
    ?name
  }
  </view>


The %%<view>%% works exactly like [[wiki:strata]], with the addition of the ''template'' group.


==== Template with types ====

You can use types and aggregations in fields in the template. For example @@rating@max [dot]@@ will determine the maximum value of the 'rating' field in the query, and display this with the 'dot' type.

==== Typed Templates ====

You can use types in vanilla templates as well. This allows you to display nicer templates without extra effort.

<template typed>
An example: @@a@@
With explicit type: @@b [ref]@@
</template>

When filled in with ''%%a[wiki]=5 (:food) + 7 (:healthcare)%%'' and ''%%b=ic:Nehket Aeka%%'' this gives the following:

{{template>tech:templates#typed
|a[wiki]=5 (:food) + 7 (:healthcare)
|b=ic:Nehket Aeka
}}