简体   繁体   English

symfony2中的全局变量可在php模板中使用

[英]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. 我需要在symfony2中定义一个全局变量,然后在几个php模板中使用它。

I found in documentation how to use in twig templates, but I need how to define and use in php template. 我在文档中找到了如何在树枝模板中使用,但是我需要如何在php模板中定义和使用。

Thanks. 谢谢。

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: 或者在PHP模板中如下所示:

// 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: 或在PHP模板中:

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

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模板访问它:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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