What are templating engines like .ejs and pug?
Hello, from what I've read, templating engines helps us to make our mark up language dynamic instead of static. That is, during render, we can set some placeholders in our html file itself which get replaced later on.
I read that we can use loops etc in our html file itself, which is kind of interesting. Apart from that, can someone explain why a templating engine is important pls, what kind of cool stuff can we do with it?
12 Replies
a template engine allows to remove the presentation from the logic
and it can make it a lot easier to implement things that contain a repeated structure
they can even allow front-end users to write code for you, in a safe way
for example, twig (for php) has a sandbox where the code to be executed can come from unsafe sources, and only does what you allow
(just to clarify, it allows you to separate "presentation and presentation logic" from "business logic". There's still some logic in templates, and that's why they're powerful, but that logic is purely there to let you have the layout be dynamic based on variables unknown at compile/deploy time.
A big example for me is the "no results" situation where you can render a
<table> if the number of results returned from a query is greater than zero, or a <p> with a message that no results were found if that number is zerothank you for clarifying, i was missing the words
php code without a templating engine looks horrid
and yes, i get the irony of thst
part of their original intent in PHP was to allow frontend developers to write the presentation, while programmers wrote the backend. Normally back then, PHP code business logic was tightly coupled with the HTML it produced, often even interspersed in the HTML, so it was very easy for a frontend dev to break your PHP code, or for them to not understand why certain things in the backend code were done for security reasons for example and expose things unnecessarily. Using a template language lets the backend dev control the data that the "frontend" dev then uses (quotes because old template languages like smarty ran on the backend, but they still made the HTML and presentational logic like a modern frontend dev does)
it's been an interesting thing to watch grow from everything just being .php and switching in and out of PHP mode, to using smarty, to backend primarily just exposing JSON apis now
I still think a backend template engine approach is probably the best for 99% of websites, we've overoptimized to the point where the web is slow compared to the early 2010s
I've still got a piece of code running that I wrote in the aughts, it uses .php files with HTML (I did some templating, using .inc.php files to standardize the header and footer and stuff), and it's FAST. It uses almost no AJAX, just calls back to the server for a new page on each click, and those clicks are just instantaneous all over
oh yeah, using twig is probably the best choice for many
or the horrid blade language that laravel uses
smarty is pretty awesome, but they removed the huge feature of calling any function from it
using php as the template language for your php files is how wordpress works, and is a lot faster than using a template language
but you are missing a lot of features, including the code not looking like unreadable garbage
yes, i hate the template syntax
I don't mind PHP as a template language if you use short open tags
<?=$test?> isn't the end of the worldtrue, but this is:
🤢
yeah, that's not very nice
not at all
I haven't dive into presentation logic, business logic and data logic yet but here:
it allows you to separate "presentation and presentation logic" from "business logic". There's still some logic in templatesWhen we say there are still some logic, the logic is related to the presentation/style of the page? Like if user is login, maybe show a small green icon at the top right corner of the page, this is considered as presentation logic?
that, and things you don't know until there is a presentation. Pagination can be part of it, or whether to show a default avatar or a specific one
yep I see
presentation layer, business layer etc, these are what we call as "system designs" ?