This is a very useful Twig that I use in a lot of different forms.  I attach a listener and when they click “Update” it will redirect the user by appending the URL with the /year/month to hook nice and cleanly in built-in routes to pass the year and month into different controllers.

<form id="yearMonthWidget" name="newDate1" method="get" action="">

   <select name="month">
      {% for mo in 1..12 %}
         <option value="{{ mo }}">{{ ('2012-' ~mo~'-01')|date("M") }}</option>
      {% endfor %}

   </select>

   <select name="year">
      {% for yr in "now"|date("Y")+1..2010 %}
         <option value="{{ yr }}">{{ yr }}</option>
      {% endfor %}
   </select>
   <input type="submit" value="Update"/>
</form>

 


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 Cheatsheet with recent updates added, a perfect reference for Symfony web developers.


Twig blocks are like methods in a class.  If the Twig container is a class, Twig blocks are methods.  So you can call on a parent Twig view and extend the parent just like a function using the “parent()” function like below within the dynamic content tags, which are a pair of squiggly brackets.  Here’s an example of how you can extend a parent’s block from a child view.

{% extends “MainBundle::Base:main.html.twig” %}

{% block Css %}
   {{ parent() }}
   <link rel="stylesheet" href="addUniqueStylesheetForThisView.css">
   <link rel="stylesheet" href="addAnotherStylesheet.css">
{% endblock %}

Without using “parent()” above, this child “Css” block will completely override the parent’s Css block.  As the title states, this is easy, useful and helps your code stay “DRY” – Don’t Repeat Yourself.

Is your company still not on a PHP framework?  Do you feel you need to modernize your company’s web, code, systems, databases and infrastructure?  Consider a “Business Systems Audit” by Belchamber Business Consultants, they will help you cut costs, improve your web development workflow, all while improving the stability of your code and systems and making your entire organization more effective.  PHP Symfony experts can help shed light on your I.T. and/or web development department.  Maybe it’s time to shake things up?


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}}

{{app.security.token.user}}

{{app.session.locale}}

Need more Twig tips?  View our Symfony Twig Cheatsheet!

Looking for a PHP developer, a Symfony expert or WordPress web developer?  Perhaps a business system architect that specializes in scalable MySQL business system design and deployment?  We have reliable, fast and professional developers that meets your organization’s needs!  Contact us for your next web development project and schedule a free consultation today.


Here is a quick way to access the user object from the Symfony user or the FOSUserBundle.  In this Twig example below is a conditional statement that if the user is authenticated and has a role, it will display “Hi Aaron Belchamber” or whatever the user’s name is.

{% if is_granted("ROLE") %}

    Hi {{ app.user.username }}!

{% endif %}