Use namespaced class in Latte

3 years ago

amik
Member | 124
+
+2
-

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.

3 years ago

Michal Vyšinský
Member | 614
+
0
-

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)

3 years ago

Michal Hlávka
Member | 166
+
0
-

{\App\Foo\Bar\BazEnum::BAZ_ONE}

3 years ago

amik
Member | 124
+
0
-

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.

3 years ago

Pavel Kravčík
Member | 944
+
-1
-

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)

3 years ago

amik
Member | 124
+
0
-

@PavelKravčík yes this obviously works, but with long expressive namespaces it makes a mess in your template.
@PavelKravčík yes, this also works, but still it is a workaround needing to add extra stuff into Presenter…
So, there is no native way?

3 years ago

hrach
Member | 1801
+
0
-

@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)

3 years ago

hrach
Member | 1801
+
0
-

Anyone!, feel free to pick the PR and rebase. :)

3 years ago

amik
Member | 124
+
+2
-

@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.

3 years ago

Pavel Kravčík
Member | 944
+
+1
-

@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”.

3 years ago

Michal Vyšinský
Member | 614
+
0
-

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)

3 years ago

amik
Member | 124
+
0
-

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 with Some\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.

3 years ago

Michal Vyšinský
Member | 614
+
0
-

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)