O
O
OKNOZA2015-06-21 23:31:05
linux
OKNOZA, 2015-06-21 23:31:05

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 бла./

It turns out that the user does not see the PHP code, there may be a cycle and whatever you want.
How does it work in NodeJS?

Answer the question

In order to leave comments, you need to log in

7 answer(s)
R
Roman Terekhin, 2018-09-21
@AlexVor

sudo apt-get install apparmor-utils
sudo service apparmor reload

S
Sergey, 2015-06-21
Protko @Fesor

because I don’t see the point, it is already a template in itself.

As long as people writing in php have such thoughts, php will continue to be laughed at. The only reason you can use php as a templating engine is the ability to open and close tags by marking the scope of the script. And for quite a long time (about 8 years for sure) in the civilized world it is customary to use one opening tag per file and not even close it. In general, PHP is a bad templating engine. Moreover, the day is not far off when it will be possible to disable these opening/closing tags altogether.
No way, js is js, html is html. I want perversions - you can store everything in js as strings and work with them. But it is better to always use template engines. This will at least limit you in what you can shove into templates and what not. Ideally, the request for data should not come from the template up the layers, only down. Let's say concepts like HMVC were born precisely from this limitation, while we do not violate the rule that the data flow goes from top to bottom instead of directly accessing logic from view.

I
index0h, 2015-06-22
@index0h

> 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

T
Timofey, 2015-06-22
@mr_T

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.

T
Timur Shemsedinov, 2015-06-22
@MarcusAurelius

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.

I
Ivan, 2015-06-22
@LiguidCool

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 ...

E
Elizaveta Borisova, 2015-06-22
@Elizaveta

As long as people writing in php have such thoughts, php will continue to be laughed at.
Exactly!
Alternatively, you can switch to client-side templating.
If you need to generate a lot of pages, template engines are the easiest.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question