Call Twirl template in scala code -


i have play scala 2.5 application. know how return twirl template (if possible) inside twirl template scala code.

example : have several twirl templates corresponding html input element. 1 template input text, input checkbox , on. have main template , inside call helper method scala object or class , based on condition return twirl template wanted input element.

in answer suppose templates have same input parameters , output type(html). play docs templates compiled normal scala functions.

for example, let's have 2 templates, template1 , template2, , both have input parameter a: string. main template should use either of two, has parameter of template: string => htmlformat.appendable.

template1:

@(a: string) @{ + " world!" } 

template2:

@(a: string) @{ + " stackoverflow!" } 

main:

@(template: string => htmlformat.appendable) @template("hello") 

if pass template1 "hello world!", , if pass template2 "hello stackoverflow!".

now, define method gettemplate wanted template:

val t1 = views.html.template1.apply _ val t2 = views.html.template2.apply _  def gettemplate(param: int) = if(param == 1) t1 else t2 

and application controller method:

def showtemplate(param: int) = action {   val template = gettemplate(param)   ok(views.html.maintemplate(template)) } 

and route, of course:

get     /template/:id               controllers.application.showtemplate(id: int) 

Comments