简体   繁体   中英

EJS and Express - How to pass data to tag attribute?

I'm trying to pass data object to my HTML page in some tags. This is the code:

<video id="video" class="video-js" poster="<%= data.placeholder %>" >

<source class="link" id="source" src="<%= data.video %>" type='video/mp4'>

and I get an error:

ReferenceError: data is not defined

This is my Express code:

app.get('/lectures/:lecture', (req, res) => {
    const index = req.params.lecture;
    let data = {
        placeholder: `../img/placeholder${index}.svg`,
        video: videoURLs[index]
    };

    res.render('lectures', data);
});

So, how to pass data to tag attribute?

Change your render functionality to:

res.render('lectures', { data });

Actually, it depends on ES version. If you are using ES6 or ES6+, it is okay to use

res.render('lectures', { data });

If ES version lower than 6 (maybe ES5) Use

res.render('lectures', { data: data});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM