Twig variable in external JS file

There a two approaches when you need to pass a twig variable to an external javascript file Define the variables inside a script block in the twig template <!DOCTYPE html> <html> <head> <title></title> </head> <body> <script> var my_twig_var = {% if twig_var is defined %}'{{ twig_var }}'{% else %}null{% endif %} </script> <script src=”scripts/functions.js”></script> </body> … Read more

Decoding JSON in Twig

That’s easy if you extend twig. First, create a class that will contain the extension: <?php namespace Acme\DemoBundle\Twig\Extension; use Symfony\Component\DependencyInjection\ContainerInterface; use \Twig_Extension; class VarsExtension extends Twig_Extension { protected $container; public function __construct(ContainerInterface $container) { $this->container = $container; } public function getName() { return ‘some.extension’; } public function getFilters() { return array( ‘json_decode’ => new \Twig_Filter_Method($this, … Read more

AngularJS-Twig conflict with double curly braces

You can change the start and end interpolation tags using interpolateProvider service. One convenient place for this is at the module initialization time. angular.module(‘myApp’, []).config(function($interpolateProvider){ $interpolateProvider.startSymbol(‘{[{‘).endSymbol(‘}]}’); }); https://docs.angularjs.org/api/ng/provider/$interpolateProvider

How to get the value from and save to Database in php

You have at least two options here: hidden fields everywhere if data (best approach): <td> <input type=”checkbox” name=”checkBox[]” id=”ic” value=”{{ user.id }}” /> <input type=”hidden” value=”{{user.lastname}}” name=”v1[{{ user.id }}]”></td> <td data-title=”id”>{{user.id}}</td> <td data-title=”firstName”>{{user.firstname}}</td> <td data-title=”lastName” contenteditable=”true”>{{user.lastname}}</td> and on serverside you do: …. your code …. foreach ($user as $id) { $lastname = $v1[$id]; … save … Read more