<!DOCTYPE html>
<html>
<head>
<title>{{title}}</title>
</head>
<body>
<h1>{{title}}</h1>
</body>
</html>
A comment is any text enclosed in {# .. #}
tags. These will be stripped out of your template before being rendered.
An expression is any value enclosed in {{ .. }}
tags. Expressions can be a variable name, a function call, or a literal value.
Notes
- The expression parser is very simple and only expects one expression per block. If you need to evaluate a complex expression,
use
add_function()
to register a custom function that will perform the expression and return the result. - The expression parser is recursive so it should understand nested expressions, like
not( equal( length(x), 0 ) )
, although it's still adviseable to limit the depth of these expressions and utlize functions for complex logic.
Map properties can be retrieved using dot notation, e.g. item.property
.
You can register functions that can be called by the template parser to perform some complex logic, data lookup, etc. and then return a value to be rendered into the output. The following functions are provided by default:
- atom( object x ) -- Returns
TRUE
ifobject x
is anatom
. - integer( object x ) -- Returns
TRUE
ifobject x
is aninteger
. - sequence( object x ) -- Returns
TRUE
ifobject x
is asequence
. - object( object x ) -- Returns
TRUE
ifobject x
is anobject
. - isset( object x ) -- Returns
TRUE
ifobject x
is set in the current response.
- equal( object a, object b ) -- Returns
TRUE
ifobject a
equalsobject b
. - not_equal( object a, object b ) -- Returns
TRUE
ifobject a
does not equalobject b
. - length( object x ) -- Returns the length of
object x
. - not( object x ) -- Returns the opposite boolean value of
object x
.
- and( object a, object b ) -- Returns the boolean result of
object a
andobject b
. - or( object a, object b ) -- Returns the boolean result of
object a
orobject b
. - xor( object a, object b ) -- Returns the boolean result of
object a
xorobject b
.
- format( object x, object p ) -- Returns a formatted string via
text:format()
. - pretty( object x, object p = PRETTY_DEFAULT ) -- Returns a formatted string via
pretty_print()
. - sprintf( object x, object d ) -- Returns a formatted string via
eu:sprintf()
.
- add( object a, object b ) -- Returns the value of
object a
plusobject b
. - subtract( object a, object b ) -- Returns the value of
object a
minusobject b
. - multiply( object a, object b ) -- Returns the value of
object a
timesobject b
. - divide( object a, object b ) -- Returns the value of
object a
divided byobject b
.
- format_date( object d, object f="%Y-%m-%d" ) -- Returns a formatted date via
datetime:format()
. - format_time( object d, object f="%H:%M:%S" ) -- Returns a formatted time via
datetime:format()
. - dt( object d, object f="%Y-%m-%d %H:%M:%S" ) -- Returns a formatted datetime via
datetime:format()
.
- to_integer( object x, object d=0 ) -- Converts an object to an integer via
convert:to_integer()
. - to_number( object x, object p=0 ) -- Converts an object to a number via
convert:to_number()
. - to_string( object x, object q=0, object e='"' ) -- Converts an object to a string via
convert:to_string()
.
A statement is one of the following enclosed in {% .. %}
tags.
if ... elsif ... else ... end if
{% if expression then %}
<p>content if true</p>
{% elsif expression then %}
<p>another possibly true expression</p>
{% else %}
<p>content if false</p>
{% end if %}
for variable = expression to expression do
<ul>
{% for i = 1 to 10 do %}
<li>Item {{i}}</li>
{% end for %}
</ul>
for variable = expression to expression by expression do
<ul>
{% for i = 1 to 10 by 2 do %}
<li>Item {{i}}</li>
{% end for %}
</ul>
for variable in expression do
<table>
<tr>
<th>Name</th>
<th>Size</th>
<th>Type</th>
<th>Last modified</th>
</tr>
{# we assume list is a sequence of maps #}
{% for item in list do %}
<tr>
<td>{{item.name}}</td>
<td>{{item.size}}</td>
<td>{{item.type}}</td>
<td>{{item.last_modified}}</td>
</tr>
{% end for %}
</table>
collapse
A collapse statement will collapse all lengths of whitespace to a single space and remove any leading and trailing whitespace. This is helpful when you want to add line breaks and indents to your template statements for clarity, but have the output be returned without any additional of the formatting.
If you have this code in your template:
<title>{% collapse %}
{% if isset(page_title) %}
{{ page_title }} -
{% end if %}
{{ site_title }}
{% end collapse %}</title>
This is the output if page_title
is set:
<title>Page Title - Site Title</title>
And this is the output if page_title
is not set:
<title>Site Title</title>
extends filename
The extends statement allows for parent/child relationships between templates. You can define a single parent "layout" template, and then extend that template in all your other templates.
{% extends "layout.html" %}
block name
Blocks are named sections used between templates. Blocks in your layout template shoudld be empty, as they will be replaced by matching blocks from the page template.
If this is your layout template:
<body>
<section name="content">
{% block content %}{% end block %}
</section>
</body>
And this is your page template:
{% extends "layout.html" %}
{% block content %}
<p>This is where my content goes!</p>
{% end block %}
Then your rendered template will look something like this:
<body>
<section name="content">
<p>This is where my content goes!</p>
</section>
</body>
The follow statement keywords can be used for compatibility with Jinja:
Euphoria MVC | Jinja |
---|---|
do | not used |
end for | endfor |
end if | endif |
elsif | elif |
then | not used |
include mvc/template.e
public procedure add_function( sequence func_name, sequence params = {}, integer func_id = routine_id(func_name) )
Add a function that can be called from an expression.
Parameters
func_name
- name of the function to addparams
- list of parameter namesfunc_id
- the routine_id() of the function
include mvc/template.e
public function render_template( sequence filename, object response = {}, integer free_response = TRUE )
Render a template with the given response object.
Parameters
template
- your template file nameresponse
- your map or sequence of template valuesfree_reponse
- whether or not yourresponse
object should be freed by callingdelete()
Returns
The completely rendered output from the template file.
include mvc/template.e
public procedure set_template_path( sequence path )
Sets the path to where template files live. By default, this is just templates
in the current directory.
Parameters
path
- path to where template files live