2

There is a cookbook for adding globals to the twig templating engine, but it doesn't get into doing the same thing for the php engine. How would I do this?

So I might have something like:

# config.yml
someSortOfReferenceToThePHPEngineInstance:
    calls:
        - [ addGlobals, ["foo", "bar"] ]
        - [ addGlobals, ["myService", "@myService"] ]

And then access those like:

// templateName.contentType.php
<?
echo $foo; // echos "bar"
echo $myService->myMethod($foo); // echos the result of modifying "bar" with "myMethod" method of "myService" service
1
  • Can you write a code example of how you would like to do this?
    – greut
    Jan 9, 2012 at 17:19

3 Answers 3

3

I could not find any documention on this for the PHP engine...

What does work however is:

Config:

//config.yml    
parameters:
      hello: "YO!"

PHP Template:

// index.html.php
<?php

print $view->container->parameters['hello'];

This does not fit as nicely as the twig convention... Maybe there is better way - I have not debugged any further...

1
  • yeah, that container object also has registered services available. we're integrating with an older system though and want to be able to provide the same interface as an existing codebase. also why we're using php instead of twig. Jan 10, 2012 at 14:37
1

Here are a couple of options:

  1. If you create a base controller that all others inherit from, you can override symfony's render function and add keys to the parameters argument, like:

    public function render($view, array $parameters = array(), Response $response = null){
        if(!array_key_exists("bar", $parameters){
            $parameters["foo"] = $this->get("foo");
        }
        if(!array_key_exists("bar", $parameters){
            $parameters["bar"] = $this->get("bar");
        }
        return parent::render($view, $parameters, $response);
    }
    

    This is the only way I see to modify the "global" variables "globally", though they'll not be available in any views rendered by controllers you don't create (of course, those'll likely be done in Twig anyway and you can use the normal twig means of adding functionality).

  2. The PHP rendering engine has what're called "helpers", which you can access via array keys of $view, like:

    $view["foo"]->doSomething();
    

    We created a class for easily making services into helpers:

    use Symfony\Component\Templating\Helper\Helper as BaseHelper;
    
    class Helper extends BaseHelper{
        protected $name;
        public $service;
    
        public function __construct($name, $service){
            $this->name = $name;
            $this->service = $service;
        }
        public function __get($name){ 
            if(isset($this->service->$name)){
                return $this->service->$name;
            }
        }
        public function __call($name, $arguments){
            if(method_exists($this->service, $name)){
                return call_user_func_array(array($this->service,$name), $arguments);
    
            }
        }  
        public function getName(){
            return $this->name;
        }
    
    }
    

    Then in our configuration under the services we'd add:

       helper.foo:
        class: %helper.class%
        arguments:
            name: "foo"
            helper: "@foo"
        tags:
            - { name: templating.helper, alias: foo }
    

    This would theoretically be available then to any view files, even those with controllers you don't have control of.

0

I had a very same problem. For some reason this feature is only available for Twig templating with TwigBundle. Both Twig and PHP templating engines provide possibility to define global variables, but only Twig engine has configuration for that. For me the only real way to achieve that is something you proposed in the question post - to define method calls (and this is the way Twig globals are registered).

Problem is, that with DI extensions you can't access service definition from outside your extension, so you can't add these calls from your DI extension. The way for me was to do that with DI compiler pass.

But I'm also developer of ChillDevViewHelpersBundle and since I was facing this problem in most of my projects I decided to implement it there for common use and you can use 0.1.8 release for this feature.

1
  • Cool. We are happy enough with using helpers (like in my answer option 2) for now, but I still like the idea of being able to define globals in configuration, perhaps at framework.templating.globals. Maybe if I get a chance I'll try my hand at a pull request to core Jan 15, 2014 at 7:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.