[Symfony 2] Twig – Global Variables
Schon mal gefragt, welche globalen Variablen in der Twig-Extension eines Symfony 2 (Standard Distri)-Projektes zur Verfügung stehen?
Schaut man sich die Klasse GlobalVariables im Namespace Symfony\Bundle\FrameworkBundle\Templating an (diese Klasse ist Teil des Framework-Bundles, d.h., Gleiches gilt ebenfalls für PHP als Template-Maschine), wird einiges klarer. Weiter unten eine Liste der Assessoren, die die Klasse bereitstellt:
Symfony\Component\Security\Core\SecurityContext|void getSecurity()
Symfony\Component\Security\Core\User\(Advanced)UserInterface getUser()
Symfony\Component\HttpFoundation\Request|void getRequest()
Symfony\Component\HttpFoundation\Session|void getSession()
string getEnvironment()
bool getDebug()
In Twig liegt der Container im “app”-Namespace, “Übersetzt” nach Twig heißt das ganze also:
app.security
app.user
app.request
app.session
app.environment
app.debug
Viel Spaß beim Templates-Bauen ;)
[...] This is a summary of global twig variables and its corresponding classes. Tweet This entry was posted in Development, symfony by Luis Cordova. Bookmark the permalink. [...]
why did you write the title in English, and the article in some other language?
The master of the internet told me to do so, but never reported back since then :(
Perhaps i decide to translate the article in the near future, in the meantime i suggest to investigate the contents of the class Symfony\Bundle\FrameworkBundle\Templating\GlobalVariables which should make things clear.
In the time you commented back to the first response, you could have translated this post :(
Thanks for pointing out the GlobalVariables.php file though!
Once you find the GlobalVariables.php all you have to do is make functions inside that file (with the same naming conventions) and Symfony will Automagically allow the use of those functions inside of Twig.
Example:
public function getHttp()
{
if ($request = $this->getRequest()) {
return $request->getHttpHost();
}
}
use {{ app.http }} in your Twig