简体   繁体   中英

Symfony2: global variables in php templating engine

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

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...

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM