Play scala template syntax for a string

tigerdev2000

I am trying to drive the href attribute of this html anchor tag using a scala parameter and can't seem to get it to work.

@{
    val key = p.getKey()
    if(key == "facebook") {
     <a href="/authenticate/@(key)">Sign in with facebook</a>
    } else if (key == "twitter"){
     <a href="/authenticate/{key}">
        <span>Sign in with twitter {key} (this works)</span>
     </a>
    }    
}

In both examples the href attribute is not generating properly, but when I use {key} in the span tag that is outside of the html attribute, it prints out the key properly.

alextsc

Twirl doesn't have support for else-if. Since that caused you problems you wrapped it in a dynamic block @{}, which you can make work I think (never tried it). However this is not how things are usually done, it's preferred to use pattern matching instead.

Here is how your code could look like:

@p.getKey() match {
    case "facebook" => {
        <a href="/authenticate/@{p.getKey()}">Sign in with facebook</a>
    }
    case "twitter" => {
        <a href="/authenticate/@{p.getKey()}">
            <span>Sign in with twitter - key @{p.getKey()} </span>
        </a>
    }
}

Now that works, however you can also define reusable scoped values with defining (instead of vals) to cut down on the duplication of p.getKey and the href itself:

@defining(p.getKey()) { key =>
    @defining(s"/authentication/$key") { href =>
        @key match {
            case "facebook" => {
                <a href="@href">Sign in with facebook</a>
            }
            case "twitter" => {
                <a href="@href"> <span>Sign in with twitter - key @key</span> </a>
            }
        }
    }
}

When assuming that the message is all the same except for the key it gets even easier, scrap the pattern matching and the href defining (since its used just once):

@defining(p.getKey()) { key =>
    <a href="/authentication/@key">Sign in with @key</a>
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Play Framework Scala Template

From Dev

Dealing with Options in Scala Play Template

From Dev

Play scala template with google Multimap

From Dev

Referencing template syntax system in Play framework

From Dev

Syntax and meaning of a Scala/Play! code sample

From Dev

Logging syntax for Play Framework 2 in Scala

From Dev

Scala + Play: how to pass a parameter into a route in a template?

From Dev

Render scala template outside of controller? (Play 2)

From Dev

Get scala template in a variable in Play Framework

From Dev

play framework passing sub class to scala template

From Dev

using bootstrap with play framework scala template

From Dev

Play framework scala template with case statement on enumeration

From Dev

Scala + Play: how to pass a parameter into a route in a template?

From Dev

play framework passing sub class to scala template

From Dev

using bootstrap with play framework scala template

From Dev

Using onclick event for play framwork scala template

From Dev

Play Scala template, define object vaiable

From Dev

Syntax highlighting for scala.html and routes files in IntelliJ / Play framework

From Dev

Scala Play Json Format for Map[Locale, String]

From Dev

Reads with validation for email or empty string in Play/Scala

From Dev

Serializing a JSON string as JSON in Scala/Play

From Dev

Serializing a JSON string as JSON in Scala/Play

From Dev

Cannot stream simple string in Play 2.5 (Scala)

From Dev

Play! Framework: Scala code inside play. scala.html template

From Dev

Play framework 2.1.3 function that will render scala template with given parameters

From Dev

Code formatter for Play's .scala.html template files

From Dev

Add <script> to the <head> from scala template tags in Play Framework 2

From Dev

reading cookie value in play framewrok 2.2 scala template

From Dev

Scala - unbound wildcard exception (Play Framework 2.3 Template)

Related Related

  1. 1

    Play Framework Scala Template

  2. 2

    Dealing with Options in Scala Play Template

  3. 3

    Play scala template with google Multimap

  4. 4

    Referencing template syntax system in Play framework

  5. 5

    Syntax and meaning of a Scala/Play! code sample

  6. 6

    Logging syntax for Play Framework 2 in Scala

  7. 7

    Scala + Play: how to pass a parameter into a route in a template?

  8. 8

    Render scala template outside of controller? (Play 2)

  9. 9

    Get scala template in a variable in Play Framework

  10. 10

    play framework passing sub class to scala template

  11. 11

    using bootstrap with play framework scala template

  12. 12

    Play framework scala template with case statement on enumeration

  13. 13

    Scala + Play: how to pass a parameter into a route in a template?

  14. 14

    play framework passing sub class to scala template

  15. 15

    using bootstrap with play framework scala template

  16. 16

    Using onclick event for play framwork scala template

  17. 17

    Play Scala template, define object vaiable

  18. 18

    Syntax highlighting for scala.html and routes files in IntelliJ / Play framework

  19. 19

    Scala Play Json Format for Map[Locale, String]

  20. 20

    Reads with validation for email or empty string in Play/Scala

  21. 21

    Serializing a JSON string as JSON in Scala/Play

  22. 22

    Serializing a JSON string as JSON in Scala/Play

  23. 23

    Cannot stream simple string in Play 2.5 (Scala)

  24. 24

    Play! Framework: Scala code inside play. scala.html template

  25. 25

    Play framework 2.1.3 function that will render scala template with given parameters

  26. 26

    Code formatter for Play's .scala.html template files

  27. 27

    Add <script> to the <head> from scala template tags in Play Framework 2

  28. 28

    reading cookie value in play framewrok 2.2 scala template

  29. 29

    Scala - unbound wildcard exception (Play Framework 2.3 Template)

HotTag

Archive