user2794692
user2794692

Reputation: 391

Global variable in symfony2 to use in php template

I need to define a global variable in symfony2 and then, use it in several php template.

I found in documentation how to use in twig templates, but I need how to define and use in php template.

Thanks.

Upvotes: 2

Views: 1024

Answers (2)

Adam Chysky
Adam Chysky

Reputation: 386

You have to define the variable in your config file, for example:

# app/config/config.yml
parameters:
      foo: "bar"

and then you can access it from your PHP templates:

<?php echo $view->container->parameters['foo']; ?>

Upvotes: 2

Victor Bocharsky
Victor Bocharsky

Reputation: 12306

You can define global variable in twig template like:

# app/config/config.yml
twig:
    # ...
    globals:
        ga_tracking: UA-xxxxx-x

Or it in PHP template like:

// app/config/config.php
$container->loadFromExtension('twig', array(
     // ...
     'globals' => array(
         'ga_tracking' => 'UA-xxxxx-x',
     ),
));

And then use it in twig:

<p>The google tracking code is: {{ ga_tracking }}</p>

or in PHP template:

<p>The google tracking code is: <?php echo $ga_tracking; ?></p>

Upvotes: 2

Related Questions