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 %}
tereško
  • 58,060
  • 25
  • 98
  • 150
pedram shabani
  • 1,654
  • 2
  • 20
  • 30

3 Answers3

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.

Andrei
  • 3,434
  • 5
  • 21
  • 44
  • 1
    To be complete, he could also use closures as described in this [post I wrote a while ago](https://stackoverflow.com/questions/34944887/executing-closure-on-twig/47957534#47957534). That being said, I agree, a Twig extension should be the first option to be considered. – Alan T. Feb 07 '18 at 09:28
  • Thank you.i have error::-> An exception has been thrown during the rendering of a template ("Class "MainBundle\Controller\ArticleController " does not exist."). – pedram shabani Feb 07 '18 at 09:38
  • Well, there you go, I told you it may not work. I haven't tested it. Go for the extension, seems a lot better. – Andrei Feb 07 '18 at 09:39
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

Snegirekk
  • 724
  • 7
  • 12
  • Hey.thank you for answer.your code work fine but i want to send data to function.i up vote your answer – pedram shabani Feb 07 '18 at 16:00
  • What's wrong with sending data to function in this case? – Snegirekk Feb 07 '18 at 16:17
  • i can send parameter in function with this method..for example::: {% datajalali($date) %} – pedram shabani Feb 07 '18 at 16:21
  • U can do it in your controller `'datajalali' => $this->datajalali( $date )` – Snegirekk Feb 07 '18 at 16:23
  • 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. – pedram shabani Feb 07 '18 at 16:35
  • For example something like this ::-> {% for article in articleIddata %} {{ datajalali( article.creatAt.date) }} {% endfor %} – pedram shabani Feb 07 '18 at 16:38
  • U can place all your logic in controller. When u get your articles in array u can manipulate them and then send to template. Anyway, my answer was updated with twig extension case for your symfony version. – Snegirekk Feb 07 '18 at 17:36
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() }}
  • 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. – pedram shabani Feb 07 '18 at 16:03