【问题标题】:How to render Image with title and content dynamically?如何动态渲染带有标题和内容的图像?
【发布时间】:2019-09-01 14:49:11
【问题描述】:

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

在我的控制器中

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>

我希望输出:

标题

图片

内容。

【问题讨论】:

  • 架构中的图像字段在哪里。将图像字段添加到架构中,在那里保存该图像或其网址,当您获取它时,您也会在数据中获取您的图像。

标签: javascript node.js mongodb express ejs


【解决方案1】:

将图像 src 添加到架构中。

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

然后使用图片标签。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-18
    • 1970-01-01
    • 2019-05-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多