1

I keep rereading the symfony 4 documentation to try to generate twig template with the console commands but I did not find a command . Is there any one know a bundle to generate twig template with console commands ?

3
  • I'm not at home now so I cannot check, but first install maker (composer require maker --dev) then check if there is a command for view (bin/console list make). If there is, it is there :) Commented Jan 19, 2018 at 12:14
  • Why the console would be usefull to generate a blank file template.html.twig ? (it's not a troll, i'm really curious) Commented Jan 19, 2018 at 15:59
  • @goto not blank, with some code already in it. CakePHP for example can generate a full CRUD with one console command based on a database table. So I'm guessing something like that was what the OP wanted. Commented Jan 31, 2018 at 9:37

4 Answers 4

2

I'm solved

 class SendEmailNotify extends Command
{
    private $container;
    private $twig;
    private $mailer;

    public function __construct($name = null, \Psr\Container\ContainerInterface $container, \Swift_Mailer $mailer)
    {
        parent::__construct($name);
        $this->container = $container;
        $this->twig = $this->container->get('twig');
        $this->mailer = $mailer;
    }
Sign up to request clarification or add additional context in comments.

Comments

1

I checked it and the sad answer is no. The list of maker commands is available here. You can even make a twig extension, but not a view. It is worth submitting to them I think.

2 Comments

can you explain me what is the twig extension exactly ?
Sure, it is an additional filter or function for twig, like for instance {{ variable | length }} (the lenght part). So sadly nothing that would help in your case.
0

I needed Twig functionality to send emails from my custom console command.

This is the solution I came up with.

First I installed Twig.

composer require "twig/twig:^2.0"

Then created my own twig service.

<?php

# src/Service/Twig.php

namespace App\Service;

use Symfony\Component\HttpKernel\KernelInterface;

class Twig extends \Twig_Environment {

    public function __construct(KernelInterface $kernel) {
        $loader = new \Twig_Loader_Filesystem($kernel->getProjectDir());

        parent::__construct($loader);
    }
}

Now my email command looks like this.

<?php

# src/Command/EmailCommand.php

namespace App\Command;

use Symfony\Component\Console\Command\Command,
    Symfony\Component\Console\Input\InputInterface,
    Symfony\Component\Console\Output\OutputInterface,
    App\Service\Twig;

class EmailCommand extends Command {

    protected static $defaultName = 'mybot:email';

    private $mailer,
            $twig;

    public function __construct(\Swift_Mailer $mailer, Twig $twig) {
        $this->mailer = $mailer;
        $this->twig = $twig;

        parent::__construct();
    }

    protected function configure() {
        $this->setDescription('Email bot.');
    }

    protected function execute(InputInterface $input, OutputInterface $output) {

        $template = $this->twig->load('templates/email.html.twig');

        $message = (new \Swift_Message('Hello Email'))
            ->setFrom('[email protected]')
            ->setTo('[email protected]')
            ->setBody(
                $template->render(['name' => 'Fabien']),
                'text/html'
            );

        $this->mailer->send($message);
    }
}

Comments

0

This instruction will make a Controller without a twig template

php bin/console make:controller --no-template  

Surprisingly this instruction will make a Controller and a template file and subdirectory

php bin/console make:controller 

Comments

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.