Writing Template - miya0001/content-template-engine GitHub Wiki

Variables

Variables from $post object

$post object is assigned as post to the template by default.

You can use post in your template like following.

<p>{{ post.post_title }}</p>

Then:

<p>Hello World!</p>

Variables from custom fields

If you would set a custom field,

Then you can write template like following.

<h2>{{ post.event_title }}</h2>

Variables from Advanced Custom Fields

Variables by Advanced Custom Fields is assigned as acf.

<p>{{ post.acf.my_field }}</p>

Also, you can use The Repeater Fields.

<table class="table table-striped">
  <tr><th>Name</th><th>Price</th></tr>
  {% for fruit in post.acf.fruits %}
    <tr><td>{{ fruit.name }}</td><td class="price">{{ fruit.price | number_format }}</td></tr>
  {% endfor %}
</table>

Then:

Variables from Smart Custom Fields

https://wordpress.org/plugins/smart-custom-fields/

Variables by Smart Custom Fields is assigned as scf.

<p>{{ post.scf.my_field }}</p>

Also, you can use repeater fields.

<table class="table table-striped">
  <tr><th>Name</th><th>Price</th></tr>
  {% for fruit in post.acf.fruits %}
    <tr><td>{{ fruit.name }}</td><td class="price">{{ fruit.price | number_format }}</td></tr>
  {% endfor %}
</table>

Filters

Variables can be modified by filters. Filters are separated from the variable by a pipe symbol |. Multiple filters can be chained. The output of one filter is applied to the next.

Following example escapes html tags.

{{ post.post_title | esc_html }}

The Content Template Engine is enabled autoescape by default.

See also Filters.

Functions

Functions can be called to generate content. Functions are called by their name followed by parentheses (()) and may have arguments.

Following example includes specific post by post_id.

{% include_post( '1234' ) %}

Control Structure

For example, to display a list of fruits provided in a variable called fruits, use the for tag:

{% for fruit in acf.fruits %}
  <li>{{ fruit.name }}: {{ fruit.price | number_format }}</li>
{% endfor %}

Next example, {% if %} ... {% endif %} allows for conditional execution of code fragments:

{% if is_home() %}
  <h2>Hello</h2>
{% endif %}

See also Twig official documentation.

⚠️ **GitHub.com Fallback** ⚠️