javascript - 如何动态渲染带有标题和内容的图像?
问题描述
我创建了动态插入的标题和内容,但我想在标题和内容之间添加一个图像,但我找不到方法
在我的控制器中
const postSchema = {
title: String,
content: String,
};
const Post = mongoose.model("Post", postSchema);
app.get("/", function(req, res){
Post.find({}, function(err, posts){
res.render("home", {
posts: posts
});
});
});
和我的 ejs
**
<h3><%=post.title%></h3>
<p>
<%=post.content.substring(0, 200) + " ..."%>
<a href="/posts/<%=post._id%>">Read More</a>
</p>
我期望输出:
标题
图片
内容。
解决方案
将图像 src 添加到架构中。
const postSchema = {
title: String,
content: String,
url: String,
altText: String
};
然后使用图像标签。
<Img src="<%=post.url%>" alt="<%=post.altText%>"/>
推荐阅读
- c# - 跳得更快?(纸马里奥风格跳跃)
- java - 如何使用 iText7 对多页 PDF 进行 OCR?
- python - `pip` 成功将`discord.py` 安装到`1.3.4`,但是`print(discord.__version__)` 显示`1.3.3`
- r - 如何不使用 R 中的函数修改 S4 对象?(流程集)
- javascript - 当一个刀片包含在另一个刀片中时,Vue.js 被忽略
- haskell - IO 是 Functor 的一个实例,仅仅是因为它首先是一个 Monad 吗?
- python - 弹出式 Folium 中的嵌入式 HTML
- ssh - GitLab CI / CD 通过 SSH 部署脚本不起作用 -> 未受保护的私钥文件
- xml - 动态地使用 for-each 变量遍历节点
- java - GuardedBy 和 concurrentHashMap