Can we use PHP inbuilt function inside Twig File in Symfony - 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

Related

Is it possible to use queryContent from a custom module with #nuxt/content v2?

I am creating a custom module to create some hook code that runs at 'build:done' lifecycle. I've tried this with a raw hook and with my current custom module, but neither makes the queryContent function available (it's always undefined), which I need to parse markdown files. Is there no way of getting the queryContent from a module or hook?
I also tried direct importing queryContent it but it is not actually exported so I get export errors. Also tried grabbing $content but it's not available on v2.

Wordpress: Use set_query_var in function multiple times on a post?

In my WordPress theme, one of my short codes calls a function that uses set_query_var. I don't understand the scope. Is it ok to use that short code more than once in a post? Is that use of set_query_var local to the short code? Because it passes to other theme parts, I think it must be global. Example:
function my_func ($atts) {
var $the_amount = $atts['amount']; //passed from the short code
set_query_var('my_amount', $the_amount);
}
Using the short code that calls this function more than once in a post seems to work ok, but I don't know if this is acceptable.
No, set_query_var is not local. The value is set globally.
Yes, it is allowed to use it multiple times on the same variable - however note that it will reset the value each time, so make sure you are finished with the previous value before resetting it.
But as long as your shortcode is well written, it should be ok.
Note that the reason it is used (even though passing information through global variables are bad practice) is because it is currently the only way to pass variables to other template parts. However the next release of WP (v5.5) will at last offer a way to pass variables between template files, and once that version is widely used that is the better, cleaner way to pass information between template files.
Passing arguments to template files in WordPress 5.5

Undefined helpers fail silently

When i use something that is undefined in my templates I'd like to have it log an error instead of failing silently. is there a way?
I just spent 20 minutes trying to figure out why my helper wasn't being called and it was defined on the wrong template.
I use global helpers instead. Templates are accessible from the command line so that's why I end up doing this anyway. If I need custom helpers for a template I define them with Iron-Router on the data object of the route.
Also, to ease this behavior you could make a helper for your app (as in App.helper.return), containing, say _.isUndefined(helperReturnValue), to throw an error. But then of course, this is not that convenient for production unless you, somehow, set client-side the process.env.node_env variable of your server.

trying to use Symfony component and having name space issues

So i am starting a new project and want to use some of the Symfony Components. I have not used name spaces before with PHP, put i am familiar with the concept from my work in java.
i have this simple piece of code and when i run it i get the error:
PHP Fatal error: Class 'Symfony\Component\CssSelector\XPath\Translator' not found in /home/me/scrapes/Symfony/Component/CssSelector/CssSelector/CssSelector.php on line 52
I am thinking it my lack of knowledge of the name space thing.
/home/me/scrapes/Symfony/Component/CssSelector/CssSelector/XPath/Translator.php does exist.
<?php
set_include_path('/home/me/html/inc');
require 'functions.php';
require 'Symfony/Component/DomCrawler/Crawler/Crawler.php';
require 'Symfony/Component/CssSelector/CssSelector/CssSelector.php';
use Symfony\Component\DomCrawler\Crawler;
use Symfony\Component\CssSelector\CssSelector;
$crawler = new Crawler();
$crawler->addContent('<html><body><p>Hello World!</p></body></html>');
print $crawler->filter('body > p')->text();
curl_close($ch);
require 'cleanup.php';
?>
thanks for any help
I think this is caused because, if you look in the files you required (for example Symfony/Component/DomCrawler/Crawler/Crawler.php) you'll see that those files use their own use statements (to load other classes).
Namespaces
Let's start with the namespaces. A namespace is used to easily create classes with the same name in different packages. Let's say I have a package called Foo and a package called Bar. Both packages contain a Client class that is used to do some client work (one to call the Google Maps API for example and the other to call the Facebook Graph API). Let's also assume neither of the packages uses namespaces.
If I execute the following code:
<?php
require 'Foo/Client.php';
require 'Bar/Client.php';
This is not going to work, because both packages declare a Client class. Oops, how is PHP going to know which Client class to use if you do this?
<?php
$client = new Client();
It's not going to know which Client to use, so it gives up and throws an error.
If you use namespaces (declared using the namespace keyword in PHP at the top of your file, directly below <?php) you can prevent this from happening. The Foo package can create a Client class in the Foo namespace and the Bar package in the Bar namespace. Now we can
actually use both files and create a client:
<?php
require 'Foo/Client.php'
require 'Bar/Client.php'
$fooClient = new Foo\Client();
$barClient = new Bar\Client();
This will work fine.
I think you might have encountered the Foo_Client notation in older PHP libraries. This is an old way to create namespaces before PHP natively supported them.
"But", I hear you say, "it's quite cumbersome to write Foo\Bar\Baz\Client() every time I want to instantiated a class".
It is, and that's where the use keyword comes in. When using the use keyword, I can tell PHP I want to use a specific client and just use the class name, like so:
<?php
require 'Foo/Bar/Baz/Client.php'
use Foo\Bar\Baz\Client;
$client = new Client();
This will work, if you use the use Foo\Bar\Baz\Client statement, because you tell PHP "Okay, I want to use the Client class from the Foo\Bar\Baz namespace to be used when I use the Client class.
Autoloading
Now, what happens if you use a lot of different classes and you seperated them into several files (which you should do). You get a lot of different require and use statements on the top of a file. That's where autoloading comes in.
There has been a spl_register_autoloader function for quite some time in PHP. This function is used by PHP to find out which files to use when you instantiate a class that is not known because you did not require the file. This function is used both when creating a class, or, and this is the key part when you use a class.
And that's what's happening in your code. You don't have an autoloader registered that can translate the use statements in the files you required to actual class declarations.
Great, how do I fix it?
To fix it, I suggest you read up on the PHP-FIG and PSR-4. These people created standards (which you can follow but are not obliged to). To create easy to use libraries, such as the Symfony component. After you've done that, read up on Composer. After you've done this, you can drop the require statements from your code and use Composer to autoload all the classes you need.

module_invoke_all not returning all modules

I'm creating a module wich provides a separate node overview page for each content type.
My problem lies in trying to recreate the node operations dropdown.
In the node module this is done by calling the module_invoke_all function with the 'node_operations' hook.
This returns an array of all modules that implement the 'node_operations' hook.
In my case the following two modules: 'node' and 'nodewords'.
When I call module_invoke_all('node_operations') in my module, it returns only the 'nodewords' module, not the 'node' module.
This is because the 'node_node_operations' function does not exist.
Can anyone explain this behavior?
It looks like the hook is in node.admin.inc, which is not automatically included. See http://api.drupal.org/api/drupal/modules--node--node.admin.inc/function/node_node_operations/7
This is imho a bug, you should look if there already is an issue and if not, create a new one.
Anyway, as a workaround, you can include the node.admin.inc file like this before calling the hook:
<?php
module_load_include('inc', 'node', 'node.admin');
?>
(Yeah, weird syntax ;))

Resources