在使用html-express-js模板引擎时,在浏览器的源代码视图中缺少换行。

huangapple go评论72阅读模式
英文:

A lack of line breaks in browser's source view using html-express-js template engine

问题

I am not experienced in express js template engines and I tried to choose a simple one from the list https://expressjs.com/en/resources/template-engines.html

我不熟悉express js模板引擎,我尝试从列表https://expressjs.com/en/resources/template-engines.html中选择一个简单的。

I chose html-express-js that is on GH: https://github.com/markcellus/html-express-js

我选择了位于GH上的html-express-js:https://github.com/markcellus/html-express-js

I implemented it in my own code, but when I ran it, there were no line breaks in Chrome source view. So then I implemented the example code from the html-express-js GitHub site, but then the same bad issue happened.

我在自己的代码中实现了它,但当我运行它时,Chrome源代码视图中没有换行符。所以我实现了html-express-js GitHub 网站上的示例代码,但问题仍然存在。

The picture of the issue:

问题的图片:

在使用html-express-js模板引擎时,在浏览器的源代码视图中缺少换行。

在使用html-express-js模板引擎时,在浏览器的源代码视图中缺少换行。

I need the line breaks there, and I think it is normal for the most HTML sites.
Is it a simple way to get the line breaks in this template engine? Or could anyone indicate a good template engine from the list?

我需要在那里有换行符,我认为这对于大多数HTML站点来说是正常的。
有没有一种简单的方法在这个模板引擎中获取换行符?或者有人可以指示列表中的一个好的模板引擎吗?

Below I insert I think the crucial example files

下面我插入了我认为关键的示例文件

example/public/index.js:

import { html } from '../../src/index.js';

export const view = (data, state) => html`
  <!DOCTYPE html>
  <html lang="en">
    <head>
      ${state.includes.head}
      <title>Dashboard</title>
    </head>

    <body>
      <h1>This is the dashboard!</h1>

      <p>
        This file is served by the <code>staticIndexHandler</code> in app.js
      </p>

      <p>Click <a href="/hello">here</a> to go hello route.</p>
    </body>
  </html>
`;

example/app.js:

import express from 'express';
import { resolve } from 'path';
import htmlExpress, { staticIndexHandler } from '../src/index.js';

const __dirname = resolve();

const app = express();

app.engine(
  'js',
  htmlExpress({
    includesDir: 'includes',
  })
);

app.set('view engine', 'js');
app.set('views', `${__dirname}/example/public`);

// serve all other static files like CSS, images, etc
app.use(express.static(`${__dirname}/example/public`));

app.get('/hello', async function (req, res) {
  res.render('hello', {
    name: 'world',
  });
});

// Automatically serve any index.js file as HTML in the public directory
app.use(
  staticIndexHandler({
    viewsDir: `${__dirname}/example/public`,
    notFoundView: 'not-found', // OPTIONAL: defaults to `404/index`
  })
);

export default app;

example/public/index.js:

import { html } from '../../src/index.js';

export const view = (data, state) => html`
  <!DOCTYPE html>
  <html lang="en">
    <head>
      ${state.includes.head}
      <title>Dashboard</title>
    </head>

    <body>
      <h1>This is the dashboard!</h1>

      <p>
        This file is served by the <code>staticIndexHandler</code> in app.js
      </p>

      <p>Click <a href="/hello">here</a> to go hello route.</p>
    </body>
  </html>
`;

example/app.js:

import express from 'express';
import { resolve } from 'path';
import htmlExpress, { staticIndexHandler } from '../src/index.js';

const __dirname = resolve();

const app = express();

app.engine(
  'js',
  htmlExpress({
    includesDir: 'includes',
  })
);

app.set('view engine', 'js');
app.set('views', `${__dirname}/example/public`);

// serve all other static files like CSS, images, etc
app.use(express.static(`${__dirname}/example/public`));

app.get('/hello', async function (req, res) {
  res.render('hello', {
    name: 'world',
  });
});

