-1

My web app has a main title which shows up on each page. For example, the way stackoverflow will show up in the top menu on every page. Currently this title is hard coded in my main.scala.html:

<title>My Main Title</title>

I want to make this title configurable on a settings page that I have. How can I persist this so that I can load it later and have it configurable by the user?

nbz
  • 3,806
  • 3
  • 28
  • 56
  • Create a model `Title` and set some default value... where's the problem? – biesior Sep 11 '14 at 22:14
  • There isn't any way to ensure that there is only one object of this model at all times? – nbz Sep 11 '14 at 22:39
  • 1
    Why would you want to ensure that there is only one object? For now, use just the first object and never add a second; in the future, you may want more titles (i18n, skinning for customers, new sites based on the same DB, groups of pages within your app....) – Paul Hicks Sep 11 '14 at 23:32
  • that's a good point, I didn't consider that. Thank you! that's what I have done for now. I just get the first object and not create any new ones. – nbz Sep 12 '14 at 14:02
  • If you post that as an answer, I am happy to accept it. – nbz Sep 12 '14 at 14:39

2 Answers2

0

Send variralbe of type String to Play Framework's views when rendering the page and use that varaible as title. This variable can be configured in controller:

  1. Your Controller

    public static Result func() { String title = "Your Title"; //Your Code return ok(index.render(title)); }

  2. Your view

    @(title: String)<!DOCTYPE html> <html> <head> <title>@title</title> </head> <body> <!--Your Body--> </body> </html>

rishiAgar
  • 168
  • 3
  • 10
  • thanks, I already knew that one. I wanted something that can be edited by the user. I have used a Model to do this. – nbz Sep 12 '14 at 14:39
0

Just create Title model + template for editing, search for title with highest ID to make sure that you'll take only latest one (in case if there will be more than one:

String pageTitle = Title.find.setOrderBy("id DESC").setMaxRows(1).findUnique();

On the quite other hand, for storing static messages, you can also use conf/messages file, i.e. add line:

mainTitle = My Main Title

So you can use it in any template as:

<title>@Messages("mainTitle")</title>

It will not allow to change the title 'on the fly' anyway as you can see it;s much easier approach then selecting pageTitle each time from DB and passing as a param.

biesior
  • 55,576
  • 10
  • 125
  • 182