TEMPLATES

Show your content on the site with Twig templates

In the admin you create tables (for example News from the Quickstart) and fill them with content. Templates decide how that content looks on your pages. EvolvitCMS uses Twig 3, a simple template language: regular HTML plus a few tags in curly braces.

1. Every template is a page

Templates are files in the twigtemplates/ folder of your site. The file name is the address of the page:

FileAddress
twigtemplates/index.twigyour-site.com/
twigtemplates/about.twigyour-site.com/about
twigtemplates/news.twigyour-site.com/news, your-site.com/news/anything/else

Create a file twigtemplates/hello.twig (with your hosting file manager or any FTP client):

<!doctype html>
<html>
<body>
  <h1>Hello!</h1>
  <p>Today is {{ "now"|date("d.m.Y") }}.</p>
</body>
</html>

Open your-site.com/hello – that's your first page. {{ … }} prints a value, {% … %} runs a command (a loop, a condition…). The twigtemplates/ folder itself is protected: visitors only see the rendered pages, never your template files.

2. One layout for all pages

Put everything your pages share (head, menu, footer) into one template, and mark the parts that change with {% block %}. twigtemplates/base.twig:

<!doctype html>
<html>
<head>
  <base href="{{ _BASE }}">
  <title>My site {% block title %}{% endblock %}</title>
  <link rel="stylesheet" href="css/style.css">
</head>
<body>
  <nav><a href="index">Home</a> <a href="news">News</a></nav>
  {% block content %}{% endblock %}
  <footer>© {{ "now"|date("Y") }} My site</footer>
</body>
</html>

Every page then only fills in its blocks:

{% extends "base.twig" %}

{% block title %}– About us{% endblock %}

{% block content %}
  <h1>About us</h1>
  <p>We are a small team…</p>
{% endblock %}

Tip: <base href="{{ _BASE }}"> makes relative links like news or css/style.css work on every page, even when the site lives in a subfolder (your-site.com/blog/).

3. List content from the admin

fetch("News") returns all rows of the News table you created in the admin. Loop over them with for. twigtemplates/news.twig:

{% extends "base.twig" %}

{% block content %}
  <h1>News</h1>

  {% for item in fetch("News") %}
    <article>
      <h2><a href="news/{{ item.Slug }}">{{ item.Title }}</a></h2>
      <small>{{ item.Date|date("d.m.Y") }}</small>
      <p>{{ item.Text }}</p>
    </article>
  {% else %}
    <p>No news yet.</p>
  {% endfor %}
{% endblock %}

Field names are exactly the names you gave the fields in the admin (they are case sensitive: item.Title, not item.title). The {% else %} part is shown when the table is empty.

4. A page for every item

Add a field of type SLUG to your table (for example Slug, generated from Title). It turns "My first news!" into my-first-news, so every item gets a nice address like your-site.com/news/my-first-news.

The parts of the address are available in _GET: for news/my-first-news, _GET[0] is news and _GET[1] is my-first-news. fetch("News", {id: …}) finds a row by its id or slug. One template can show both the list and the detail page:

{% extends "base.twig" %}

{% set slug = _GET[1] ?? null %}

{% block content %}
  {% if slug %}
    {% set item = fetch("News", {id: slug})|first %}
    {% if item %}
      <h1>{{ item.Title }}</h1>
      <div>{{ item.Body|raw }}</div>
      <a href="news">← All news</a>
    {% else %}
      <p>This news item doesn't exist.</p>
    {% endif %}
  {% else %}
    {% for item in fetch("News") %}
      <p><a href="news/{{ item.Slug }}">{{ item.Title }}</a></p>
    {% endfor %}
  {% endif %}
{% endblock %}

Tip: for a proper "not found" page add {% do header('HTTP/1.1 404 Not Found') %} in the else branch, so search engines don't index empty pages.

5. Printing each field type

Everything you print with {{ }} is escaped automatically: if someone types <script> into a field, it is shown as text and never runs. The only exception is the HTML editor field, which is meant to contain HTML.

