Jump to content

Using Latte


Greg Lumley
 Share

Recommended Posts

I feel like this could be a stupid question but I'm stumped.

I'd really liked to give latte templating engine a try, but I have no idea how to incorporate it into PW. Particularly from a directory structure point of view. Do I install it in PW templates? Once that's done, what's the next step to get it working in PW? 

I'd really appreciate some help. 

Thank you. 

Link to comment
Share on other sites

9 hours ago, MarkE said:

Have you read this @Greg Lumley?

Hi Mark, yes indeed, in fact I scoured the forum and web before asking. Even on the (Nette) Latte site, it doesn't give you a classic "Hello World" example. 

While Berhard shows using Latte, he does not show the steps in setting up Latte within Processwire to start with. Of course that wasn't what the post was about. 

That being said, I got it working, but I'm sure it's a bit of a hack. I to get it working, had to remove: 

<?php namespace ProcessWire;

My vendor file is loaded into the PW root, I'm calling like this: Obviously this is for testing purposes. 

<?php 

 require '../../vendor/autoload.php';
 $latte = new Latte\Engine;
 $latte->setTempDirectory('latte');
 $parameters['items'] = array('one', 'two', 'three');

 //Get html content
 $parameters['html_content'] = $page->html_content; 


 //Let's pull the images and load them to an array. 
 $images = array();
 foreach($page->images as $item){
   $images[] =  $item->size(600)->url;
 }
 $parameters['images'] =  $images;

 //Let's render final output
 $latte->render('latte/test.latte', $parameters);

?>

And the template "test.latte" 

<div id="content">

{$html_content|noescape}
<hr>
<div n:foreach="$items as $item">{$item}</div>
<img n:foreach="$images as $image" src="{$image}">
</div>

 

Link to comment
Share on other sites

Just now, Greg Lumley said:

While Berhard shows using Latte, he does not show the steps in setting up Latte within Processwire to start with. Of course that wasn't what the post was about. 

But he did mention that he would be releasing a module shortly.....

  • Like 1
Link to comment
Share on other sites

7 minutes ago, MarkE said:

But he did mention that he would be releasing a module shortly.....

Yip! But I had time this weekend and really wanted to play. Sometimes the urge should not be resisted or moved. My attention span is shorter than that of an ant. It was also a good experience to get it to work. I'm not a full time dev to say the least. 🙂 

  • Like 1
Link to comment
Share on other sites

RockFrontend will have a render() method that can render latte files. In my setups the main markup file is PHP and there I just use something like this:

<head>
  <?= $rockfrontend->styles('head')
    ->add('my/theme.less')
  	->add('foo/bar.css')
    ->render()
  ?>
</head>
<body>
  <?= $rockfrontend->render('sections/header.latte') ?>
  <?= $rockfrontend->render('sections/main.latte') ?>
  <?= $rockfrontend->render('sections/footer.latte') ?>
</body>

Then I have empty PHP files for each template so that PW knows that these templates are viewable on the frontend. Ugly, but that's how PW works.

Latte docs show how you can render latte files here: https://latte.nette.org/en/develop

  • Like 1
Link to comment
Share on other sites

I've been using the TemplateLatteReplace module as standard for a long time now. In fact, it's what makes Processwire usable for me, having come to it from MODX. It's no longer under development, but still working well. The only problem I've faced is converting my date & time filters from strftime() formats on upgrading to PHP 8.1. It's easy to install and set up, without having to understand the internal workings of PW or Latte/Nette.

I like Latte because it keeps the HTML well separated from the logic side of templates, while still offering intelligent processing of data within the views where it helps. I found it very easy to convert my MODX templates and to use the HTML of sites built with other generators as the starting point for Latte views. I did look at other templating engines such as Twig, but found it meant more or less learning a new language for each. Latte somehow seems more natural with just knowledge of PHP and HTML.

I look forward to trying out RockFrontend when it comes along!

  • Like 2
Link to comment
Share on other sites

On 7/24/2022 at 1:56 PM, kp52 said:

I've been using the TemplateLatteReplace module as standard for a long time now. In fact, it's what makes Processwire usable for me, having come to it from MODX. It's no longer under development, but still working well. The only problem I've faced is converting my date & time filters from strftime() formats on upgrading to PHP 8.1. It's easy to install and set up, without having to understand the internal workings of PW or Latte/Nette.

I like Latte because it keeps the HTML well separated from the logic side of templates, while still offering intelligent processing of data within the views where it helps. I found it very easy to convert my MODX templates and to use the HTML of sites built with other generators as the starting point for Latte views. I did look at other templating engines such as Twig, but found it meant more or less learning a new language for each. Latte somehow seems more natural with just knowledge of PHP and HTML.

I look forward to trying out RockFrontend when it comes along!

Thank you for putting me on to this, it's something to start with. I really like the neatness of it too! I'm struggling with the concept of blocks a bit... 

Are you by any chance using SEO Maestro? I'm finding I can't call {$page->SEO} in @layout.latte it crashes the server. If I call it in the view page it works. Obvioulsy this is an issue as that output must go in the header. 

Link to comment
Share on other sites

2 hours ago, bernhard said:

Maybe {$page->SEO|noescape} ?

Hi Bernhard, thaks for replying. Tried that, it doesn't even output escaped characters. It crashes the server before that. As I say, it does work if I put it in the view page.  home.php / home.latte

All other fields that I've tested seem to work perfectly in the layout page. Since the page ID is working, I tried this. (A hack I know) 
 

{$pages->get($page->id)->SEO}

Same thing, server crashes. I also check PW logs, nothing to note. My php logs aren't set up, that's the next step. 

Of course, I realise when it's working I will need to use "noescape" 

Link to comment
Share on other sites

I don't use SEO Maestro, but I installed it on a local mirror of a simple site, added a field using it to the basic-page template and put it into the body of the relevant Latte view. Out of memory error! A bit more investigation led to the module's BreadcrumbStructuredData.php, which creates a template named structured_data_breadcrumb.php. That causes a fatal error, as there is no associated Latte view file. The solution is to add "structured_data_breadcrumb" to Ignored Templates in the Template Latte Replace module settings. Having done that, no problem outputting the SEO data in the page, or adding {$page->SEO|noescape} to the head of the @layout file.

  • Like 4
Link to comment
Share on other sites

On 7/30/2022 at 2:17 PM, kp52 said:

I don't use SEO Maestro, but I installed it on a local mirror of a simple site, added a field using it to the basic-page template and put it into the body of the relevant Latte view. Out of memory error! A bit more investigation led to the module's BreadcrumbStructuredData.php, which creates a template named structured_data_breadcrumb.php. That causes a fatal error, as there is no associated Latte view file. The solution is to add "structured_data_breadcrumb" to Ignored Templates in the Template Latte Replace module settings. Having done that, no problem outputting the SEO data in the page, or adding {$page->SEO|noescape} to the head of the @layout file.

Thank you! 🙂 

 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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