7

I want to retrieve Cities names from a table in the database and put them as options in a select input (combobox) which is defined in 'layout.html.twig' . All my views extends 'layout.html.twig', so how can I access to cities names in every page?

[Solution]

I'm not able to respond to my topic ,I didn't have much reputation so I edit my topic

I have found the solution, using "embedding controllers"

  • first I've created an action to retreive all cities names:

    public function listCitiesAction(){
     // retreiving cities
    $entities = $this->getDoctrine()->getRepository("MedAdBundle:City")->findAll();
    
    return $this->render('MedAdBundle:Ville:list_cities.html.twig',
        array('entities' => $entities));
    
    }
    
  • this action render list_cities.html.twig defined as :

    <select class="form-control">
    {% for entity in entities %}
    <option>{{ entity.name}}</option>
    {% endfor %}
    </select>
    
  • finnaly I edit my layout.html.twig

    <div>
    {{ render(controller('MedAdBundle:City:listCities'))}}
    </div>
    

In this way I can access to cities combobox in every page in my app ;)

Cœur
  • 37,241
  • 25
  • 195
  • 267
user3190603
  • 71
  • 1
  • 3
  • You could use a global twig-variable http://symfony.com/doc/current/cookbook/templating/global_variables.html Instead you should use autocomplete in your form, see for instance: http://stackoverflow.com/questions/11962613/how-to-add-an-autocomplete-field-in-a-symfony2-form-for-a-collection-and-using-p – dbrumann Jan 13 '14 at 15:34

3 Answers3

10

Another nice way would be use render.

This allows you to call a controller out of your layout.html.twig

{{ render(controller("AcmeDemoBundle:Helper:citySelector")) }}

you also can cache the output with ESI.

Flask
  • 4,966
  • 1
  • 20
  • 39
  • dude this is great. I didn't know this. No service, no code duplicate on every controller. Just great. – pregmatch Jan 13 '14 at 16:01
  • @Sparkup why if you have fat blown controllers for sure.. the benefit on the other hand is that you could use caching (ESI) oder hinclude (javascript async load of the fragment) http://symfony.com/doc/current/book/http_cache.html#using-esi-in-symfony2 – Flask May 27 '14 at 13:41
3

It's well explained in the cookbook.

http://symfony.com/doc/current/cookbook/templating/global_variables.html

Jorge Faianca
  • 791
  • 5
  • 11
1

I would go with these steps:

  • Write a form hosting a symfony entity field configured to work with the Cities table
  • Define this form as a service in the DIC
  • Define a twig extension that exposes a function to output the form HTML
  • Use the twig function in the layout.html.twig that is extended by all the other templates

As an optimization I would look how I could wire Doctrine with some caching system (e.g. memcached) to avoid hitting the database on each page load.

This is where you can find documentation about the entity field: http://symfony.com/doc/current/reference/forms/types/entity.html

Use the Symfony documentation to find how to define a form as a service and how to write your own twig extension.

Aldo Stracquadanio
  • 6,167
  • 1
  • 23
  • 34