Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call ucwords in twig?

Tags:

twig

EDIT: Dec 3 2016

  • Want to learn how to add custom extensions(filters) to twig? see this answer by lxg

  • Do you just need to find the twig equivalent for ucwords? see this answer by Javier Eguiluz

I found several posts on calling php functions from twig, that show it should be supported, however it doesn't seem to work.

{{ ucwords( item|replace({'_':' '}) ) }}

results in :l

Slim Application Error

The application could not run because of the following error:

Details

Type: Twig_Error_Syntax Message: The function "ucwords" does not exist in "home.twig" at line 101

File: /usr/share/dev89/html/vhosts/local/libs/vendor/twig/twig/lib/Twig/ExpressionParser.php Line: 572

like image 979
r3wt Avatar asked Jun 30 '15 18:06

r3wt


People also ask

Can I use Twig without Symfony?

Twig is a template engine for PHP and can be used without Symfony, although it is also made by SensioLabs.

What are filters in Twig?

Filters in Twig can be used to modify variables. Filters are separated from the variable by a pipe symbol. They may have optional arguments in parentheses. Multiple filters can be chained. The output of one filter is applied to the next.

What is Symfony Twig?

It's an open source product licensed under a BSD License and maintained by Fabien Potencier. The initial version was created by Armin Ronacher. Symfony PHP framework comes with a bundled support for Twig as its default template engine since version 2. Twig.


2 Answers

As @lxg said, it's not possible to call all PHP functions from Twig templates ... unless you want to do that and define your own filters/functions. Instead of a drawback, this is a good thing to "force" you to create good templates that don't contain too much logic.

Anyway, in this particular case, Twig already contains a filter called title which applies the "title case", which is equivalent to the ucwords() PHP function:

{{ item|replace({'_':' '})|title }}
like image 71
Javier Eguiluz Avatar answered Oct 13 '22 13:10

Javier Eguiluz


Update: Twig 2.x comes with the capitalize filter which does exactly that.


It is not true that all PHP functions are available in Twig. Only a few Twig filters and functions go by the same names as their equivalents in PHP.

But you can easily create your own Twig extension for ucwords – filter as well as function:

<?php

namespace Acme\TestBundle\Twig;

class UcWordsExtension extends \Twig_Extension
{
    public function getFunctions()
    {
        return [
            new \Twig_SimpleFunction('ucwords', 'ucwords')
        ];
    }

    public function getFilters()
    {
        return [
            new \Twig_SimpleFilter('ucwords', 'ucwords')
        ];
    }
    
    public function getName()
    {
        return 'ext.ucwords';
    }

}

The first parameter of Twig_SimpleFunction/Twig_SimpleFilter is the name of the function/filter in Twig. The second parameter is a PHP callable. As the ucfirst function already exists, it is sufficient to pass its name as a string.

Test in Twig:

{{ "test foobar"|ucwords }} {# filter #} <br>
{{ ucwords("test foobar") }} {# function #} 

Returns:

Test Foobar
Test Foobar
like image 40
lxg Avatar answered Oct 13 '22 14:10

lxg