sebibu Posted April 4 Share Posted April 4 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"> ?? 1 Link to comment Share on other sites More sharing options...
bernhard Posted April 4 Author Share Posted April 4 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 More sharing options...
sebibu Posted April 4 Share Posted April 4 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 More sharing options...
bernhard Posted April 4 Author Share Posted April 4 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. 1 Link to comment Share on other sites More sharing options...
MarkE Posted April 18 Share Posted April 18 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 More sharing options...
bernhard Posted April 18 Author Share Posted April 18 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 ? 1 Link to comment Share on other sites More sharing options...
MarkE Posted April 18 Share Posted April 18 (edited) 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 April 18 by MarkE Correction Link to comment Share on other sites More sharing options...
nurkka Posted July 25 Share Posted July 25 I am using latte (not with RockFrontend yet, but "standalone" - I can't integrate RockFrontend in my current project yet, but hopefully in the next one), and I noticed that if I make errors in the .latte-file, the latte engine outputs a 500 Server Error, although I wrapped the latte call in a try/catch-block. My code in ProcessWire template: templateContent = '{func($var)}'; // contains an undefined function call $params = []; try { $output = $latte->renderToString( $templateContent, $params ); } catch (\Exception $e) { wire()->log->save('latte_errors', $e->getMessage()); } When testing the same latte markup in the latte sandbox https://fiddle.nette.org/latte/#0b4eb6af74 , it displays an appropriate error message. So there must be a way to catch the error, but I can't find it. Or can this only be done with PHP-features (which I do not know yet), or has ProcessWire any features to catch the errors? Does anyone know how to do it? Link to comment Share on other sites More sharing options...
nurkka Posted July 25 Share Posted July 25 I found the solution – of course right after posting ? One simply has to wrap the latte markup into {try} ... {/try} {try} {func($var)} {/try} And if you want to log the errors, you can do the following: $loggingHandler = function (\Throwable $e, \Latte\Runtime\Template $template) { wire()->log->save('latte_errors', $e->getMessage()); }; $latte->setExceptionHandler($loggingHandler); 1 Link to comment Share on other sites More sharing options...
da² Posted July 25 Share Posted July 25 2 hours ago, nurkka said: try { $output = $latte->renderToString( $templateContent, $params ); } catch (\Exception $e) { wire()->log->save('latte_errors', $e->getMessage()); } Probably here you should catch Error too: catch (\Exception | \Error $e) I use similar code with Twig, rendering an error template if any error. class Twig { public static function render(string $name, array $parameters = []): void { if (NoticeManager::hasNotice()) $parameters['rfroNotices'] = NoticeManager::render(); $parameters['templatesUrl'] = wire()->config->urls->templates; $parameters['homePage'] = wire()->pages->get('/'); $parameters['buildVersion'] = BUILD_VERSION; $parameters['buildDate'] = BUILD_DATE; /** @var TemplateEngineFactory $twigEngine */ $twigEngine = modules('TemplateEngineFactory'); try { echo $twigEngine->render( $name, $parameters ); } catch (Exception|Error $e) { // Catching everything if (wire()->config->debug) // If debug, throw the error with stack trace throw new Error("{$e->getMessage()}\n{$e->getTraceAsString()}"); wire()->log->error($e); $parameters['errorPage'] = wire()->page->path; try { // Try to render my error template, that shows something nicer for users. echo $twigEngine->render('error', $parameters); } catch (Exception|Error) { // It may happen, rarely, that the error is located at a lower level in my Twig structure (inheritance...), // so I display a very basic template that uses no code and can't fail. echo $twigEngine->render('error-safe', $parameters); } } } } Link to comment Share on other sites More sharing options...
Recommended Posts