Sacred Socks
Sacred Socks

Reputation: 402

Twig CamelCase Filter in Symfony2

So I'm pretty new to Symfony2 and I'm trying to use the camelize filter in a twig template. However when I request the page I get an error saying that the filter doesn't exist:

The filter "camelize" does not exist in ::base.html.twig

Here's the line from my template file:

{{ 'hello world' | camelize }}

The filter is listed on Twig's quick reference page.

I'm confused, doesn't Symfony2 support all of twig's filters? There seem to be quite a few missing, why? And if it doesn't support them, then is there any way to add the missing ones in?

Thanks in advance!

edit Ok, so it turns out I'm retarded and I need to remember to check that I've actually got the right git project. No wonder I was confused. Thanks replies!

Upvotes: 4

Views: 13967

Answers (5)

Zyfella
Zyfella

Reputation: 75

In case someone doesn't want to write a php funtion or filter for a solution, which is recommended if you're trying to use the camelizer in a lot of places during your programming, here is a solution using only Twig step by step cutting words and updating them:
in this case, I will make a snake case into a camel case, which will hopefully cover all possible scenarios:

    {% set snakeVariable = "snake_to_camel_case" %}

{# first we remove all the underscores _ from snakeVariable #}
{% set variable = snakeVariable|replace({'_': ' '}) %} {# output : "snake to camel case" #}
{% set words = variable|split(' ') %}
{# output :
array:3 [▼
0 => "snake"
1 => "to"
2 => "camel"
3 => "case"
] #}

{# now we take the first word seperately #}
{% set firstWord = words|first %} {# output : snake }


{# here we take the rest of the words cutting out the first word with slice(1) #}
{# join the array words using  join(' ') #}
{# uppercase every first letter of the words |title #}
{# remove whitespace between the words replace({' ': ''} #}
{% set restWords = words|slice(1)|join(' ')|title|replace({' ': ''})  %} {# output: ToCamelCase }


{# combine the whole variable back to one  " snake ~ ToCamelCase #}
{% set allWords = firstWord ~ restWords %}
All words: {{ allWords }} {# output: snakeToCamelCase #}

I tried explaining the filters and functions using in-code comments.

Upvotes: 1

Tunji Oyeniran
Tunji Oyeniran

Reputation: 4394

Here is the best solution by default in Craft CMS 3

Craft 3 now has a |camel filter for twig

https://docs.craftcms.com/v3/dev/filters.html#camel

{{ 'foo bar'|camel }}
{# Output: fooBar #}

Upvotes: 0

Massimiliano Arione
Massimiliano Arione

Reputation: 2466

The filter you're looking for is named "title": http://twig.sensiolabs.org/doc/filters/title.html

Upvotes: 3

Samia Ruponti
Samia Ruponti

Reputation: 4038

Symfony 2 has title filter for camel case use

{{ entity.yourstring | title }}

to camel case your string

Upvotes: 13

gremo
gremo

Reputation: 48899

Your link points to a fork on GitHub, meaning a modified copy of the original project. The original project is https://github.com/fabpot/Twig.

There is no camelize filter in Twig. Built-in filters are here. You can write your own camilize filter (it's easy, actually...) following this tutorial: How to write a custom Twig Extension.

EDIT: just for fun, you can write something like:

class MyTwigExtension extends Twig_Extension
{
    public function getFilters()
    {
        return array(
            'camelize' => new Twig_Filter_Method($this, 'camelizeFilter'),
        );
    }

    public function camelizeFilter($value)
    {
        if(!is_string($value)) {
            return $value;
        }

        $chunks    = explode(' ', $value);
        $ucfirsted = array_map(function($s) { return ucfirst($s); }, $chunks);

        return implode('', $ucfirsted);
    }

    public function getName()
    {
        return 'my_twig_extension';
    }
}

Note that this is a quick and dirty filter! Take a look at the built-in filters to learn best practice!

Upvotes: 9

Related Questions