twig.js
JS implementation of the Twig Templating Language
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)
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)
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)
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)
Anyone knows how to concatenate strings in twig? I want to do something like:
{{ concat('http://', app.request.host) }}
Source: (StackOverflow)
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)
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)
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)
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)
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:
<b> a word <b>
I want this instead:
<b> a word </b>
Is it possible to get this easily?
Source: (StackOverflow)
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)
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)
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)