THELIA Forum

Welcome to the THELIA support and discusssion forum

Announcement

Rejoignez la communauté sur le Discord Thelia : https://discord.gg/YgwpYEE3y3

Offline


Hi
There is a way to generate the html with smarty engine from a command? Like what we do with the method renderRaw() in controllers.
Thanks.

Offline


You can try to write a Command in a module which generates output from a template (see Command documentation here: http://doc.thelia.net/en/documentation/ … mands.html).

First, you have to pass a TemplateHelper (container service thelia.template_helper) and a TheliaParser (container service thelia.parser) to your command:

public function __construct(TemplateHelperInterface $templateHelper, ParserInterface $parser)
{
    $this->templateHelper = $templateHelper;
    $this->parser =$parser;
}

Then you can write :

$this->parser->setTemplateDefinition($this->templateHelper->getActiveFrontTemplate());

$data = $this->getParser($templateDir)->render($templateName, $args);

OpenStudio Toulouse

Offline


And how can i pass the 2 interfaces? Beacuase now it gives me this:

[Symfony\Component\Debug\Exception\ContextErrorException]                                                            
Catchable Fatal Error: Argument 1 passed to WareHouse\Commands\CreatePdfForWareHouse::__construct() must be an instance of Thelia\Core\Template\TemplateHelperInterface, none given

Offline


I did not realized that we can't pass arguments to command constructors from the module's config.xml. You can use the container instead :

public function __construct()
{
    $this->templateHelper = $this->getContainer()->get('thelia.template_helper');
    $this->parser = $this->getContainer()->get('thelia.parser');
}

OpenStudio Toulouse

Offline


Now i get this

Fatal error: Call to a member function getKernel() on null in /opt/lampp/htdocs/eglaan/core/lib/Thelia/Command/ContainerAwareCommand.php on line 51

Call Stack:
    0.0001     227280   1. {main}() /opt/lampp/htdocs/eglaan/Thelia:0
    0.0005     240864   2. include('/opt/lampp/htdocs/eglaan/core/Thelia') /opt/lampp/htdocs/eglaan/Thelia:5
    0.1530   14834656   3. Symfony\Component\Console\Application->run() /opt/lampp/htdocs/eglaan/core/Thelia:54
    0.1531   14835656   4. Thelia\Core\Application->doRun() /opt/lampp/htdocs/eglaan/core/vendor/symfony/console/Application.php:124
    0.1531   14835792   5. Thelia\Core\Application->registerCommands() /opt/lampp/htdocs/eglaan/core/lib/Thelia/Core/Application.php:55
    0.1719   18282784   6. ReflectionClass->newInstance() /opt/lampp/htdocs/eglaan/core/lib/Thelia/Core/Application.php:72
    0.1719   18283096   7. WareHouse\Commands\CreatePdfForWareHouse->__construct() /opt/lampp/htdocs/eglaan/core/lib/Thelia/Core/Application.php:72
    0.1720   18284928   8. Thelia\Command\ContainerAwareCommand->getContainer() /opt/lampp/htdocs/eglaan/local/modules/WareHouse/Commands/CreatePdfForWareHouse.php:24

Offline


Okay, delete the constructor, and use directly $this->getContainer()->get(...)


OpenStudio Toulouse

Offline


Another error

Fatal error: Call to a member function getSession() on null in /opt/lampp/htdocs/eglaan/core/lib/Thelia/Core/Template/ParserContext.php on line 289

Call Stack:
    0.0001     227280   1. {main}() /opt/lampp/htdocs/eglaan/Thelia:0
    0.0004     240864   2. include('/opt/lampp/htdocs/eglaan/core/Thelia') /opt/lampp/htdocs/eglaan/Thelia:5
    0.1488   14834656   3. Symfony\Component\Console\Application->run() /opt/lampp/htdocs/eglaan/core/Thelia:54
    0.1489   14835656   4. Thelia\Core\Application->doRun() /opt/lampp/htdocs/eglaan/core/vendor/symfony/console/Application.php:124
    0.1680   18422048   5. Symfony\Component\Console\Application->doRun() /opt/lampp/htdocs/eglaan/core/lib/Thelia/Core/Application.php:61
    0.1681   18423080   6. Symfony\Component\Console\Application->doRunCommand() /opt/lampp/htdocs/eglaan/core/vendor/symfony/console/Application.php:193
    0.1687   18450288   7. Symfony\Component\Console\Command\Command->run() /opt/lampp/htdocs/eglaan/core/vendor/symfony/console/Application.php:866
    0.1689   18452224   8. WareHouse\Commands\CreatePdfForWareHouse->execute() /opt/lampp/htdocs/eglaan/core/vendor/symfony/console/Command/Command.php:259
    0.1868   21917776   9. Symfony\Component\DependencyInjection\Container->get() /opt/lampp/htdocs/eglaan/local/modules/WareHouse/Commands/CreatePdfForWareHouse.php:81
    0.1868   21919600  10. CoreDevDebugProjectContainer->getThelia_ParserService() /opt/lampp/htdocs/eglaan/core/vendor/symfony/dependency-injection/Container.php:314
    0.1931   23121448  11. Symfony\Component\DependencyInjection\Container->get() /opt/lampp/htdocs/eglaan/cache/dev/CoreDevDebugProjectContainer.php:5355
    0.1931   23121768  12. CoreDevDebugProjectContainer->getThelia_Parser_ContextService() /opt/lampp/htdocs/eglaan/core/vendor/symfony/dependency-injection/Container.php:314
    0.1942   23263920  13. Thelia\Core\Template\ParserContext->__construct() /opt/lampp/htdocs/eglaan/cache/dev/CoreDevDebugProjectContainer.php:5431
    0.1942   23264440  14. Thelia\Core\Template\ParserContext->cleanOutdatedFormErrorInformation() /opt/lampp/htdocs/eglaan/core/lib/Thelia/Core/Template/ParserContext.php:59
    0.1942   23264504  15. Thelia\Core\Template\ParserContext->getSession() /opt/lampp/htdocs/eglaan/core/lib/Thelia/Core/Template/ParserContext.php:233

This is the code

$this->getContainer()->get('thelia.parser')->setTemplateDefinition($this->getContainer()->get('thelia.template_helper')->getActivePdfTemplate());
$html = $this->getContainer()->get('thelia.parser')->render("invoice-warehouse-eglaan", ["arg" => "arg"]);

Offline


Okay, there's no session. You can create one usin the following code :

    protected function init()
    {
        $container = $this->getContainer();

        $request = new Request();
        $request->setSession(new Session(new MockArraySessionStorage()));

        /** @var RequestStack $requestStack */
        $requestStack = $container->get('request_stack');
        $requestStack->push($request);
    }

OpenStudio Toulouse

Offline


Ok, now it works.
Thanks.