Questions tagged [twig]

Twig is a modern, fast, flexible, and secure templating engine for PHP. Created for Symfony and adopted by Drupal 8.

Twig is a modern template engine for . It is developed for, and used by default, in the , , and frameworks, but can also easily be used in other projects. It’s both designer and developer friendly by sticking to PHP’s principles and adding functionality useful for templating environments.

Its key features are (source: https://twig.symfony.com/) :

  • Fast: Twig compiles templates down to plain optimized PHP code. The overhead compared to regular PHP code was reduced to the very minimum.
  • Secure: Twig has a sandbox mode to evaluate untrusted template code. This allows Twig to be used as a template language for applications where users may modify the template design.
  • Flexible: Twig is powered by a flexible lexer and parser. This allows the developer to define its own custom tags and filters, and create its own DSL.

When asking or answering a question, do not hesitate to isolate the problematic code to reproduce it or solve it in an external environment, such as twigfiddle, to get more efficient help or give a working solution.

For more details refer to:

  1. Official Website
  2. Twig Documentation
  3. Twig Installation
  4. Twig Download from Github
10606 questions
3
votes
3 answers

Slim Framework 404 css issue

Hello so I am using slim framework for a simple project and currently having an issue. My code for throwing a 404 error is: $app->notFound(function () use ($app) { $app->render('404.html'); }); The code will go to the 404 page for 2 unknown url…
wobsoriano
  • 12,348
  • 24
  • 92
  • 162
3
votes
1 answer

symfony custom form type with assets

I've created a custom form type in symfony2. This formtype has it's own template and this is working fine. The form type also needs some javascript on the clientside to work nicely. I would like to add this javascript to the page using the same…
DoppyNL
  • 1,415
  • 1
  • 14
  • 24
3
votes
1 answer

Fetching objects from database in Symfony 2.7.3

I'm trying to show some object properties stored on the database. I've got the controller, the Entity, and the view. I'get no excepctions but I can't see the object properties. Controller: /** * @Route ("/ov", name="ov") */ public function…
jcobo1
  • 1,065
  • 1
  • 18
  • 34
3
votes
1 answer

Dynamic variables in Twigs

In a form, I dynamically define the fields in a controller. Then in the TWIG I would like to use form_widget to output the fields. The normal approach, when knowing the fields is like shown bellow: {{ form_widget(form.field1) }} In this case, we…
Roel Veldhuizen
  • 4,613
  • 8
  • 44
  • 78
3
votes
1 answer

Get Button Id dynamically

I need to get the button id when the button is clicked--- This is my code for my button, to show dynamically data dynamically when the condition is true. Twig: {% for usr in userstats %} {% if usr.condition == TRUE %}
Istiak Mahmood
  • 2,330
  • 8
  • 31
  • 73
3
votes
0 answers

Include Twig File Without Giving File Extension

I am new to twig template engine. I am trying to include some twig file inside another twig file. I am using the following code to include it, {% include 'layout/css.html.twig' %} But for all inclusion i want to include the file without giving…
Vinoth Babu
  • 6,724
  • 10
  • 36
  • 55
3
votes
1 answer

Twig exception on PHP date_create()

I want to calculate a voters age base on her birthday, in my entity, I created a function like this //voters.php public function getAge() { $birthday = $this->birthday; $age = date_diff(date_create($birthday),date_create('today'))->y; …
Danielle Rose Mabunga
  • 1,676
  • 4
  • 25
  • 48
3
votes
0 answers

How to override twig native functions?

I would like to override twig functions include and extend. The reason is that my bundle provides several compleatly different themes, but the file names and tree structure are always the same. E.g. when theme is set to bootstrap and required file…
VladRia
  • 1,475
  • 3
  • 20
  • 32
3
votes
2 answers

Symfony 2/Doctrine: How to lower the num of queries without losing the benefit of ORM?

I'm using Symfony 2.7 with Doctrine. My controller actions often look like: # my/namespace/Controller/ItemsController.php -> listAction() $items = $this->get('repository.items')->findAll(); return $this->render('itemsList.html.twig', array('items'…
Mr. B.
  • 8,041
  • 14
  • 67
  • 117
3
votes
3 answers

Symfony2 Change elements style on every loop made

So I have a collapsable CSS page for FAQ. With static text everyting works fine. However I am not using static text and I am getting the questions and answers from the database. So by normal CSS the collapsable FAQ looks like this:
Dominykas55
  • 1,231
  • 14
  • 45
3
votes
1 answer

Cycle through and pull information from array in Twig

I currently have an array full of pre-populated form fields: $fields = array('title','first_name') $info = array( 'title' => 'Mr', 'first_name' => 'John', 'last_name' => 'Smith' ) As you can see this particular fields array only…
jmack
  • 189
  • 2
  • 12
3
votes
2 answers

twig inheritance and symfony2 controller variables

Im trying my first project using symfony2 + twig. I created basic twig template with defined blocks. It basically looks like this {% block content %} some content... {% endblock %} {% block footer %} {{ footer.content}} {% endblock %} I want…
marysmech
  • 183
  • 1
  • 3
  • 10
3
votes
1 answer

represent a variable in twig using for loop

In my Service I return an array which looks like this: $array = [ 'bible_anagram_word' => $result->getBibleAnagramWord(), 'word_1' => $result->getWord1(), 'word_2' => $result->getWord2(), 'word_3' …
Ron Piggott
  • 705
  • 1
  • 8
  • 26
3
votes
1 answer

How to "break" a loop in Twig?

I want to use break in the Twig template engine. {% for key, db_staff_language in db_staff_languages %} {% for staff_language in model_data.staff_languages %} {% if staff_language.id == db_staff_language.id %}
SuStartX
  • 31
  • 1
  • 4
3
votes
3 answers

Iterate array in javascript

I have in my controller a function to make a search filter. The search parameter is obtained from a select field of my twig template. The selected option is passed to the controller to see where results are with that value. The query result is…
urreta17
  • 183
  • 4
  • 20