gpt4 book ai didi

php - 像在 php 中一样在 nodejs 中渲染 html

转载 作者:搜寻专家 更新时间:2023-11-01 00:28:20 25 4
gpt4 key购买 nike

这似乎是一个简单的问题,但我在 google 或 stackoverflow 中找不到任何相关信息。可能我还没有找到合适的关键字。

在php中你可以这样写

例子.php

<?php
//a bunch of sloppy logic, or maybe some includes but for this I'll just define the variables below...
$title = "Word to your mother";
$heading = "Too legit";
$paragraph = "Extensive research has shown that people's taste in music stops changing once they are forced to earn a living. Maybe that's when they learn how dumb the lyrics really are or maybe they don't have time to waste on things that no longer matter...";
?>
<!DOCTYPE html>
<html>
<head>
<title><?=$title?></title>
</head>
<body>
<h1><?=$heading?></h1>
<p><?=$paragraph?></p>
<h2>Counting</h2>
<?php
for($x=1;$x<=10;$x++){
?>
<p><?=$x?></p>
<?php
}
?>
</body>
</html>

在 nodejs 中是否有类似的东西?

更广泛的问题与从浏览器视为静态的 nodejs 服务器提供动态内容有关。在客户端需要任何类型的 JavaScript 的解决方案不适合我。

我看到了 ejs 和 vue,但它们都有类似 <script src='source.js'> 的东西在 DOM 上,这正是我不想要也不能拥有的。

如果您认为这是重复的,请在评论中指出,并在将其标记为重复之前给我一个澄清的机会。

澄清/更新:

术语"template"可能是我要描述的功能的否定关键字。在上面的 php 示例中,我可以在 if 语句或循环中嵌套一个 html 元素,同时保持服务器标记内联。还有一个帖子here问题基本上问的是同样的事情,但答案并不令人满意。

最佳答案

使用 Express,您可以使用模板引擎(例如 EJS)从服务器提供静态内容。

At runtime, the template engine replaces variables in a template file with actual values, and transforms the template into an HTML file sent to the client.

如果您从未使用过 Express,您可以在这里阅读:https://expressjs.com/en/guide/using-template-engines.html

关于php - 像在 php 中一样在 nodejs 中渲染 html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49101058/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com