• Home
  • PHP
  • Twig and Timber usage in theme creation with wordpress

Twig and Timber usage in theme creation with wordpress

linkaiyi
Follow

combine string in twig template

use ‘~’ to concatenate string together

This should work fine:

{{ 'http://' ~ app.request.host }}

To add a filter - like ‘trans’ - in the same tag use

{{ ('http://' ~ app.request.host) | trans }}

As Adam Elsodaney points out, you can also use string interpolation, this does require double quoted strings:

{{ "http://#{app.request.host}" }}

truncate words and add …

{{ myentity.text|length > 50 ? myentity.text|slice(0, 50) ~ '...' : myentity.text  }}

use for loop and loop index

{% for block in blocks %}
  {% if loop.index0 % 2 == 0 %}
    <div>
  {% endif %}
   {# other stuff #}
  {% if loop.index0 % 2 == 1 or loop.last %}
    </div>
  {% endif %}
{% endfor %}

Or, more generally,

{% set cols = 2 %}
{% for block in blocks %}
  {% if loop.index0 % cols == 0 %}
    <div>
  {% endif %}
   {# other stuff #}
  {% if (loop.index0 % cols == cols - 1 or loop.last) %}
    </div>
  {% endif %}
{% endfor %}

query string for wordpress

$context = Timber::get_context();
$args = array(
// Get post type project
'post_type' => 'project',
// Get all posts
'posts_per_page' => -1,
// Gest post by "graphic" category
'meta_query' => array(
    array(
        'key' => 'project_category',
        'value' => 'graphic',
        'compare' => 'LIKE'
    )
),
// Order by post date
'orderby' => array(
    'date' => 'DESC'
));

$context['graphics'] = Timber::get_posts( $args );
Object has 0 attachments

Was this article helpful?

This article is viewed 61 times!

0 Comments

Leave a Comment

Support