12 articles Twig & Doctrine

Rendering Twig templates from Symfony Repository without Using Dependency Injection (DIC)

Sometimes it's useful to access Twig templates from a Symfony repository, one could argue that this goes against a repository's role, but if you think of Twig template files or Twig strings perhaps grabbed from a database as data, then this coincides nicely with the function of a repo. These arguments aside, whatever your situation,…

Symfony Twig: Accessing global values reference

Here is a reference list to access global values within Twig: For $_POST variables use this : {{ app.request.parameter.get("page") }} For $_GET variables use this : {{ app.request.query.get("page") }} For $_COOKIE variables use this : {{ app.request.cookies.get("page") }} For $_SESSION variables use this : {{ app.request.session.get("page") }} For more Symfony Twig shortcuts, view the Twig…

Accessing Symfony global and server variables in Twig views through the “app” variable

In Symfony, you don't always have to actually pass values from your controllers to your Twig views.  Twig can actually pull global and server values directly through the super global "app" variable.  Most frequently, you can access these corresponding objects like Kernel, Request, Security and Session variables like so: {{app.kernel.cachedir}} {{app.kernel.logdir}} {{app.kernel.rootdir}} {{app.request.basepath}} {{app.request.baseurl}} {{app.request.uri}}…

How to show or access cookie values in Symfony Twig views

Strange how obscure it is to find a clear example of how to just access or show the value of a cookie in Twig, so here it is! {{ app.request.cookies.get('yourCookieHere') }} Twig is the templating engine that powers Symfony views.  Symfony is the PHP framework from Sensio Labs. If you are a company that uses…

Example of basic looping through array values in Symfony Twig

Cycle through an array of values for current year and 5 years into the future: {% set start_yr = date() | date('Y') %} {% set end_yr = start_yr + 5 %} {% for yr in start_yr..end_yr %} {{ cycle(['odd', 'even'], loop.index0) }} {% endfor %} The array "colors" would probably be better off defined in…