hassen zouari
hassen zouari

Reputation: 971

Passing parameters to Twig extension from template?

I use Twig extension to pass global variables from the database like in the code below. But I want to make this more dynamic to get data from database by the id parameter ..

service

app.twig.database_globals_extension:
 class: Coursat\CoursatBundle\Twig\Extension\DatabaseGlobalsExtension
 arguments: ["@doctrine.orm.entity_manager"]
 tags:
     - { name: twig.extension }

extension

<?php

namespace Coursat\CoursatBundle\Twig\Extension;

use Doctrine\ORM\EntityManager;

class DatabaseGlobalsExtension extends \Twig_Extension
{

   protected $em;

   public function __construct(EntityManager $em)
   {
      $this->em = $em;
   }

   public function getGlobals()
   {
      return array (
              "myVariable" => $this->em->getRepository('CoursatBundle:test')->find(##I want to pass a var here from the template##),
      );
   }

   public function getName()
   {
      return "CoursatBundle:DatabaseGlobalsExtension";
   }

}

template

{{ myVariable.name() }}

Upvotes: 0

Views: 5508

Answers (3)

voodoo417
voodoo417

Reputation: 12101

If the question is "Passing parameters to Twig extension from template?"

E.g. for Filters

//... 
use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;

class SomeFilterExtension extends AbstractExtension
{

  public function getFilters()
  {
    return [
        new TwigFilter('someFilter', [$this, 'someFilter']),
    ];
}

public function someFilter(mixed $baseValue,mixed $mandatoryParam,mixed $optionalParam = null): mixed
{
    // ....
    return $baseValue;
}

}

Inside templates:

// ...
{{ blaBlaBlaValue|someFilter(mandatoryParam) }}
// ...

or

// ...
{{ blaBlaBlaValue|someFilter(mandatoryParam,optionalParam) }}
// ... 

Look at -> Twig Extension

Upvotes: 0

Alain
Alain

Reputation: 36954

This is a very bad idea to store this in your globals, because your database will be requested for each calls of your website.

You can use a function to retrieve those data:

<?php

namespace Coursat\CoursatBundle\Twig\Extension;

use Doctrine\ORM\EntityManager;

class DatabaseGlobalsExtension extends \Twig_Extension
{

   protected $em;

   public function __construct(EntityManager $em)
   {
      $this->em = $em;
   }

   public function getFunctions()
   {
        return array(
            new \Twig_SimpleFunction('my_test', array($this, 'myTest')),
        );
   }

   public function myTest($id)
   {
      return $this->em->getRepository('CoursatBundle:test')->find($id);
   }

   public function getName()
   {
      return "CoursatBundle:DatabaseGlobalsExtension";
   }

}

Then in your Twig template, use this to load your entity:

{% set twigVar = my_test(42) %}

This is still a bad practice though, you should load your entities in your controllers, not in your views.

Upvotes: 3

DarkBee
DarkBee

Reputation: 15633

class DatabaseGlobalsExtension extends \Twig_Extension
{
    ...
    ...
    public function getFunctions() {
        return array(
             'get_db_global', function($key) {
                  $globals = $this->getGlobals();
                  return isset($globals[$key]) ? $globals[$key] : null;
              }
        );
    }
    ...
    ...
}

Inside twig :

   The global with key "Foo" is : {{ get_db_global('foo') }}

Upvotes: 1

Related Questions