Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9099991
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T00:45:22+00:00 2026-06-17T00:45:22+00:00

How is it possible to perform a conditional statement in html template in GAE

  • 0

How is it possible to perform a conditional statement in html template in GAE GO? I was trying to accomplish this to make an option selected in a select html tag:

<select name=".Grade">
          <option value=""></option>
          <option value="1" {{ if .Grade="1" }} selected="selected" {{ end }}>Grade One</option>
          <option value="2" {{ if .Grade="2" }} selected="selected" {{ end }}>Grade Two</option>
          <option value="3" {{ if .Grade="3" }} selected="selected" {{ end }}>Grade Three</option>
          <option value="4" {{ if .Grade="4" }} selected="selected" {{ end }}>Grade Four</option>
          <option value="5" {{ if .Grade="5" }} selected="selected" {{ end }}>Grade Five</option>
          <option value="6" {{ if .Grade="6" }} selected="selected" {{ end }}>Grade Six</option>
</select>

There is

{{ if .Grade }} selected="selected" {{ end }} 

in the reference doc but this only evaluates to true if .Grade has value. Any help would be highly appreciated. Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T00:45:25+00:00Added an answer on June 17, 2026 at 12:45 am

    There is no equality statement in the base template package.
    Here is an interesting discussion from golang-nuts about it.

    You have several possibilities:

    • define an external function for equality, like the one Russ Cox suggests in the golang-nuts thread and test it with a if condition
    • use something the base template package can understand (see my code below)
    • remove some logic from the template: instead of having 6 hardcoded fields, you could construct a datatype with a selected boolean field and give an array of 6 of these objects to a template with a range statement

    I recreated your example by using a slice of booleans:

    func main() {
        temp,err := template.ParseFiles("template.html")
        if err != nil {
            panic(err)
        }
    
        g := make([]bool, 7)
        g[1] = true
        temp.Execute(os.Stdout, &g)
    }
    

    A line in the template looks like this:

    <option value="3"{{ if index . 3 }} selected="selected"{{ end }}>Grade Three</option>
    

    This doesn’t look so good to me. But I’d say that all solutions have their drawbacks and that this is a matter of taste (the third solution should be cleaner but might be considered overkill for such a simple thing).

    Edit (2013/12/11)

    In Go 1.2 (released on 2013/12/01), the template engine has been updated and includes new operators, including comparison. This now should work as expected:

    {{if eq .Grade 1 }} selected="selected" {{end}}
    

    You can still choose to keep as few logic as possible in your templates, though.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to perform an update statement in sql, but only update if
Is it possible to perform an asynchronous wait (read : non-blocking) on a conditional
Is it possible to ask fabric to perform actions conditionally? This should be fairly
Is it possible to perform a partial or patch commit in Team Foundation? I
Is it possible to perform query expansion with Lucene 3.6 using any ontology? I've
Is it possible to perform a replace-string operation inside of a rectangular region in
is it possible to perform a first commit on a new repository using a
Is it possible to perform a global reversed-find on NHibernate-managed objects? Specifically, I have
Is it possible to perform some custom processing when Windsor instantiates a type? Something
Is it possible to perform an action when a UITableViewCell is highlighted? For example

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.