How to display a list of years from a specific year using Twig and Symfony2

With this application that I am creating, I want the user to be able to watch recordings from previous years. Therefore, if this user has records related to 2005, I want to be able to display a list of years since then, including this year, for example:

List of years: 2012 | 2011 | 2010 | 2009 | 2008 | 2007 | 2006 | 2005

I developed SQL to retrieve the oldest record from the database, which looks like this:

// Retrieve Oldest Record $minimumyear = $dm->createQuery(' SELECT min(msu.endDate) as msuMin FROM InstructorBundle:MapSubscriptions msu LEFT JOIN InstructorBundle:MapContacts mc WHERE msu.contact = mc.id LEFT JOIN InstructorBundle:MapFamilies mf WHERE mc.family = mf.id WHERE mf.branch = :centre' )->setParameter('centre', $centreid); $minyear = $minimumyear->getResult(); 

My problem is that now I have the oldest year, I'm not sure how I can use Twig to display the list that I need. I thought about extending Twig to allow this, but I don’t want to go down this prospectus, so far there is just a feature in Twig that would allow me to do this.

How can I display the list I need using Twig?

+7
symfony twig
source share
1 answer

Just use the twig date function to get the current year with "now" , then go from the minimum year to the current with help for the loop .

 {% set minimumYear = 2010 %} List of years: {{ minimumYear }} {% for year in (minimumYear+1).."now"|date("Y") %} | {{ year }} {% endfor %} 

outputs:

List of years: 2010 | 2011 | 2012 | 2013

.. or get the first year of the current year

 {% set minimumYear = 2010 %} {% set currentYear = "now"|date("Y") %} List of years: {{ currentYear }} {% for year in (currentYear-1)..minimumYear %} | {{ year }} {% endfor %} 

outputs:

List of years: 2013 | 2012 | 2011 | 2010

+17
source share

All Articles