将背景图像添加到EJS文件

时间:2017-10-04 20:23:09

标签: javascript express ejs

我似乎很好地将图像从我的app.js文件渲染到主ejs文件,但是当我将其更改为背景图像时,它不会显示。有什么想法吗?

我的app.js

| 21         | Trust Server Certificate                | VT_BOOL |   No      | Yes       |
来自ejs页面的

const express = require("express"),
  app     = express(),
  bodyParser = require("body-parser"),
  jade = require("jade"),
  path = require('path'),
  redis = require('redis');
  images = [{image:"http://nuvotera.com/wp-content/uploads/2013/10/email-protection-banner-2.jpg"}];

app.use(bodyParser.urlencoded({extended:true}));
app.set("view engine", "ejs");

//render email page
app.get("/email", function(req, res){
  res.render("emailMain", {image:images});
});

app.listen(3000, function(){
console.log("Email Server has started");
});

3 个答案:

答案 0 :(得分:0)

此:

app.get("/email", function(req, res){
  res.render("emailMain", {image:images});
});

应该是这样的:

app.get("/email", function(req, res){
  res.render("emailMain", {images: images});
});

请注意额外的s

此外,您还没有在此处使用变量i

<tr>
<% for(var i=0; i < images.length; i++){ %>

  <td class="vmlHero" background = "<%= images.image %>" style="background-repeat:no-repeat" bgcolor="#000000" width="100%" height="430" valign="top">

我认为应该是<%= images[i].image %>,或者只使用forEach

还值得注意的是background td属性已被弃用,您可以使用样式中的background-imagebackground达到相同的效果

答案 1 :(得分:0)

在app.js中更改

res.render("emailMain", {image:images});

要:

res.render("emailMain", {images:images});

在.ejs文件中更改

background="<%= images.image %>"

要:

background="<%= images[i].image %>"

答案 2 :(得分:0)

我解决了这个最简单的方法。

ejs文件

将此添加到您的CSS文件中

body {
        width: 100%;
        align-items : center;
        height: 100%;
         

        /* Background image is centered vertically and horizontally at all times */
        background-position: center center;

        /* Background image doesn't tile */
        background-repeat: no-repeat;

        /* Background image is fixed in the viewport so that it doesn't move when 
        the content's height is greater than the image's height */
        background-attachment: fixed;

        /* This is what makes the background image rescale based
        on the container's size */
        background-size: cover;

        /* Set a background color that will be displayed
        while the background image is loading */
        background-color: green;
        overflow: hidden;

        /* Location of the image */
        background-image:url('images/introBackGround.jpg') ;
    }  

(删除所有块引用)

路由器文件

您必须添加它。

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

文件夹

public
 └ images