Symfony2 branch inheritance variables and controllers

I am trying to complete my first project using symfony2 + twig. I created a basic branch template with specific blocks. It basically looks like

{% block content %}
  some content...
{% endblock %}

{% block footer %}
  {{ footer.content}}
{% endblock %}

      

I want the footer to be the same for all pages. The footer is loaded from the DB and installed in the controller. I wanted to inherit from the template described above to other pages, but I must always set the footer in the controller, otherwise the variable is undefined.

My questions are: if there is any "good" way to set a footer variable for multiple templates inherited from the parent template?

+3


source to share


2 answers


Solution: Embedded Controllers

In some cases, you need to do more than include a simple template. Let's say you have a sidebar in your layout that contains the last three articles. Getting the three articles might involve querying the database or doing other heavy logic that cannot be done from within the template.

The solution is to just insert the whole controller result from your template. First, create a controller that has the number of recent articles:

controller

// src/AppBundle/Controller/ArticleController.php
namespace AppBundle\Controller;
// ...

class ArticleController extends Controller
{
    public function recentArticlesAction($max = 3)
    {
        // make a database call or other logic
        // to get the "$max" most recent articles
        $articles = ...;

        return $this->render(
            'article/recent_list.html.twig',
            array('articles' => $articles)
        );
    }
}

      

View



{# app/Resources/views/article/recent_list.html.twig #}
{% for article in articles %}
    <a href="/article/{{ article.slug }}">
        {{ article.title }}
    </a>
{% endfor %}

      

Markup

{# app / Resources / views / base.html.twig #}

{# ... #}
<div id="sidebar">
    {{ render(controller(
        'AppBundle:Article:recentArticles',
        { 'max': 3 }
    )) }}
</div>

      

+7


source


you can do one of the following: "write a custom Twig extension"

<?php

namespace AppBundle\Extension;

class MyTwigExtension extends \Twig_Extension
{
    private $em;
    private $conn;

    public function __construct(\Doctrine\ORM\EntityManager $em) {
        $this->em = $em;
        $this->conn = $em->getConnection();
    }

    public function getFunctions()
    {
        return array(
            'users' => new \Twig_Function_Method($this, 'getUsers'),
        );
    }

    public function getUsers()
    {
        $sql = "SELECT * FROM users ORDER BY accountname";
        return $this->conn->fetchAll($sql);
    }

    public function getName()
    {
        return 'smproc4_twig_extension';
    }
}

      

Register an extension as a service

services:
    my.twig.extension:
        class: AppBundle\Extension\MyTwigExtension
        tags:
            - { name: twig.extension }
        arguments:
            em: "@doctrine.orm.entity_manager"

      

Using a custom extension



Hello {{ name }}!
<ul>
{% for user in users() %}
    <li>{{ user.accountname }}</li>
{% endfor %}
</ul>

      

take a look at

How to write a custom Twig extension

Extension creation

0


source







All Articles