Answer the question
In order to leave comments, you need to log in
Can I not use NodeJS templating engine?
I began to slowly learn NodeJS, the question arose, how to embed NodeJS code in HTML?
I’ll say right away that JS will work without problems, why such a question, I don’t use a template engine in PHP, because I don’t see the point of it being a template in itself.
index.php
/...бал html бла./
<?=внедрил код;?>
/...бал html footer бла./
Answer the question
In order to leave comments, you need to log in
sudo apt-get install apparmor-utils
sudo service apparmor reload
because I don’t see the point, it is already a template in itself.
> How does it work in NodeJS?
Take it as the original: you work with the string that you write in the body of the response. No more and no less. Yes, you can make this line as "< html >< body >" + myContentVarHere + "< /body >< /html >", but why? There are popular template engines: Jade, EJS, JQtemplates use them.
@Fesor
> In general, PHP is a bad templating engine.
Disagree. PHP is a stateless language, which is quite a templating engine. According to Wikipedia: PHP: Hypertext Preprocessor. Yes, there is twig, yes there is smarty, but these templating engines only parody PHP. The result of their work is the same PHP code, only cached.
> As long as people writing in php have such thoughts, php will continue to be laughed at.
1. Dynamic typing. 5 + "5abc" + "abc5" for good should give an exception in the style of "guy change the drug dealer, this is some kind of UG!".
2. Not consistent API. With the same if (! strpos (...)) they probably drank grief.
3. Having a million standards. Yes, there is a PSR, tell the author who knows the kohan about it.
4. What about scalar type hinting? Only 20 years have passed since they decided to introduce it in the 7th, and then with restrictions on the withdrawal.
5. What about demons? Yes, I know it's possible, yes I know how, but №;%: there are languages adapted for this and php is not one of them, it's a stateless language!
I moved away from the topic of template engines.
Why is {{someVar}} better than <?= $someVar; ?>, considering
I'll be original and just answer the question.
By itself, JS does not interfere with HTML code in principle (except for PHP, this does not happen anywhere, in fact), that is, there is no way to directly embed JS code in HTML. Therefore (and not only because, but other reasons are colorfully described in other answers) template engines are used to generate HTML in NodeJS. And if you need something that will be as similar to PHP as possible, then you can take EJS. But since you have already taken up the node, then in my opinion you need to take Jade without reasoning, which is better nowhere, in particular in other languages / environments.
Template engines are needed only if you make indexed pages such as an information site or blog, a node can do this, but this is not its profile, it is a bad habit for web applications. If you have an application, then make it a one-page SPA, give the root page and AJAX API on the server, and take JSON on the client and modify the DOM of the page according to the data. For the browser side, check out React and WebComponents.
It is not required, just as it is not necessary to use any Framework like Express. You can write in pure JS, but why?
In general, it depends on the situation. I often do not use a template engine in PHP, but this is convenient only when not a lot of data is displayed on the page. If there are a lot of them, everything becomes a mess ...
As long as people writing in php have such thoughts, php will continue to be laughed at.Exactly!
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question