How to define Global scope of variable in Twig - global-variables

How do we declare a variable as global in Twig?
The variable is going to be inside multiple blocks in twig template, and every block should be able to use that changed variable.
I have tried doing that, but it retains the value of the variable in which it is declared.

If you need a global variable in twig, call addGlobal() on your \Twig_Environment instance. I'm unfamiliar with FoxyCart and its twig integration, so I cannot help you with the details.

If you're using Drupal 8, then you can add variables into preprocess_pages

Related

How global is global variables in lotusscript IBM Notes 9

In Lotusscript' form (IBM Notes 9) I added global variables and it is working as expected when used in click event on buttons in the form.
But when I added a computed-text element, it can't seem to read the global variable of the form. Which means I cannot use these global variables to configure the computed-text's value and Hide-when properties.
This technique is supposed to simulate the ErrorMsg control in xpage,
but I can't get this to use the form's global variable,
I'm thinking of adding a global form which will contain global variables much like the sessionScope in xpage instead.
Or is there a better solution?
Here's the form's global declaration:
Global variables are only accessible within form's LotusScript code.
Computed text fields use formula language and don't have access to LotusScript's global variables.
Use document fields (items) instead. Those can be accessed by LotusScript code and formula code.

Can we use PHP inbuilt function inside Twig File in Symfony

Hi Can anyone Please let me know can we use php inbuilt function inside a twig file.if not then why.
What is the way then to access php inbuilt function inside a twig file.
Because in a application in 100 of time we need to check many conditions basis of php inbuilt function.I have tried in_array() function to check multiple vaslue selected in a multiple dropdown list but i am getting error Is_array() not defined.
Please help
Thanks
As #DonCallisto Said, There is some PHP equivalent function exists in twig not all. So you cant call a php function from twig template. You may have to use a existing equivalent or need to create one if not exists.
Why?
One of the main reason is SoC. Template is for presentation layer of your application. So twig made available tools(filter,functions, global variables) to do that.
Know the differences
Though you have date function in twig. its not the same date function you have in php. To achieve a similar functionality you may have to use same or different approach in twig then php. for instance you can achieve php's in_array functionality using the twig's Containment Operator
What is the way
Now come to last part of your question:
What is the way then to access php inbuilt function inside a twig file?
I think you already know the short answer from #DonCallisto. You can create your own extension. And define what function you needed. Or if you are crazy enough to access all php builtin function from your template, you can use this Extension. It will allow you to call any php functions by prefixed with php_. for example if you like to call in_array function, then you can call like php_in_array() from your template.
Happy coding!
Twig have some php builtin functions equivalent. For instance in_array() php function is in twig function. Check it out
If you don't find some builtin, you need to write your own twig exstension

Add variable to each twig template

How can I transfer variable to each twig template?
I need transfer rates to each template in my application.
You do that by using global variables.
How to Inject Variables into all Templates (i.e. Global Variables) (I know, searching the docs is hard...)

Is a global variable for a Twig template available inside a Symfony2 controller?

A global variable for a Twig template can be defined inside the config.yml of a Symfony2 application as in the following:
twig:
globals:
var_name: var_value
Hence, in each Twig template the variable can be used as follows:
{{var_name}}
that will display
var_value
Do you know such a way to get the value of a global variable just inside a Symfony2 controller?
There doesn't seem to be a way to grab a particular value. But you can get the full array of globals from the twig service and then grab it's offset.
$twig = $this->container->get('twig');
$globals = $twig->getGlobals();
echo $globals['var_name'];
The right practice is to follow the official tutorial about Twig global variables in the Symfony's Cookbook. In practice, one should define a global variable that can be used in the controllers, e.g.:
; app/config/parameters.ini
[parameters]
my_global_var: HiAll
Then the variable is defined as global for the Twig templates
# app/config/config.yml
twig:
globals:
my_var: %my_global_var%
Hence, {{my_var}} will return HiAll but one should take care of the value once in the parameters.ini file.
So, the answer to the question is no! Or precisely, not in an effective way. MDrollette drawn a possible way!
I wasn't clear if you wanted to access the twig var in the controller, or just access a global var from the config. Here is how to access a global var from the config file...
You can place the value in the parameters section of the config..
parameters:
var_name: some_value
Now you can access it from the controller...
$value = $this->container->getParameter('var_name');
I think you'd better to use bundle configuration or DIC parameters for your value, and then add it to the twig globals (via your bundle extension class, for example), and not trying to do the opposite.

drupal global variable

where is the drupal global variable in ,it's say in developer/global.php.but in drupal install file,i can't find this file. what's the difference of the global variable and the avariable variables in page.tpl.php and node.tpl.php...... where is the declaration of the template avariable variables in.thank you
Variables for template files are declared in template preprocess functions. This page in the Drupal theming guide contains a flowchart describing the flow of Drupal's theme() function. For every template, the variables pass every preprocess function that matches the appropriate naming scheme.
For instance, for page.tpl.php, Drupal will first run template_preprocess() and template_preprocess_page(). Next, if some module contains the function somemodule_preprocess_page(), and/or if your custom contains yourtheme_preprocess_page(), those functions will be run as well. Every preprocess function can alter and add variables for the page.tpl.php template. When all preprocess functions have finished, the variables are passed to page.tpl.php.
There is a file called settings.php which may be what you're looking for.
Alternatively, if you'd like the owners of the site to be able to modify the variables without them having to change the source code, you can create a variable in the admin page of one of your modules, which can then be accessed at any point your application using drupal's variable_get() function.

Resources