Field typeHow to print it
TEXT{{ item.Title }}
LONGTEXT{{ item.Text|nl2br }} – keeps the line breaks
HTMLEDIT{{ item.Body|raw }} – the formatted text from the editor (only use |raw here)
DATE{{ item.Date|date("d.m.Y") }}
IMAGE<img src="img/{{ item.Image }}" alt="{{ item.Title }}">
IMAGE, resized<img src="imgcache/400/{{ item.Image }}"> – 400 px wide, cached (jpg, png, gif, webp)
CHECKBOX{% if item.Published == 'true' %}…{% endif %} – stored as the text 'true' / 'false'
DROPDOWN{{ item.Category_label }} – the chosen value's text; item.Category is its id
NUMERIC / INTEGER{{ item.Price|number_format(2, ',', '.') }}
COLOR<div style="background: {{ item.Color }}">

Image galleries: give an IMAGE field a name ending with an underscore (for example Gallery_) and the admin lets you upload many images with labels. In the template it is a list:

{% for image in item.Gallery_ ?? [] %}
  <figure>
    <a href="img/{{ image.slug }}"><img src="imgcache/300/{{ image.slug }}" alt="{{ image.label }}"></a>
    <figcaption>{{ image.label }}</figcaption>
  </figure>
{% endfor %}

6. Filtering, sorting and limiting

Pass field values to fetch to get only matching rows:

{# only news from the category with id 3 #}
{% for item in fetch("News", {Category: 3}) %} … {% endfor %}

{# only published news #}
{% for item in fetch("News", {Published: 'true'}) %} … {% endfor %}

Or use Twig filters on the result:

{# the 3 newest items #}
{% for item in fetch("News")|sort((a, b) => b.Date <=> a.Date)|slice(0, 3) %} … {% endfor %}

{# a condition per item #}
{% for item in fetch("News")|filter(item => item.Image) %} … {% endfor %}

{# how many? #}
<p>{{ fetch("News")|length }} news items</p>

The default order of a table can also be set in the admin (Schema → your table → sort field).

If you know SQL, fetch also accepts a query. Always pass values as :parameters – never glue them into the SQL text:

{% set latest = fetch(
  'select Title, Slug, Date from News where Date >= :from order by Date desc limit 5',
  {from: "now"|date_modify("-30 days")|date("Y-m-d")}
) %}

Coming from older versions (Twig 2)? {% for x in list if x.Published == 'true' %} no longer works in Twig 3. Write {% for x in list|filter(x => x.Published == 'true') %} instead.

7. Site settings and other single-record tables

Some tables hold just one record – site name, contact e-mail, social links. When a table is set to a single record in the admin, fetch returns that record directly instead of a list:

{% set settings = fetch("SiteSettings") %}
<title>{{ settings.SiteName }}</title>
<a href="mailto:{{ settings.Email }}">{{ settings.Email }}</a>

Put this in base.twig and the values are available on every page – and editable in the admin.

8. Adding the CMS to an existing HTML site

Already have a finished HTML site? You don't need to rebuild it:

  1. Extract EvolvitCMS into the site's folder (next to your index.html, css/, images/).
  2. Move each page into twigtemplates/ and rename it: about.html → twigtemplates/about.twig. Your home page becomes twigtemplates/index.twig. Delete the old .html files afterwards, so they don't stay reachable at their old addresses.
  3. Change links from about.html to about.
  4. Replace only the text and images the client should be able to edit with {{ … }} – the rest of your HTML and CSS stays exactly as it is.

For example, an editable headline and photo on an existing page:

{% set home = fetch("HomePage") %}
<section class="hero">
  <h1>{{ home.Headline }}</h1>
  <img src="img/{{ home.Photo }}" alt="">
</section>

9. Cheat sheet

{{ value }}print a value (escaped)
{{ html|raw }}print HTML from the HTML editor
{% for x in list %}…{% else %}…{% endfor %}loop, with an "empty" case
{% if a %}…{% elseif b %}…{% else %}…{% endif %}condition
{% set name = value %}variable
{% extends "base.twig" %}, {% block x %}layouts
{% include "menu.twig" %}insert another template
{# comment #}comment, not sent to the browser
fetch("Table"), fetch("Table", {id: slug})content from the admin
_GET[1], _GET.pageparts of the address / ?page=2
_BASEthe site's base address
loop.index, loop.first, loop.lastinside a for loop
|date("d.m.Y") |upper |length |first |slice(0, 3) |default("–")useful filters

Want to experiment without installing anything? Try the Playground – edit templates on the left and see the result immediately. The full Twig documentation is at twig.symfony.com.