0

I want to call the function of from the controller in twig.How can i do it

It is my controller

class ArticleController extends Controller
{
   /**
    * @Route("Article")
    */
   public function indexAction()
   {
       ....................... 
       return $this->render('MainBundle:Article:index.html.twig', array(
               'lastArticleCategoryData' => $lastArticleCategoryData
            ));
   }
   public function datajalali()
   {
       $articles = "sss";
       $v = new Verta(); //1396-02-02 15:32:08
       $v = Verta::now();

       return $v;
   }
}

My twig

{% if Article.datajalali %}
   {{ datajalali }}
{% endif %}

3 Answers 3

4

You, normally, can't call PHP functions in twig directly.

You can however, write a Twig extension

http://symfony.com/doc/current/templating/twig_extension.html

In your case it should look something like this

// src/Twig/AppExtension.php
namespace App\Twig;

use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;

class AppExtension extends AbstractExtension
{
    public function datajalali()
    {
        $articles = "sss";
        $v = new Verta(); //1396-02-02 15:32:08
        $v = Verta::now();

        return $v;
    }

}

With that said, I suppose you could do something like

{{ render(controller(
    'AppBundle\\Controller\\ArticleController ::datajalali'
)) }}

But that seems like bad practice to me, to be honest. I'm not entirely sure it will work properly either.

Sign up to request clarification or add additional context in comments.

3 Comments

To be complete, he could also use closures as described in this post I wrote a while ago. That being said, I agree, a Twig extension should be the first option to be considered.
Thank you.i have error::-> An exception has been thrown during the rendering of a template ("Class "MainBundle\Controller\ArticleController " does not exist.").
Well, there you go, I told you it may not work. I haven't tested it. Go for the extension, seems a lot better.
1

Just try to send your datajalali to template, like this:

    return $this->render('MainBundle:Article:index.html.twig', array(
           'lastArticleCategoryData' => $lastArticleCategoryData,
           'datajalali' => $this->datajalali(),
        ));

See How to Embed Controllers in a Template

UPDATE

Andrew and Jonathan Jalouzot told you about twig extensions, but this way is different for symfony3 and symfony4. In your case it looks like this:

class AppExtension extends \Twig_Extension
{
    public function getFunctions()
    {
        return array(new \Twig_Function('datajalali', array($this, 'datajalali')));
    }

    public function datajalali()
    {
        // Your logic here
    }
}

And use it in your template:

{{ datajalali() }}

See How to Write a custom Twig Extension

7 Comments

Hey.thank you for answer.your code work fine but i want to send data to function.i up vote your answer
What's wrong with sending data to function in this case?
i can send parameter in function with this method..for example::: {% datajalali($date) %}
U can do it in your controller 'datajalali' => $this->datajalali( $date )
In my database, the date of each article is gregorian data.I want to when i print article with for loop.each data print date jalali for this i have class .And then i try convert data at the time of the show.
|
0

You need to follow http://symfony.com/doc/current/templating/twig_extension.html

You need to created extension twig.

namespace App\Twig;

use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;
use Verta;

class AppExtension extends AbstractExtension
{
    public function getFunctions()
    {
        return array(
            new TwigFunction('datajalali', array($this, 'datajalali')),
        );
    }

    public function datajalali()
    {
        $articles = "sss";
        $v = new Verta(); //1396-02-02 15:32:08
        $v = Verta::now();

        return $v;
    }
}

You need after add service with tag twig.extension exemple

//service.yml
App\Twig\AppExtension:
    tags: ['twig.extension']

And you can use in your twig template

//twig
{{ datajalali() }}

1 Comment

Hey.thank you for time out.when i use your code i gave this error.:::-----1/1) RuntimeException The definition for "tags" has no class. If you intend to inject this service dynamically at runtime, please mark it as synthetic=true. If this is an abstract definition solely used by child definitions, please add abstract=true, otherwise specify a class to get rid of this error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.