[2.2.2-Scala] Dynamic Title in Template Form

98 views
Skip to first unread message

Neil Chaudhuri

unread,
Apr 29, 2014, 11:28:40 PM4/29/14
to play-fr...@googlegroups.com
I have a main.scala.html looking as usual:

@(title: String)(content: Html)
<!DOCTYPE html>
<html>
...
</html>

And I have a form that uses it:

@(personForm: Form[PersonData])
@import helper._


@main("Person") { //This is where I have issues
...
}


I am having an impossible time doing the simplest thing--defining the title to pass to the main template from my form. I have tried declaring a string variable for the title in the template and passing it to main. I have also tried adding a title parameter to the form at the top, but the compiler tells me apply will have none of it. I certainly don't think it belongs in the Form, but I guess I could try that.

I am curious about the right way to "calculate" the title and pass it to the main template.

Thanks.


Peter Empen

unread,
Apr 30, 2014, 6:23:51 AM4/30/14
to play-fr...@googlegroups.com
Neil: Your above code should work. What exactly goes wrong? Could you put in minimal code at the places marked by ...?
Peter

Donovan Muller

unread,
Apr 30, 2014, 6:53:28 AM4/30/14
to play-fr...@googlegroups.com
Hi Neil,

Everything looks fine. Should work, as Peter said...

Have you checked and compared your code against the sample apps, specifically see the simplest example (hello-world), which passes a title param:


On 30 April 2014 12:23, Peter Empen <empen...@gmail.com> wrote:
Neil: Your above code should work. What exactly goes wrong? Could you put in minimal code at the places marked by ...?
Peter

--
You received this message because you are subscribed to the Google Groups "play-framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to play-framewor...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Neil Chaudhuri

unread,
Apr 30, 2014, 9:45:36 AM4/30/14
to play-fr...@googlegroups.com
Sorry I was unclear. Nothing actually goes wrong per se, and I certainly have looked at the samples. Let me try to be more clear on my problem.

The problem is I can't figure out how to get anything but a hardcoded title to the main template.

In my example I had this:

@main("Person") { //This is where I have issues
...
}

What if I don't want the title to be a hardcoded value "Person"? I want to "calculate" the title--basically according to whether this is a "create person" form or an "edit person" form.

I have tried this at the top of the template:

@(title: String)(personForm: Form[PersonData])

(where title then gets passed to main) and then this in the controller

Ok(views.html.persons.profile("Desired title", personForm.fill(PersonData(...)))

Then I get a compilation error:

too many arguments for method apply: (title: String)(personForm: play.api.data.Form[controllers.personData])play.api.templates.HtmlFormat.Appendable in object profile

I have also seen this in the samples:

@title = { ... }


But I have no desire to generate new HTML. I just want to control the string that gets sent to the main template via some logic rather than a hardcoded string.

Hopefully this helps clarify. This seems way too simple to be causing a problem.

Thanks.

Donovan Muller

unread,
Apr 30, 2014, 11:59:41 AM4/30/14
to play-fr...@googlegroups.com
See this little demo project: https://github.com/donovanmuller/play-helloworld-dynamic-title

Hope that helps...
Message has been deleted

Donovan Muller

unread,
Apr 30, 2014, 2:32:38 PM4/30/14
to play-fr...@googlegroups.com
Hmm, I might be misunderstanding you but you don't need a form. 
I just used it in my example to show you that you can use any String value to pass through to your main.* template.

I just added this example just to illustrate that you can send any String value in your controller: https://github.com/donovanmuller/play-helloworld-dynamic-title/commit/7a937a8


On 30 April 2014 19:56, Neil Chaudhuri <ne...@vidyasource.com> wrote:
Thanks for the project. I considered the possibility of adding the title to the Form, but I wasn't sure if that was appropriate. Your example suggests it is.

Thanks again.

Neil Chaudhuri

unread,
Apr 30, 2014, 2:33:56 PM4/30/14
to play-fr...@googlegroups.com
Thanks for the demo project, but this is actually more complicated than what I'm asking. It may very well be that what I am really asking for is so trivial and obvious that you can't even fathom someone dumb enough to ask it.

And yet here I am!

All I want is for the title of the page to be "New Person" in the new form and then the person's name in the edit form--while having only one form template. I want to pass the title as a parameter to the form (which will then pass that title on to the main template), but Play won't let me via the Form object. 

That's all.

Does that make sense?

Donovan Muller

unread,
Apr 30, 2014, 2:39:22 PM4/30/14
to play-fr...@googlegroups.com
There are no dumb questions :)

To be honest, I'm not really understanding your problem...
So, why not fork my project, add an example of what your trying to do and then fire off a pull request.

That way I can get a better idea of what you're after, will *hopefully* be able to fix it and then push the fix up.

Neil Chaudhuri

unread,
Apr 30, 2014, 3:55:36 PM4/30/14
to play-fr...@googlegroups.com
I did a pull, but then with some more tinkering, I discovered my problem was simply the syntax I was using to pass the title to the page. A silly error, but I have better command of what's going on.

Thanks for helping me through the thought process.

Donovan Muller

unread,
Apr 30, 2014, 3:59:47 PM4/30/14
to play-fr...@googlegroups.com
Just too late then :)
I actually think I understand your problem from reading your reply again.

Run it and go to: localhost:9000/person

Watch the title as you add/edit...

No probs.
I've always used the Java side of things, so this was a nice way to get into some of the Scala stuff.
That said, some of the stuff could probably be done more functionally.

Cheers
Reply all
Reply to author
Forward
0 new messages