EzDevInfo.com

twig.js

JS implementation of the Twig Templating Language

Ternary operators in Twig php

Is it possible to use ternary operators in twig template? Now, for adding some class to DOM element depend on some condition I do like this:

{%if ability.id in company_abilities%}
    <tr class="selected">
{%else%}
    <tr>
{%endif%}

Instead of

<tr class="<?=in_array($ability->id, $company_abilities) ? 'selected' : ''?>">

in native php template engine.


Source: (StackOverflow)

Get the environment name in a Twig template with Symfony2

Is there a way to get the name of the current environment in a Twig template? I'd like to set some CSS style values depending on it.


Source: (StackOverflow)

Advertisements

symfony 2 twig limit the length of the text and put three dots

How can I limit the lenght of the text eg 50 and put three dots in the display

{% if myentity.text|length > 50 %}

{% block td_text %} {{ myentity.text}}{% endblock %}

{%endif%}

Source: (StackOverflow)

PhpStorm wrap/surround selection?

Often in coding and templating i need to wrap a certain part of text. Is there any shortcut to wrap the current selection, for example:

Hello World
"Hello World"

Hello World
{{ trans 'Hello World' }}

Im using PhpStorm 7 for Mac and PC. I found something similiar, with: ctrl+alt+j you can wrap with a html-tag but nothing else. Also ctrl+alt+- comments the current selection according to the current file format(php, twig, html, ...)


Source: (StackOverflow)

How to concatenate strings in twig

Anyone knows how to concatenate strings in twig? I want to do something like:

{{ concat('http://', app.request.host) }}

Source: (StackOverflow)

How to check for null in Twig?

What construct should I use to check whether a value is NULL in a Twig template?


Source: (StackOverflow)

AngularJS-Twig conflict with double curly braces

As you know, both angular and twig has common control construction - double curly braces. How can I change default value of Angular?

I know that I can do it in Twig, but in some projects I can't, only JS.


Source: (StackOverflow)

How to access class constants in Twig?

I have a few class constants in my entity class, e.g.:

class Entity {
    const TYPE_PERSON = 0;
    const TYPE_COMPANY = 1;
}

In normal PHP I often do if($var == Entity::TYPE_PERSON) and I would like to do this kind of stuff in Twig. Is it possible?


Source: (StackOverflow)

How to get config parameters in Symfony2 Twig Templates

I have a Symfony2 Twig template. I want to output the value of a config parameter in this twig template (a version number). Therefore I defined the config parameter like this:

parameters:
    app.version: 0.1.0

I'm able to use this config parameter in Controllers but I have no clue how to get it in my Twig template.


Source: (StackOverflow)

get current url in twig template?

Possible Duplicate:
How to get current route in Symfony 2?

I looked around for the code to get the current path in a Twig template (and not the full URL), i.e. I don't want http:www.sitename.com/page, I only need /page.


Source: (StackOverflow)

Twig for loop and array with key

I use Twig and I have an array with key like this :

array[1] = "Array1"
array[2] = "Aay1"
array[3] = "Aray1"
array[8] = "Arr1"
array[9] = "Array"

And I would like to get the key (1,2,3,8,9) and the content (Array1, Aay1, Aray1, Arr1, Array) in a loop to get all value of this array.

How I can do this ?

Thank you


Source: (StackOverflow)

Display a string that contains HTML in twig template

How can I display a string that contains HTML tags in twig template?

My PHP variable contains this html and text:

$word = '<b> a word </b>';

When I do this in my twig template:

{{ word }}

I get this:

&lt;b&gt; a word &lt;b&gt;

I want this instead:

<b> a word </b>

Is it possible to get this easily?


Source: (StackOverflow)

Find substring in the string in TWIG

I want to find substring of the string or check if there is no such substring using Twig. On the words, I need analogue of 'strstr' or 'strpos' in php. I googled and searched this issue in stackoverflow but nothing found. Does someone know how to solve this problem?


Source: (StackOverflow)

Twig: in_array or similar possible within if statement?

I am using Twig as templating engine and I am really loving it. However, now I have run in a situation which definitely mustbe accomplishable in a simpler way than I have found.

What I have right now is this:

{% for myVar in someArray %}    
    {% set found = 0 %}
    {% for id, data in someOtherArray %}
        {% if id == myVar %}
            {{ myVar }} exists within someOtherArray.
            {% set found = 1 %} 
        {% endif %}
    {% endfor %}

    {% if found == 0 %}
        {{ myVar }} doesn't exist within someOtherArray.
    {% endif %}
{% endfor %}

What I am looking for is something more like this:

{% for myVar in someArray %}    
    {% if myVar is in_array(array_keys(someOtherArray)) %}
       {{ myVar }} exists within someOtherArray.
    {% else %}
       {{ myVar }} doesn't exist within someOtherArray.
    {% endif %}
{% endfor %}

Is there a way to accomplish this which I haven't seen yet?

If I need to create my own extension, how can I access myVar within the test function?

Thanks for your help!


Source: (StackOverflow)

twig: IF with multiple conditions

It seem I have problem with a twig if statement.

{%if fields | length > 0 || trans_fields | length > 0 -%}

The error is:

Unexpected token "punctuation" of value "|" ("name" expected) in 

I can't understand why this doesn't work, it's like if twig was lost with all the pipes.

I've tried this :

{% set count1 = fields | length %}
{% set count2 = trans_fields | length %}
{%if count1 > 0 || count2 > 0 -%}

but the if also fail.

Then tried this:

{% set count1 = fields | length > 0 %}
{% set count2 = trans_fields | length > 0 %}
{%if count1 || count2 -%}

And it still doesn't work, same error every time ...

So... that lead me to a really simple question: does Twig support multiple conditions IF ?


Source: (StackOverflow)