简体   繁体   中英

Object properties with special characters in EJS

I am writing a website where I have a language file looking like the following:

replace = {
    "x:a": "Hello",
    "x:b": "World!",
    "y:c.d": "Another text"
}

And now I want to pass this object to my express page (which is using EJS) to replace the template placeholders:

app.js

res.render('index.ejs', replace)

index.ejs

<html>
    <head>
    </head>
    <body>
         <%= x:a %>
         <%= x:b %>
         <%= y:c.d %>
    </body>
</html>

But obviously this isn't working, because x:a is not a valid name, so how can I call these names in my index.ejs?

Pass an object instead, then access properties on it.

res.render('index.ejs', { replace })

     <%= replace['x:a'] %>
     <%= replace['x:b'] %>
     <%= replace['y:c.d'] %>

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