I have a question about best practices in templates.
Let's assume I am editing the product details template file. I would like to call a method in the product model which can raise an exception.
What is the best way to proceed :
- to put the call in a verbose try / catch in the template, displaying the appropriate result in case of success/failure ;
try { echo $product->getSomething()->getName(); } catch (NoSuchEntityException $ex) { echo "No related Something"; } catch (\Exception $ex)) { echo "An error occured while extracting the related Something"; }
- or creating an intermediate method (in the block ?), handling the try catch and returning and array with the result (if success) and a possible message, to make the template cleaner?
$result = $block->getSomething(); if($result['message'] === "") { // Everything went well echo $result['Something']->getName(); } else { // Something went wrong echo $result['message']; }