Use namespaced class in Latte
- amik
- Member | 118
Hi,
is there currently any way to use PHP class in a latte template?
the {use} macro imports new macros and I haven't found any other way.
I want to do something like:
{useClass App\Foo\Bar\BazEnum}
...
<div n:if="$currentBaz === BazEnum::BAZ_ONE">
Welcome to Baz One.
</div>
If this is not currently supported, do you consider it useful so that I'll maybe create pull request for it?
Thanks.
- Michal Vyšinský
- Member | 608
Hi,
you can use {php} macro:
{php use App\Foo\Bar\BazEnum}
Disclaimer: I've not been working with Latte for a long time but I hope it still works :)
Last edited by Michal Vyšinský (2016-03-17 11:46)
- amik
- Member | 118
Michal Vyšinský wrote:
Hi,
you can use {php} macro:{php use App\Foo\Bar\BazEnum}
Disclaimer: I've not been working with Latte for a long time but I hope it still works :)
Well, no, because compiled Latte template is a class and all content (including prolog and epilog) is placed inside render() method. Rendered template looks like this (:
<?php
// source: /Users/richard/www/some/template.latte
class Template97f58933c6d409c1190406bf7eb23a28 extends Latte\Template {
function render() {
foreach ($this->params as $__k => $__v) $$__k = $__v; unset($__k, $__v);
// prolog Latte\Macros\CoreMacros
list($_b, $_g, $_l) = $template->initialize('86673602b1', 'html')
;
//
// main template
//
use App\Foo\Bar\BazEnum; //OBVIOUSLY WRONG
?>
<h1>Hello world</h1>
<?php
}}
anyway, {php} macro does not exist any more, it would be {? … } now, but still not usable for use statements because of the reason above.
- Pavel Kravčík
- Member | 1196
In case when working with class without dependindecies (entities). But I prefer @emptywall's solutions.
// Presenter
use \Very\Far\Far\Place\FooClass;
$this->template->fooClass = new FooClass;
// Template
{if $bar == $fooClass::BAZ_ONE}
Last edited by Pavel Kravčík (2016-03-17 12:28)
- hrach
- Member | 1838
@MichalVyšinský you can't, see my two year old PR.
https://github.com/…/issues/1121
But this is insufficient because it gets rendered after snippet blocks. If you need that class in snippet, you are out.
Last edited by hrach (2016-03-17 13:49)
- amik
- Member | 118
@hrach thanks, your last comment on the issue led me to idea similar like @PavelKravčík 's solution :)
{var $BazEnum = \Fully\Qualified\Name\Of\Baz\Enum::class} {* or pass as a string in PHP<=5.4 *}
{$BazEnum::BAZ_ONE}
That's it. Still kinda hack, but at least it does not pollute presenters.
- Pavel Kravčík
- Member | 1196
@amik: Well, this is very close to perfect. My “solution” was ugly, I mostly use “full namespace way”. But your hack seems better than “full namespace way”.
- Michal Vyšinský
- Member | 608
And what about making if ‘fake’? So basically modify template in compile time?
{useClass Some\Foo\Bar}
{Bar::SOME_CONSTANT}
Before compliling to php code it could replace Bar
with
Some\Foo\Bar
. So you would have comfort of using short class name
and Latte would be happy too.
Last edited by Michal Vyšinský (2016-03-17 15:02)
- amik
- Member | 118
Michal Vyšinský wrote:
And what about making if ‘fake’? So basically modify template in compile time?
{useClass Some\Foo\Bar} {Bar::SOME_CONSTANT}
Before compliling to php code it could replace
Bar
withSome\Foo\Bar
. So you would have comfort of using short class name and Latte would be happy too.
How? What you've written here will not work, my original point was that there is no macro like useClass.
- Michal Vyšinský
- Member | 608
Yeah, if I understand correctly there is no way to use real ‘use’ in Latte because template becomes a class. What I wrote is just an idea how it could be implemented.
Last edited by Michal Vyšinský (2016-03-17 17:22)