gpt4 book ai didi

Unable to include CSS styling in handleBar templates with Node Js App(无法使用Node Js App在车把模板中包含CSS样式)

转载 作者:bug小助手 更新时间:2023-10-25 09:09:38 27 4
gpt4 key购买 nike



I have a Node Js-Express Based app and using handlebars template to design a structure for Nodemailer HTML body. I tried including the CSS file multiple ways for the same, but still none of the styles are being applied to the HTML.

我有一个基于Node Js-Express的应用程序,并使用把手模板来设计一个Nodemailer的HTML主体结构。为了同样的目的,我尝试了多种方式来包含css文件,但是仍然没有一种样式被应用到HTML。


My current project structure is something like this:

我目前的项目结构是这样的:


- backend
- src
- helpers
- templates
- emailTemplate.handlebars
- public
- styles.css
- app.js

The app.js is defined to use the handlebars like this:

App.js被定义为使用如下所示的把手:


const express = require('express');
const app = express();

app.use(express.static(path.join(__dirname, '/public')));

The handlebar template goes like this:

车把模板如下所示:


<html>
<head>
<title>Update Project Details</title>
{{#each css}}
<link rel="stylesheet" href="../../public/{{this}}">
{{/each}}
</head>

<body>
<h1>{{headerMessage}}</h1>
<p>{{message}}</p>
<p>
{{bodyText}}
<div class="row">
<div class="side">
<button class="edit"> Edit Details </button>
</div>
<div class="main">
<button class="archive"> Archive Product </button>
</div>
</div>
</body>
</html>

As the nodemailer functions and mail is received, the HTML content is rendered from the HandleBar template inside the mail body, but without any CSS or styling.

当nodemailer运行并接收邮件时,将从邮件正文中的Handlebar模板呈现HTML内容,但不使用任何CSS或样式。


I also tried keeping the CSS file inside the same folder as the template, but no luck. Not sure where I'm going wrong. Any help to resolve the same will be highly appreciated.

我还尝试将css文件保存在与模板相同的文件夹中,但没有成功。不知道我错在哪里了。任何帮助解决这一问题的人都将受到高度赞赏。


更多回答
优秀答案推荐

the only issue that I see here is the actual linking up of the style sheet to your DOM. If you look at the app.use in your app.js where it says:

我在这里看到的唯一问题是样式表与DOM的实际链接。如果您查看app.js中的app.use,它会显示:


app.use(express.static(path.join(__dirname, '/public')));

that means that any so called "public" files, images, local js, style are stored in the public folder, and that is the new relative path for all headers. So no matter where your ejs or html file is, the relative path to the style sheet will always be from the top level down of your public folder. This makes it more dynamic and the only thing you have to change is:

这意味着任何所谓的“公共”文件、图像、本地js、样式都存储在公共文件夹中,这是所有头文件的新相对路径。因此,无论您的ejs或html文件在哪里,样式表的相对路径都将始终是从您的公用文件夹的顶层向下。这让它更有活力,你唯一需要改变的是:


<link rel="stylesheet" href="../../public/{{this}}">

to:

致:


<link rel="stylesheet" href="{{this}}">

Simple as that. Hope this makes sense, happy coding!

就这么简单。希望这是有意义的,快乐的编码!



i used internal style. i placed the css code on the head section it worked.

我用的是内部风格。我把css代码放在头部分,它起作用了。


更多回答

Thanks for the explanation. After changing, it still doesn't seem to work :/

谢谢你的解释。换了以后,似乎还是不管用:/

@enthu_dev Hey there, try just doing <link rel="stylesheet" href="style.css"> I dunno what you mean by the {{this}} tag so just flush it out with the first section in this comment.

@enthu_dev嘿,试着只做我不知道您所说的{{this}}标记是什么意思,所以只需用这条评论的第一部分将其清除。

Still not work. Though I found a weird flex. I inspected the email body HTML in the browser, and seems like even the class="" are removed from every <div> or any HTML tags, forget about including the CSS. Not understanding why is this happening. At least the class names should be there while inspecting the code.

还是不管用。虽然我发现了一种奇怪的弯曲。我检查了浏览器中的电子邮件正文HTML,似乎甚至从每个

或任何HTML标记中都去掉了class=“”,忘记了包括css。不明白为什么会发生这种事。在检查代码时,至少应该有类名。

No issues, Thank you anyway for looking into this.

没问题,不管怎样,谢谢你调查这件事。

hey, did you find any solution for this ?

嘿,你有什么办法解决这个问题吗?

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

正如它目前所写的,你的答案并不清楚。请编辑以添加更多详细信息,以帮助其他人了解这是如何解决提出的问题的。你可以在帮助中心找到更多关于如何写出好答案的信息。

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