Add variable to each twig template - symfony

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

Related

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

How to define Global scope of variable in Twig

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

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.

How do I use theme preprocessor functions for my own templates?

I have several .tpl.php files for nodes, CCK fields, and Views theming. These template files have a lot of logic in them to move things around, strip links, create new links, etc. I understand that this is bad development and not "The Drupal Way".
If I understand correctly, "The Drupal Way" is to use preprocessor functions in your template.php file to manipulate variables and add new variables. A few questions about that:
Is there a naming convention for creating a preprocessor function for a specific theme? For example, if I have a CCK field template called content-field-field_transmission_make_model.tpl, how would I name the preprocessor function?
Can I use template preprocessor functions for node templates, CCK field templates, and Views templates? Do they have different methods of modifying template variables or adding new ones?
For a general overview, you should read up on manipulating variables within preprocess functions.
Concerning the naming convention, this is normally pretty simple, but there is a catch for your current example (see below):
A preprocess functions signature needs to be
[yourModuleName|yourThemeName]_preprocess_[themeFunctionName](&$variables)
so implementing one for the page template within a themes template.php file would result in
themeName_preprocess_page(&$variables)
Most of the time the name of the theme function will be the name of the *.tpl.php file, without the .tpl.php ending and with underscores instead of the hyphens. But there is a catch if the template file gets selected on the base of template suggestions, as the preprocess function can only be implemented for the base name, not for the additional suggestions! (The suggestions for alternate template files are added in preprocess functions themselves.)
Your current example is one of those cases, as content-field-field_transmission_make_model.tpl.php is such a suggestion, with the base name being content-field.tpl.php, and the corresponding theme function being content_field. So you would have to implement a preprocess function named yourThemeName_preprocess_content_field(&$variables), and within that inspect the available entries in the $variables array to check if you are actually called for the 'field_transmission_make_model', and not for a completely different CCK field, e.g.:
function yourThemeName_preprocess_content_field(&$variables) {
// Are we called for the right field?
if ('field_transmission_make_model' == $variables['field_name']) {
// Yes, add/manipulate entries within the variables array
$variables['new_entry'] = 'A useless new variable';
$variables['label'] = 'A useless change of the existing label variable';
}
}
(Note: Untested code, beware of typos)
After this, there should be a new variable $new_entry being available in your template file, and the content of the $label variable should have changed (all top level entries within the $variables array will be turned into separate variables for the template file, named after the array index).
As for your second question, the basic usage of preprocess functions is the same for all template files, but be aware:
Preprocess functions are only available for theme calls that use *.tpl.php files, not for theme functions
The content of the $variables array varies heavily, depending on what gets themed
Other modules might implement the preprocess functions as well, and they will be called one after another, so if you want to change something that gets added by another module, you can only do so if your implementation gets called after that (which will be no problem in your case, as implementations within a theme are called after all implementations within modules - just wanted to mention that there can be many implementations at once)
In order to figure out what our preprocessing function should be named, we need to know what template file or theme function some output comes from, and one great way to do this is by using the theme developer module.
Here is a video which explains it in detail - http://buildamodule.com/video/drupal-theming-essentials-template-files-theme-function-overrides-and-preprocessing-functions-how-to-use-simple-preprocessing-functions

Resources