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') %}

    Cottser’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.