4

I am using Scalate (scaml specifically, but the format is somewhat unimportant), and I have a number of values that are defined across a number of templates. These variables are not always known at compile time. My objective is to define them in a single shared template that I include as part of other templates (sort of a template inheritance). I would also like these values to be lazily instantiated. So what I want is something like this:

globals.scaml:

- import mylib.DataProvider._

- lazy val name = get("name")
- lazy val address = get("address)

template.scaml:

= include("globals.scaml")
The address for #{name} is #{address}

I have messed around with various schemes, including reversing this such that globals.scaml loads the correct template while passing in some variables, like this:

globals.scaml:

-@ val template: String
- import mylib.DataProvider._

- val data = Map("name" -> get("name"), "address" -> get("address))
- render(template, data)

But this only works if I actually declare the vals in the template as well, like this:

-@ val name:String
-@ val address:String

The address for #{name} is #{address}

Problem is this is actually worse than just repeating myself in every template, because I still have to declare all the vals and I don't get lazy evaluation.

Is there a way (without writing my own pre-parser) to do this in Scalate?

rs_atl
  • 8,935
  • 1
  • 23
  • 28

0 Answers0