Jump to content

Why I love the Latte Template Engine


bernhard
 Share

Recommended Posts

Thanks to all three of you! Learned something new again.

I'm on PHP8 and because the $page object should be existent on every page, this should be sufficient and works great:

<body n:class="$page->category?->value">

👏🙏

  • Like 1
Link to comment
Share on other sites

Just now, sebibu said:

Thanks to all three of you! Learned something new again.

Me too, thx 😄 I have to get used to these new nullsafe operators 🙂 

Link to comment
Share on other sites

Directly another question.😇

To save the editor the trouble of entering a dropdown field, I would like to determine the value of a dropdown field in the next (and probably only) pagetree-upper page having a template with template-id e.g. = 50.

Is this also possible only with Latte or do I have to determine the value in PHP first?😅

Thx!👋

Link to comment
Share on other sites

I don't understand the question and it's probably a bit offtopic for a "why I love latte" thread 😉 

But you can use regular PW API inside Latte. That's why latte is so great, it parses templates to regular PHP so you can do anything that PHP can do. Though this should be used with caution, as you should also try to keep your latte files clean, so whenever you need more complex logic use custom page classes and put the logic there in a custom method, so you can do {$page->my_custom_dropdown_value()} for example.

If it's simple you can do {$page->parent->dropdown_value} instead.

  • Thanks 1
Link to comment
Share on other sites

  • 2 weeks later...

I do love Latte, but one little thing is puzzling me at the moment: namespaces. I am using custom page classes for all my 'business logic' - e.g a method getEmailHash() in the custom page class called in the Latte script with $page->getEmailHash(). The class has the namespace ProcessWire. It seems that I always need to specify the namespace in the method  - i.e. $page->ProcessWire\getEmailHash().

Is there a way of avoiding this?

Link to comment
Share on other sites

If you have a look at the RockFrontend Site Profile you'll see that it should work without namespaces:

https://github.com/baumrock/site-rockfrontend/blob/3c6dcc3d6a73432f4a98f5d47bec9858fa1fbd23/templates/sections/footer.latte#L17

https://github.com/baumrock/site-rockfrontend/blob/3c6dcc3d6a73432f4a98f5d47bec9858fa1fbd23/classes/HomePage.php#L27

I have no idea why the syntax you mention would work. I've never seen adding a namespace on the method call of an object 😮 

  • Like 1
Link to comment
Share on other sites

3 hours ago, bernhard said:

If you have a look at the RockFrontend Site Profile you'll see that it should work without namespaces:

It works for me too in a similar context - i.e. when calling it to output: i.e in {$page->foo()} - but not when called in a variable declaration - ie. {var $foo = $page->foo()} That's when I seem to need {var $foo = $page->ProcessWire\foo()}. Odd.

EDIT - Correction: The issue occurs not with page classes but just with functions defined in init.php (which is in the ProcessWire namespace.

Edited by MarkE
Correction
Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...