Volt service access

By reading documentation i found this feauture: Inject Services into a Template. there is written: If a service container (DI) is available for Volt.

How can i make DI available for Volt?



81.2k

DI is already enabled in Volt, you just have to access:

{{ di.get('someService').some }}

Yes, yes, but in documentation you specify access just by name of service. I think, there is a logic: if the variable does not exist, it is checked whether there is such a service. In documentation mistake?



81.2k

No, it's not a mistake, you can access any service by its name:

{{ url.get(['for': 'my-route']) }}
{{ flash.getMessages()|dump }}
{{ session.get("auth") }}

But Volt can also be used in stand-alone mode:

<?php

$compiler = new \Phalcon\Mvc\View\Engine\Volt\Compiler();
echo $compiler->compileString('{{ "hello" }}');

The code below does not have a DI available so you can't access services as variables or access the DI itself.