Jump to content

Why I love the Latte Template Engine


bernhard
 Share

Recommended Posts

10 hours ago, bernhard said:

It should work just like any other PW api... Does it render empty divs? Does it throw an exception?

I receive the markup but couldn't access any data somehow.

10 hours ago, bernhard said:

I'm never using {include ...}, I'm always using {$rockfrontend->render(...)}, but both should work.

Using $rockfrontend every time wouldn't save me that much time so I tried using it the .latte way.

10 hours ago, zoeck said:

I think you have to pass the variable to your included latte file

This did the trick. Thanks for this hint!

Link to comment
Share on other sites

On 7/19/2022 at 3:37 PM, bernhard said:
<ul>
  <li n:foreach="$page->parents()->add($page) as $item">
    <a href="{$item->url}" n:tag-if="$item->id != $page->id">
      {$item->title}
    </a>
  </li>
</ul>

If it's ok to dig up old threads....

@bernhard here, aren't you double encoding url, title? Assuming this is front end code, with `$pages->of(true)` and you have html entities text formatter in place on your title, url fields then a title with - say - an & would come out looking like &amp; as it would actually be &amp;amp;

 

Link to comment
Share on other sites

20 hours ago, artfulrobot said:

@bernhard here, aren't you double encoding url, title? Assuming this is front end code, with `$pages->of(true)` and you have html entities text formatter in place on your title, url fields then a title with - say - an & would come out looking like &amp; as it would actually be &amp;amp;

I think you are right for the ->title, but not for the ->url. The url is a property of the page object, not a regular field. So there is no textformatter applied and therefore it should just work like shown in the example. If you have a textformatter applied on the title field, then you'd need a |noescape filter in latte, yes.

 

  • Like 1
Link to comment
Share on other sites

  • 4 weeks later...

@bernhard I absolutely love Rockfrontend and latte, it's so intuative!! The only issue I'm having is bracket pair colours in Visual Studio Code. Is there a way to get VSC to highlight the brackets and div pairs in a *.latte file? I'm sure there must be, I can't seem to find anything online. 

Link to comment
Share on other sites

Hey @Greg Lumley great to hear that, thx 🙂 

There is a latte extension for VSCode. It's not perfect, but it's ok:

1dEEuNK.png

Also there is no proper intellisense/code completion in latte files which is a pity, but I try to keep my latte files as clean as possible and put all the logic in the custom page class or in my Site.module.php so latte files are really just some simple markup and calling {$obj->method()} which is something that I can manage without typos most of the time 😄 

Link to comment
Share on other sites

  • 7 months later...

Imagine you want to display an image on your website. Easy.

Imagine you want to open the bigger version in a lightbox. Easy when using UIkit (or any other framework).

Imagine you only want to add the lightbox link only if there is actually a larger version than the one displayed. Easy when using LATTE 🙂 

<div
  n:if="$img = $page->image"
  class="uk-background-muted uk-padding-small uk-text-center"
  uk-lightbox
>
  <a
    n:tag-if="$img->width > 800 || $img->height > 400"
    href="{$img->maxSize(1920,1920)->webp->url}"
  >
    <img src="{$img->size(800,400)->webp->url}" alt="{$page->title}" />
  </a>
</div>

This takes care of so many things with so little and clean code:

  • the n:if on the outer div makes sure that we don't get any errors if no image is uploaded
  • the n:tag-if makes sure that the anchor wrapper is only added if the original image is larger than the thumbnail

Note that you can assign $img directly in the n:if condition. No need for an additional {var $img = $page->image} at the top. For some that might be too short, but I like it 🙂 

  • Like 2
Link to comment
Share on other sites

Have reworked all my projects with Latte and CustomPage classes and the code is more clean, way easier to read and maintain and more structured now. Definitely recommend diving into Latte and CustomPage classes for new projects to anyone. Both techniques were easy to learn and paid off quite soon in my projects.

  • Like 4
Link to comment
Share on other sites

  • 2 weeks later...
On 1/28/2024 at 1:45 PM, cwsoft said:

Have reworked all my projects with Latte and CustomPage classes and the code is more clean, way easier to read and maintain and more structured now. Definitely recommend diving into Latte and CustomPage classes for new projects to anyone. Both techniques were easy to learn and paid off quite soon in my projects.

I want to do this to my projects too. What resources did you use to learn how to do this @cwsoft? Or is any of your code shareable? Thank you!

Link to comment
Share on other sites

@rastographicsYou can find some additional infos in this post here. Meanwhile I used the 2nd approach shown in the linked tutorial with a slightly modified setup (e.g. proper namespaces, use statements, method names etc.) to implement Latte template engine in all my new projects so far. 

For Latte I would go with official Latte documentation linked above. In addition I would read the Custom Page Classes tutorial linked and watch Bernhards video.

Then I would start off with a first project and use normal PW template files (like a controller), loading the required latte template (view) and implement my page specific methods via custom page classes. Nice part is, that you can call the specific page methods via {$page->myAwesomeMethod()} in Latte view too. 

  • Like 1
Link to comment
Share on other sites

Thanks @cwsoft and @zoeck. I've used custom page classes for a while but I don't think to full potential so I'm sure these resources will get me to the next level of what is possible. 

My reason for coming to this thread is I'm trying to have a more elegant way of using htmx by using "partials" on the php side, and I read that latte will let me break the html up into partials better than the regular php templates. Excited to hopefully use latte to make the htmx experience better. And just overall structure my processwire projects better.

  • Like 2
Link to comment
Share on other sites

On 2/12/2024 at 5:06 PM, rastographics said:

Thanks @cwsoft and @zoeck. I've used custom page classes for a while but I don't think to full potential so I'm sure these resources will get me to the next level of what is possible. 

My reason for coming to this thread is I'm trying to have a more elegant way of using htmx by using "partials" on the php side, and I read that latte will let me break the html up into partials better than the regular php templates. Excited to hopefully use latte to make the htmx experience better. And just overall structure my processwire projects better.

Inspired by this reading? https://htmx.org/essays/template-fragments/#known-template-fragment-implementations

  • Like 2
Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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