Coder Social home page Coder Social logo

Get value by key from map about pongo2 HOT 5 CLOSED

vgarvardt avatar vgarvardt commented on May 21, 2024
Get value by key from map

from pongo2.

Comments (5)

denisgrib avatar denisgrib commented on May 21, 2024 1

not work, where I can see stupid example?

I need

    for _, key := range gameByViewers {
        fmt.Println(data[key]["name"])
    }

in template

from pongo2.

flosch avatar flosch commented on May 21, 2024

Simply try {% if !mymap.Key %}Key does not exist{% else %}Does exist{% endif %}. Does this work for you? Worked for me.

BTW, instead of using the ! operator, you can use the not keyword as well (if not mymap.Key).

Of course, it should work the other way around as well (not using the not or !-operator).

from pongo2.

flosch avatar flosch commented on May 21, 2024

Working example:

func main() {
        result := pongo2.RenderTemplateString("{% if not mymap.Key %}Key does not exist{% else %}Does exist{% endif %}", pongo2.Context{"mymap": map[string]int{"Key": 1}})
        fmt.Println(result)
}

or

func main() {
        result := pongo2.RenderTemplateString("{% if mymap.Key %}Key does exist{% else %}Does not exist{% endif %}", pongo2.Context{"mymap": map[string]int{}})
        fmt.Println(result)
}

Both works.

from pongo2.

vgarvardt avatar vgarvardt commented on May 21, 2024

Yes, this works fine. Found type in my code. Thanks for quick response!

from pongo2.

flosch avatar flosch commented on May 21, 2024

You're welcome!

from pongo2.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.