Its not possible to access any PHP function inside Twig directly.
What you can do is write a Twig extension. A common structure is, writing a service with some utility functions, write a Twig extension as bridge to access the service from twig. The Twig extension will use the service and your controller can use the service too.
Take a look: http://symfony.com/doc/current/cookbook/templating/twig_extension.html
Cheers.
There is already a Twig extension that lets you call PHP functions form your Twig templates like:
Hi, I am unique: {{ uniqid() }}.
And {{ floor(7.7) }} is floor of 7.7.
See official extension repository.
I am surprised the code answer is not posted already, it's a one liner.
You could just{{ categeory_id | getVariations }}
It's a one-liner:
Twig2:
$twig->addFilter('getVariations', new Twig_Filter_Function('getVariations'));
Twig 3:
$this->twig->addFilter(new \Twig\TwigFilter('getVariations','getVariations'));
Twig 3 but as function instead of a filter:
$this->twig->addFunction(new \Twig\TwigFunction('getVariantsFunc', 'getVariations'));
You can check your all defined function by
$arr = get_defined_functions();
print_r($arr);
this will give you array of all functions in if your function exist in it you can use it like:
{{ user.myfunction({{parameter}}) }}
I know this is an old thread. But with symfony 3.3 I did this:
{{ render(controller(
'AppBundle\\Controller\\yourController::yourAction'
)) }}
While I agree with the comments about passing in variables from your controller you can also register undefined functions when setting up the twig environment
$twig->registerUndefinedFunctionCallback(function ($name) {
// security
$allowed = false;
switch ($name) {
// example of calling a wordpress function
case 'get_admin_page_title':
$allowed = true;
break;
}
if ($allowed && function_exists($name)) {
return new Twig_Function_Function($name);
}
return false;
});
This is from the Twig recipe page
Haven't tried calling a function on an object as the original question requested
This worked for me (using Slim Twig-View):
$twig->getEnvironment()->addFilter(
new \Twig_Filter('md5', function($arg){ return md5($arg); })
);
This creates a new filter namedmd5
which returns the MD5 checksum of the argument.
If you really know what you do and you don't mind the evil ways, this is the only additional Twig extension you'll ever need:
function evilEvalPhp($eval, $args = null)
{
$result = null;
eval($eval);
return $result;
}
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
Symfony compares favorably with other PHP frameworks in terms of reliability and maturity. This framework appeared a long time ago, in 2005, that is, it has existed much longer than most of the other tools we are considering. It is popular for its web standards compliance and PHP design patterns.
https://symfony.com/
Slim is, as its name suggests, a micro-framework that is compact and fast. The reason for its such characteristics lies in the fact that it is completely independent of third-party code. It was created in 2010, at the moment its most recent version is 4.5.0.
https://www.slimframework.com/
HTML (English "hyper text markup language" - hypertext markup language) is a special markup language that is used to create sites on the Internet.
Browsers understand html perfectly and can interpret it in an understandable way. In general, any page on the site is html-code, which the browser translates into a user-friendly form. By the way, the code of any page is available to everyone.
https://www.w3.org/html/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.