How are Symfony elements passed to the template action?

How the element comes into action ...

$this->foo = 'bar';

      

... becomes a variable accessible from the template ...

echo $foo; // bar

      

I would like to know how this is achieved at the framework level.

There is a lot of documentation on how to use Symfony, but I haven't been able to find much on how it all fits together (class structure / inheritance, etc.).

Thanks in advance for your help!

+2


a source to share


1 answer


The general model is as follows:

The controller implements __set()

, which adds variables to the view:

class Controller {
  .. snip ..
  public function __set($key, $value) {
    $this->_view->addVar($key, $value);
  }
  .. snip ..
}

      



The view uses extract()

(or another suitable approach such as variable variables) to create scope variables from these values:

class View {
  private $_vars = array();
  private $_templatePath;
  public function __construct($templatePath) {
    $this->_templatePath = $templatePath;
  }
  public function addVar($key, $value) {
    $this->_vars[$key] = $value;
  }
  public function render() {
    extract($this->_vars);
    include $this->_templatePath;
  }
}

      

Because of the way PHP handles scope, the template has access to variables created by the view method render()

.

+4


a source







All Articles