2

I'm trying to make a tag that behaves differently based on the route, but when I try to render html within a @{ ... } block, I can't seem to insert my variables correctly.

@(route: String, on_image: String, off_image: String, alt_text: String)

@{
    val Pattern = ("(" + route + ".*)").r
    val path = request().path()


    path match {
        case Pattern(foo) => {
            if(path != route && route == "/") {
                <script type="text/javascript">
                    alert("{route}");
                </script>
            } else {
                <script type="text/javascript">
                    alert("{route}");
                </script>           
            }
        }
        case _ => {
            <script type="text/javascript">
                alert("{route}");
            </script>       
        }
    }
}

When I load the page, I get a syntax error in my Firebug console, where it's revealed that the content of the script tag evaluated to this:

alert(&quot;/accounts&quot;);

Now, I've tried things like @Html{ ... } and @Html( ... ) and @Html({ ... }) surrounding the whole block. I've also tried alert(@Html("{bar}")); and other similar things, but everything produces various errors.

What's the right way to do this?

Edit

Updated to show trying to use only one variable for simplicity's sake, and showing where that variable comes from.

3
  • where is bar coming from? assuming it is somewhere in the code too, have you tried @ foo and @ bar inside the quotes instead of {foo} ? Commented May 18, 2012 at 20:10
  • That's actually a step backwards. Then I get &quot;@foo&quot; Commented May 18, 2012 at 20:23
  • In any case, the problem is with the quotes, not with what's inside them. You can see from the output that I posted in my question that {foo} (or actually, {route}) returns the value. I need to get that value into a javascript string. Commented May 18, 2012 at 20:25

1 Answer 1

1

Solved with a little indirection.

@(route: String, on_image: String, off_image: String, alt_text: String)

@on = {
    <a href="@route"><img src="@routes.Assets.at(on_image)" alt="@alt_text" /></a>
}

@off = {
    <a href="@route"><img src="@routes.Assets.at(off_image)" alt="@alt_text" /></a>
}

@{
    val Pattern = ("(" + route + ".*)").r
    val path = request().path()


    path match {
        case Pattern(foo) => {
            if(path != route && route == "/") {
                {off}
            } else {
                {on}    
            }
        }
        case _ => {
            {off}       
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.