// Automatically serve any index.js file as HTML in the public directory
app.use(
  staticIndexHandler({
    viewsDir: `${__dirname}/example/public`,
    notFoundView: 'not-found', // OPTIONAL: defaults to `404/index`
  })
);

export default app;

I hope this helps! 如果有任何问题,请随时提出。

英文:

I am not experienced in express js template engines and I tried to choose a simple one from the list https://expressjs.com/en/resources/template-engines.html

I chose html-express-js that is on GH: https://github.com/markcellus/html-express-js

I implemented it in my own code, but when I ran it, there were no line breaks in Chrome source view. So then I implemented the example code from the html-express-js GitHub site, but then the same bad issue happened.

The picture of the issue:

在使用html-express-js模板引擎时,在浏览器的源代码视图中缺少换行。

I need the line breaks there, and I think it is normal for the most HTML sites.
Is it a simple way to get the line breaks in this template engine? Or could anyone indicate a good template engine from the list?

Below I insert I think the crucial example files

example/public/index.js:

import { html } from &#39;../../src/index.js&#39;;

export const view = (data, state) =&gt; html`
  &lt;!DOCTYPE html&gt;
  &lt;html lang=&quot;en&quot;&gt;
    &lt;head&gt;
      ${state.includes.head}
      &lt;title&gt;Dashboard&lt;/title&gt;
    &lt;/head&gt;

    &lt;body&gt;
      &lt;h1&gt;This is the dashboard!&lt;/h1&gt;

      &lt;p&gt;
        This file is served by the &lt;code&gt;staticIndexHandler&lt;/code&gt; in app.js
      &lt;/p&gt;

      &lt;p&gt;Click &lt;a href=&quot;/hello&quot;&gt;here&lt;/a&gt; to go hello route.&lt;/p&gt;
    &lt;/body&gt;
  &lt;/html&gt;
`;

example/app.js:

import express from &#39;express&#39;;
import { resolve } from &#39;path&#39;;
import htmlExpress, { staticIndexHandler } from &#39;../src/index.js&#39;;

const __dirname = resolve();

const app = express();

app.engine(
  &#39;js&#39;,
  htmlExpress({
    includesDir: &#39;includes&#39;,
  })
);

app.set(&#39;view engine&#39;, &#39;js&#39;);
app.set(&#39;views&#39;, `${__dirname}/example/public`);

// serve all other static files like CSS, images, etc
app.use(express.static(`${__dirname}/example/public`));

app.get(&#39;/hello&#39;, async function (req, res) {
  res.render(&#39;hello&#39;, {
    name: &#39;world&#39;,
  });
});

// Automatically serve any index.js file as HTML in the public directory
app.use(
  staticIndexHandler({
    viewsDir: `${__dirname}/example/public`,
    notFoundView: &#39;not-found&#39;, // OPTIONAL: defaults to `404/index`
  })
);

export default app;

答案1

得分: 1

html-express-js模板引擎的html()函数将在HTML模板字符串中全局替换\n\r&#39;&#39;。请参考这行代码v1.1.1/src/index.js#L65:

export function html(strings, ...data) {
  let rawHtml = &#39;&#39;;
  for (const [i, str] of strings.entries()) {
    const exp = data[i] || &#39;&#39;;
    rawHtml += str + exp;
  }
  const html = rawHtml.replace(/[\n\r]/g, &#39;&#39;);
  return html;
}

你可以使用ejsPug模板引擎。请选择知名的库,它们通常更全面,经过充分测试和维护。

英文:

The html() function of html-express-js template engine will replace the \n and \r with &#39;&#39; globally from the HTML template string. See this line v1.1.1/src/index.js#L65:

export function html(strings, ...data) {
  let rawHtml = &#39;&#39;;
  for (const [i, str] of strings.entries()) {
    const exp = data[i] || &#39;&#39;;
    rawHtml += str + exp;
  }
  const html = rawHtml.replace(/[\n\r]/g, &#39;&#39;);
  return html;
}

You can use ejs or Pug template engine. Please choose the famous libraries, they are usually more full-featured, well-tested, and well-maintained.

huangapple
  • 本文由 发表于 2023年6月25日 18:14:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76549901.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定