Can I use conditional statements with EJS templates (in JMVC)?

前端 未结 7 481
误落风尘
误落风尘 2020-11-30 19:16

and if yes, what is the syntax? My goal is to prepend an \'s\' to the word \'comment\' when there is more than one. in an jQuery.ejs template in a JMVC app. The following br

相关标签:
7条回答
  • 2020-11-30 19:47

    You can also use else if syntax:

    <% if (x === 1) { %>
        <p>Hello world!</p>
    <% } else if (x === 2) { %>
        <p>Hi earth!</p>
    <% } else { %>
        <p>Hey terra!</p>
    <% } %>
    
    0 讨论(0)
  • 2020-11-30 19:50

    Just making code shorter you can use ES6 features. The same things can be written as

    app.get("/recipes", (req, res) => {
        res.render("recipes.ejs", {
            recipes
        });
    }); 
    

    And the Templeate can be render as the same!

    <%if (recipes.length > 0) { %>
    // Do something with more than 1 recipe
    <% } %>
    
    
    0 讨论(0)
  • 2020-11-30 19:55

    I know this is a little late answer,

    you can use if and else statements in ejs as follows

    <% if (something) { %>
       // Then do some operation
    <% } else { %>
       // Then do some operation
    <% } %>
    

    But there is another thing I want to emphasize is that if you use the code this way,

    <% if (something) { %>
       // Then do some operation
    <% } %>
    <% else { %>
       // Then do some operation
    <% } %>
    

    It will produce an error.

    Hope this will help to someone

    0 讨论(0)
  • 2020-11-30 20:00

    Yes , You can use conditional statement with EJS like if else , ternary operator or even switch case also

    For Example

    Ternary operator : <%- role == 'Admin' ? 'Super Admin' : role == 'subAdmin' ? 'Sub Admin' : role %>

    Switch Case

    <% switch (role) {
    case 'Admin' : %>
            Super Admin
            <% break;
    
    case 'eventAdmin' : %>
            Event Admin
            <% break;
    
    case 'subAdmin' : %>
            Sub Admin
            <% break;
    
    } %>
    
    0 讨论(0)
  • 2020-11-30 20:01

    For others that stumble on this, you can also use ejs params/props in conditional statements:

    recipes.js File:

    app.get("/recipes", function(req, res) {
        res.render("recipes.ejs", {
            recipes: recipes
        });
    });
    

    recipes.ejs File:

    <%if (recipes.length > 0) { %>
    // Do something with more than 1 recipe
    <% } %>
    
    0 讨论(0)
  • 2020-11-30 20:04

    Conditionals work if they're structured correctly, I ran into this issue and figured it out.

    For conditionals, the tag before else has to be paired with the end tag of the previous if otherwise the statements will evaluate separately and produce an error.

    ERROR!

    <% if(true){ %>
       <h1>foo</h1>
    <% } %>
    <% else{ %>
       <h1>bar</h1>
     <% } %>
    

    Correct

    <% if(true){ %>
       <h1>foo</h1>
     <% } else{ %>  
       <h1>bar</h1>
    <% } %>
    

    hope this helped.

    0 讨论(0)
提交回复
热议问题