0

I'm a backend (PHP) developer, but I have converted some (static) HTMLs to dynamic templates (for different people, with different levels of coding knowledge).

It doesn't matter which template engine we use (blade, smarty, twig), we can use "include" and/or "extends".

(what i mean by) include method:

include header
write the content part
include footer

(what i mean by) extends method:

extends layout
write the content part between the correct command ( {block } or @section() )

In some projects "extends" reduces the number of files (ex: instead of header dash-header, footer dash-footer, there are layout and dash-layout).

What crosses my mind and I'm writing about is the KISS principle("Keep it simple, stupid!").

Although the "extends" method is not complicated, people who haven't used it before, tend to get frustrated. And developers would call a template cheap if it is developed with the "include" method.

If we put the argument "it's a personal based opinion" aside, we can say we know the user/employer (person who is going to use/edit the template), and based on that, we can choose a method.

But is there a logical reason (ex: template engine processes, performance, resource consumption) to choose "extends" over "include" ?

FaramarzDev
  • 19
  • 1
  • 8
  • You have tagged this with 3 different template engines, unsure what your real question is here... If you are seeking recommendations over one engine over the other, then that is off topic on SO – DarkBee Jul 03 '23 at 15:07
  • @DarkBee ; the question is in the last paragraph, it's not about choosing a template engine, it's about two most common template developing methods: (extends layout) and (include header+footer), is there a logical reason to choose one over another ? it would be crystal clear if you read the question. – FaramarzDev Jul 04 '23 at 05:47
  • How is this crystal clear? – DarkBee Jul 04 '23 at 06:02
  • IMHO you are asking the wrong question here, the question shouldn't be wether to use `extend` or `include` but *when* should you use `extend` or `include`. Both have their own distinctive use and will be used both throughout a project – DarkBee Jul 04 '23 at 06:10
  • See [here](https://stackoverflow.com/questions/7051743/extending-or-including-what-is-better-in-twig) and/or [here](https://stackoverflow.com/questions/36723690/extend-or-include-multiple-instances-twig). – DarkBee Jul 04 '23 at 06:11
  • @DarkBee ; thank you man, actually i'm looking to check if there is no logical differences between `extend` and `include` ; isn't `extend` against the KISS principle (specially when we are developing for a newbie buyer/employer) – FaramarzDev Jul 04 '23 at 06:25
  • But they are two complete different things. If you want to keep it simple then you create one "master" template (AKA the base template) with one block/section which can be overridden in templates which extends from the master/base template. Includes should be used for reuse-ability, e.g. `{% include "nav/menu.html" with { 'links' main_navigation %}...{% include "nav/menu.html" with { 'links' secondary_navigation %}` – DarkBee Jul 04 '23 at 13:12

0 Answers0