首页 > 解决方案 > 如何动态渲染带有标题和内容的图像?

问题描述

我创建了动态插入的标题和内容,但我想在标题和内容之间添加一个图像,但我找不到方法

在我的控制器中

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>

我期望输出:

标题

图片

内容。

标签: javascriptnode.jsmongodbexpressejs

解决方案


将图像 src 添加到架构中。

const postSchema = {
    title: String,
    content: String,
    url: String,
    altText: String
};

然后使用图像标签。

<Img src="<%=post.url%>" alt="<%=post.altText%>"/>

推荐阅读