2

I'm using PlayFramework and Twirl to generate some classes using swagger files.

I'm trying to split a big file into small pieces and making it more maintainable. But when the file is rendered the tag with the content from the other file is ignored:

bigFile.scala.txt:

@(otherFile: String) 
{
  { 
   "foo": "bar"
  },
  @otherFile
  {
   "one": "two"
  }
}

otherFile.scala.txt:

{
  {
   "a": "b"
  },
}

bigFile.template:

package com.telefonica.baikal.views.txt

import _root_.play.twirl.api.TwirlFeatureImports._
import _root_.play.twirl.api.TwirlHelperImports._
import _root_.play.twirl.api.Html
import _root_.play.twirl.api.JavaScript
import _root_.play.twirl.api.Txt
import _root_.play.twirl.api.Xml
import models._
import controllers._
import play.api.i18n._
import views.txt._
import play.api.templates.PlayMagic._
import play.api.mvc._
import play.api.data._

object bigFile extends _root_.play.twirl.api.BaseScalaTemplate[play.twirl.api.TxtFormat.Appendable,_root_.play.twirl.api.Format[play.twirl.api.TxtFormat.Appendable]](play.twirl.api.TxtFormat) with _root_.play.twirl.api.Template2[String,String,play.twirl.api.TxtFormat.Appendable] {

/**/
def apply/*1.2*/(otherFile: String):play.twirl.api.TxtFormat.Appendable = {
_display_ {
  {


Seq[Any](format.raw/*1.21*/("""
"""),format.raw/*2.1*/("""{"""),format.raw/*2.2*/("""
"""),format.raw/*3.3*/("""{"""),format.raw/*3.4*/("""
"""),format.raw/*4.5*/(""""foo": "bar"
"""),format.raw/*5.3*/("""}"""),format.raw/*5.4*/(""",
"""),_display_(/*6.4*/otherFile),format.raw/*6.13*/("""
"""),format.raw/*7.3*/("""{"""),format.raw/*7.4*/("""
"""),format.raw/*8.5*/(""""one": "two"
"""),format.raw/*9.3*/("""}"""),format.raw/*9.4*/("""
"""),format.raw/*10.1*/("""}"""),format.raw/*10.2*/("""
"""))
  }
 }
}

Render file:

{
  { 
   "foo": "bar"
  },
  {
   "one": "two"
  }
}

Any idea to render de other file into the big file?

1 Answers1

0

Couple of issues with bigFile.scala.txt:

  • argument otherFile is a string that shadows template with same name.
  • to include a template into another template, use brackets. If you included brackets and didn't rename input argument, it would probably not compile.

So to sum it up, try something like this, bigFile.scala.txt:

@(otherFile2: String)
{
    {
        "foo": "bar"
    },
    @otherFile2,
    @otherFile(),
    {
        "one": "two"
    }
}

Somewhere in your controller:

def serveBigFile() = Action { request =>
  Ok(views.txt.bigFile("""{"x": "y"}"""))
}

Result should look like:

{
    {
        "foo": "bar"
    },
    {"x": "y"},
    {
        {
            "a": "b"
        },
    },
    {
        "one": "two"
    }
}
anamar
  • 104
  • 1
  • 5