schranz-templating / templating

A template abstraction prototype for php template engines.
MIT License
24 stars 0 forks source link

Add `exist` method to TemplateRendererInterface #58

Open alexander-schranz opened 1 year ago

alexander-schranz commented 1 year ago

There are currently 2 options we can use. There can be a TemplateNotFoundException or a exist method on the TemplateRendererInterface. As feedback from @fabpot here: https://github.com/php-fig/fig-standards/pull/1280#issuecomment-1143306242:

Having to catch an exception for non-existing templates was a performance issue in Twig as we have a feature that let you fall back to other templates. So we added a way to check if a template exists that returns a Boolean.

So the exception isn't ideal and a exist method is better. We need to check which

stof commented 1 year ago

Another note: an exception does not allow to implement the fallback feature if the only API you have is ->render(), as you don't want the fallback to apply if the template exists but include another template for which we have a failure (with no fallback there). Twig used to be able to use an exception-based logic for that thanks to its loadTemplate API that only loads (and compiles) a template without rendering it (and so without resolving other imports which might trigger the exception as well). Without that, you don't know whether the exception is for your own template or a referenced one.

alexander-schranz commented 1 year ago

@stof that is great insight knowledge about the handling. Thank you for sharing!