I thought it would be useful to have an issue to track functions/filters that themers miss when working on the conversion and perhaps later on.

And just another thought: we will probably need a way to let other modules provide additional functions/filters. But we can save that for later.

We'll need to decide weather we want things like t or count to be filters or functions in twig.

In addition to the functions and filters already provided by twig, at the moment Drupal twig.engine maps the following:

Current state

Functions:

  1. render_var
  2. url
  3. path
  4. link
  5. file_url
  6. attach_library
  7. active_theme_path
  8. active_theme

Filters:

  1. t
  2. trans
  3. placeholder
  4. drupal_escape
  5. safe_join
  6. without
  7. clean_class
  8. clean_id
  9. render
  10. format_date

Candidates

Drupal functions to Twig functions

  • attributes/drupal_attributes
  • render
  • format_interval

PHP functions to Twig functions

  • unset

Drupal functions to Twig filters

Excluded/Replaced

  1. show()/hide() become the |without filter. @see https://www.drupal.org/node/2212845

Related issues
#1778968: Convert theme_table to Twig - _theme_table_cell

Comments

daggerhart’s picture

Very late to the game here, sorry if I'm posting this out of place. I'm excited what I am learning about Twig, and want to become involved and help.

Twig filters look incredibly useful, especially since they chain. To answer your questions, I would like to see t() and count() as filters.

Chaining with the replace filter.
{{ node.title|replace({"foo":"bar"})|replace({"bar":"buz"}) }}

current t()

print t("2 days in the future is @date", array('@date' => date('M-d-Y', strtotime('+2days'))));

twig filter |t()
{{ "2 days in the future is @date"|t({ "@date" : '+2days'|date('M-d-Y') }) }}
Seems reasonable to me.

count()
With count, there is already a filter in twig for length. http://twig.sensiolabs.org/doc/filters/length.html

Is there another need count() fulfills that |length doesn't ?

Example iterate using length:

{% for i in 0..object|length %}
    * {{ i }}
{% endfor %}
jenlampton’s picture

Project: Drupal 8 with twig - abandoned sandbox »

In Munich, we decided to always use filters over functions, but I'm hesitant to make that decision for one major reason.

Twig functions work just like PHP functions, and Drupal is built on PHP. The learning curve to becoming a Drupal developer starts in the theme layer, and eventually even theme devs will need to know PHP to use preprocess functions. If we start by giving people only filters they'll need to learn yet another syntax.

Right now, the twig engine provides all PHP functions both as functions and as filters. We may decide to change this later on :)

Also - Moving to the active sandbox.

jenlampton’s picture

adding tags

jenlampton’s picture

Issue tags: +Twig engine
Fabianx’s picture

Component: Code » Twig templates
Priority: Normal » Major
Status: Active » Needs work

The syntax of:

'Hello'|t(...)

is very nice, but: Can the translation scripts _find_ and detect these.

That might be a reason to stick to t('Hello', ...).

Fabianx’s picture

Project: » Drupal core
Issue summary: View changes

adding issue summary from duplicate issue

joelpittet’s picture

Project: Drupal core »
Component: Twig templates » Twig templates conversion (front-end branch)

Consider adding format_plural to twig filters, or better yet

http://twig.sensiolabs.org/doc/extensions/i18n.html

With plural and trans blocks

steveoliver’s picture

Project: » Drupal core
Version: » 8.x-dev
Component: Twig templates conversion (front-end branch) » theme system
Priority: Major » Normal
Status: Needs work » Active
Issue tags: +Twig

Moving this to the core queue with lower priority and marking as task.

steveoliver’s picture

Title: Filters or Functions? » Twig: Filters or Functions?

Updating title. :/

Sutharsan’s picture

IMO twig documentation is not very clear about what the difference between a function and a filter is. My conclusion is that a Twig filter uses something that can already be outputted and converts it into desired output.
For example:

{{ name|striptags|title }}

What is left of the pipe symbol (name) is meaning full output.

While a Twig function uses or acts upon its parameter. The function will return output, but the parameter is (usually) no valid output. This discussion helped me understanding this difference between functions and filters.

From the functions mentioned in the issue summary, t, show and hide fit in the filter definition:

{{ 'Read more' | t }}                                {# Outputs the translation of 'Read more' #}
{{ content | hide (['field_foo']) }}                 {# Outputs the content except for field foo #}
{{ content | show (['field_foo', 'field_bar]') }}    {# Outputs only fields foo and bar from content #}
ksenzee’s picture

Re #5, the issue for that is #2011442: Support for Drupal 8 twig t filter translatables. It shouldn't be too hard to pick up the new syntax - just a new regex or two. Certainly not a big enough deal that Twig should compromise on the right thing to do here.

ksenzee’s picture

Issue summary: View changes

Add current state

joelpittet’s picture

Issue summary: View changes

Updated the current state.

sergeypavlenko’s picture

Priority: Normal » Major

Hi, all

Now function "hide" not work. There are plans to add it?

I'm use function so:

{% hide(content.links) %}
{% content|hide(content.links) %}
{% content|hide('content.links') %}

star-szr’s picture

Priority: Major » Normal
joelpittet’s picture

Title: Twig: Filters or Functions? » Twig: Filters or Functions
Category: Task » Plan
Issue summary: View changes

Repurposed this issue to track things as a plan.

joelpittet’s picture

Version: 8.0.x-dev » 8.1.x-dev
joelpittet’s picture

Title: Twig: Filters or Functions » TwigExtension: Filters and Functions
joelpittet’s picture

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.0-beta1 was released on March 2, 2016, which means new developments and disruptive changes should now be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.0-beta1 was released on August 3, 2016, which means new developments and disruptive changes should now be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.0-alpha1 will be released the week of January 30, 2017, which means new developments and disruptive changes should now be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.0-alpha1 will be released the week of July 31, 2017, which means new developments and disruptive changes should now be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.