当前位置: 动力学知识库 > 问答 > 编程问答 >

symfony2 - 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

分享给朋友:
您可能感兴趣的文章:
随机阅读: