タグの属性にJSの変数を使う【Pug】

  プログラミング, HTML/CSS

DBにあるimgSrcという変数をimgタグのsrc属性に入れる

DBにimgSrcを保存するようにする

const Post = sequelize.define('Post', {
  id: {
    type: Sequelize.INTEGER,
    autoIncrement: true,
    primaryKey: true
  },
  content: {
    type: Sequelize.TEXT
  },
  imgSrc: {
    type: Sequelize.STRING
  }
}, {
    freezeTableName: true,
    timestamps: true
  });

imgSrcに内容を追加

Post.create({
  content: content,
  imgSrc: `https://hoge.hoge/hoge.png`
  ・
  ・

Pugで呼び出す

- var imgSrc = post.imgSrc
  img(src=imgSrc width="480" height="360")