John Carty
John Carty

Reputation: 262

Change HTML tag based on results from database in Adonis.js

I am building a node.js app using Adonis and depending on what one of the columns returned in my query is change how the html is done. If the row has a 'Y' in the Group column I need to display it as an <h3> otherwise it needs to be displayed as a <label> for a <select>. Any idea the best way to check this? I tried to use @if, but I get the following error E_INVALID_EXPRESSION: Invalid expression <{{ question.Group }} == 'Y'> passed to (if) block. Here is an example of the code in my app.

@each(question in questions)
        @if( {{ question.Group }} == 'Y')
          <h3> {{ question.Questions }}</h3>        
        @else
          <label for="{{ questions.SalesQuestionsKey }}"> {{ question.Questions }}, {{question.Group }}</label>
          <select type="text" name="{{ question.SalesQuestionsKey }}">
            <option value="na">NA</option>
            <option value="yes">Yes</option>
            <option value="no">No</option>
          </select>
        @endif
        <br />
@endeach

Upvotes: 2

Views: 424

Answers (1)

Nathan Fries
Nathan Fries

Reputation: 1524

You don't need to interpolate inside a conditional according to the syntax guide. Try this:

@each(question in questions)
    @if(question.Group == 'Y')
      <h3> {{ question.Questions }}</h3>        
    @else
      <label for="{{ questions.SalesQuestionsKey }}"> {{ question.Questions }}, {{question.Group }}</label>
      <select type="text" name="{{ question.SalesQuestionsKey }}">
        <option value="na">NA</option>
        <option value="yes">Yes</option>
        <option value="no">No</option>
      </select>
    @endif
    <br />
@endeach

[https://edge.adonisjs.com/docs/conditionals]

Upvotes: 1

Related Questions