Class/Object

org.clapper.scalasti

ST

Related Docs: object ST | package scalasti

Permalink

class ST extends AnyRef

A Scala interface to a StringTemplate ST template. objet Note that this interface does not directly expose all the underlying ST methods. In particular, this Scala interface is geared primarily toward reading and rendering external templates, not toward generating templates in code.

This class cannot be instantiated directly. Instead, use the apply() methods in the companion object.

Because of the way the ST API instantiates templates, this class cannot easily subclass the real ST class. So, it wraps the underlying string template object and stores it internally. You can retrieve the wrapped template object via the nativeST method. You are free to call methods directly on template, though they will use Java semantics, rather than Scala semantics.

Note that this class explicitly handles mapping the following types of values in an attribute map:

Bean Wrapping

Regular objects are, by default, wrapped in a Java Bean, because the underlying String Template API uses Java Bean semantics to access object fields. Thus, if a template references "foo.bar", StringTemplate will expect that the object associated with the name "foo" has a method called getBar(). To allow Scala objects (and, especially, case class objects) to be used directly, Scalasti automatically generates wrapper Java Beans for them.

There are cases where you don't want this behavior, however. For instance, it doesn't make much sense with numeric values or strings, so Scalasti deliberately does not wrap those objects. There are other cases where you might not want the automatic Bean-wrapper behavior; see the add method for more details.

Linear Supertypes
AnyRef, Any
Ordering
  1. Alphabetic
  2. By Inheritance
Inherited
  1. ST
  2. AnyRef
  3. Any
  1. Hide All
  2. Show All
Visibility
  1. Public
  2. All

Value Members

  1. final def !=(arg0: Any): Boolean

    Permalink
    Definition Classes
    AnyRef → Any
  2. final def ##(): Int

    Permalink
    Definition Classes
    AnyRef → Any
  3. final def ==(arg0: Any): Boolean

    Permalink
    Definition Classes
    AnyRef → Any
  4. def add(name: String, value: Any, raw: Boolean = false): ST

    Permalink

    Add an attribute to the template.

    Add an attribute to the template. Sequences, iterators and maps are converted (copied) to their Java equivalents, with their contents mapped to strings. (If you need nested objects of arbitrary depth, used a mapped aggregate.) Other objects are wrapped in Java Beans, unless raw is set to true.

    NOTE: If you add an AttributeRenderer to a parent group, any objects you intend the renderer to render must be added as raw objects. The underlying StringTemplate API matches attribute renders to attributes by type (i.e., by Java class). If you don't specify raw=true, Scalasti will wrap the object in a generated Java Bean, and StringTemplate won't be able to match it with the corresponding renderer.

    Adding an attribute twice seems to add it twice, causing it to be rendered concatenated, in some cases. If you want to ensure that it is set "clean", use the set method, which clears any existing attribute first.

    name

    the name to associate with the attribute. This is the name by which the attribute can be referenced in the template

    value

    the value of the attribute

    raw

    false (the default) to wrap the value in a Java Bean. true to add it, as is.

    returns

    this object, for chaining

  5. def addAggregate(aggrSpec: String, values: Any*): ST

    Permalink

    Set an automatic aggregate from the specified arguments.

    Set an automatic aggregate from the specified arguments. An automatic aggregate looks like an object from within a template, but it isn't backed by a bean. Instead, you specify the aggregate with a special syntax. For instance, the following code defines an aggregate attribute called name, with two fields, first and last. Those fields can be interpolated within a template via $item.first$ and $item.last$.

    val st = new ST( ... )
    st.setAggregate("name.{first,last}", "Moe", "Howard")

    Setting the same aggregate multiple times results in a list of aggregates:

    val st = new ST( ... )
    st.setAggregate("name.{first,last}", "Moe", "Howard")
    st.setAggregate("name.{first,last}", "Larry", "Fine")
    st.setAggregate("name.{first,last}", "Curley", "Howard")

    Note, however, that this syntax does not support nested aggregates. Use addMappedAggregate() for that.

    See http://www.antlr.org/wiki/display/ST/Expressions#Expressions-Automaticaggregatecreation for more information.

    aggrSpec

    the spec, as described above

    values

    one or more values. The values are treated as discrete; that is, lists are not supported.

    returns

    this object, for convenience

  6. def addAttributes(attrs: Map[String, Any], raw: Boolean = true): ST

    Permalink

    Add a map of objects (key=value pairs) to the template.

    Add a map of objects (key=value pairs) to the template.

    attrs

    the attributes to add

    raw

    false (the default) to wrap all values in Java Beans. true to add it, as is.

    returns

    this object, for chaining

  7. def addMappedAggregate(attrName: String, valueMap: Map[String, Any]): ST

    Permalink

    Create a "mapped aggregate".

    Create a "mapped aggregate". The supplied map's keys are used as the fields of the aggregate. With a mapped aggregate, Scalasti actually translates the map into a Java Bean, which it then uses to set the attribute. Because Scalasti recursively converts all maps it finds (as long as they are of type Map[String, Any]), a mapped attribute can handle nested attribute references.

    The underlying ST library does _not_ support the notion of a mapped aggregate; mapped aggregates are a Scalasti add-on.

    For example, given this map:

    Map("foo" -> List(1, 2), "bar" -> "barski")

    and the name "mystuff", this method will produce the equivalent of the following call:

    template.setAggregate("mystuff.{foo, bar}", List(1, 2), "barski")

    Nested maps are supported. For instance, this code fragment:

    val attrMap = Map("foo"   -> "FOO",
                      "alien" -> Map("firstName" -> "John",
                                     "lastName"  -> "Smallberries"))
    template.setAggregate("thing", attrMap)

    will make the following values available in a template:

    $thing.foo$                  # expands to "FOO"
    $things.alien.firstName$     # expands to "John"
    $things.alien.lastName$      # expands to "Smallberries"
    attrName

    the attribute's name (i.e., the outermost name)

    valueMap

    the map of attribute fields

    returns

    this object, for convenience

  8. final def asInstanceOf[T0]: T0

    Permalink
    Definition Classes
    Any
  9. def attribute[T](name: String)(implicit arg0: scala.reflect.api.JavaUniverse.TypeTag[T]): Option[T]

    Permalink

    Retrieve an attribute from the map.

  10. def attributes: Map[String, Any]

    Permalink

    Get a map of the attributes in the template.

    Get a map of the attributes in the template.

    returns

    a copy of the internal attributes

  11. def clone(): AnyRef

    Permalink
    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  12. final def eq(arg0: AnyRef): Boolean

    Permalink
    Definition Classes
    AnyRef
  13. def equals(arg0: Any): Boolean

    Permalink
    Definition Classes
    AnyRef → Any
  14. def finalize(): Unit

    Permalink
    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( classOf[java.lang.Throwable] )
  15. final def getClass(): Class[_]

    Permalink
    Definition Classes
    AnyRef → Any
  16. def hashCode(): Int

    Permalink
    Definition Classes
    AnyRef → Any
  17. def isAnonSubtemplate: Boolean

    Permalink

    Determine whether this template is an anonymous sub-template.

    Determine whether this template is an anonymous sub-template. See the StringTemplate documents for details.

    returns

    true if the template is an anonymous subtemplate, false otherwise

  18. final def isInstanceOf[T0]: Boolean

    Permalink
    Definition Classes
    Any
  19. def name: String

    Permalink

    Get the template's name.

    Get the template's name.

    returns

    the name, which will always be non-null

  20. def nativeTemplate: stringtemplate.v4.ST

    Permalink

    Get the underlying native StringTemplate API template.

    Get the underlying native StringTemplate API template.

    returns

    the template

  21. final def ne(arg0: AnyRef): Boolean

    Permalink
    Definition Classes
    AnyRef
  22. final def notify(): Unit

    Permalink
    Definition Classes
    AnyRef
  23. final def notifyAll(): Unit

    Permalink
    Definition Classes
    AnyRef
  24. def remove(name: String): Unit

    Permalink

    Remove an attribute from the template.

    Remove an attribute from the template.

    name

    the name of the attribute to remove

  25. def render(locale: Locale = Locale.getDefault, lineWidth: Int = 80): String

    Permalink

    Render the template to a string.

    Render the template to a string.

    locale

    The locale to use

    lineWidth

    The line width

    returns

    The rendered string

  26. def set(name: String, value: Any, raw: Boolean = false): ST

    Permalink

    Set an attribute in the template, clearing any existing attribute of the same name first.

    Set an attribute in the template, clearing any existing attribute of the same name first. Calling this method is the same as calling remove, followed by add.

    name

    the name to associate with the attribute. This is the name by which the attribute can be referenced in the template

    value

    the value of the attribute

    raw

    false (the default) to wrap the value in a Java Bean. true to add it, as is.

    returns

    this object, for chaining

  27. def setAttributes(attrs: Map[String, Any], raw: Boolean = true): ST

    Permalink

    Clear all existing attributes from the template, and add a new map of objects (key=value pairs) to the template.

    Clear all existing attributes from the template, and add a new map of objects (key=value pairs) to the template.

    attrs

    the attributes to add

    raw

    false (the default) to wrap all values in Java Beans. true to add it, as is.

    returns

    this object, for chaining

  28. final def synchronized[T0](arg0: ⇒ T0): T0

    Permalink
    Definition Classes
    AnyRef
  29. def toString(): String

    Permalink

    Return a string representation of the template.

    Return a string representation of the template. NOTE: As of StringTemplate 4, the toString() method no longer renders the template. Use the render method for that.

    returns

    the rendered template.

    Definition Classes
    ST → AnyRef → Any
  30. final def wait(): Unit

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  31. final def wait(arg0: Long, arg1: Int): Unit

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  32. final def wait(arg0: Long): Unit

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  33. def write(out: STWriter, locale: Locale = Locale.getDefault, listener: Option[STErrorListener] = None): Try[Int]

    Permalink

    Write the template to an STWriter.

    Write the template to an STWriter. The STWriter interface is supplied by the underlying StringTemplate API, which also supplies some implementations of it (e.g., AutoIndentWriter and NoIndentWriter).

    out

    the STWriter

    locale

    the locale

    listener

    an implementation of the StringTemplate's STErrorListener interface, to receive an errors that occur during the write

    returns

    Success(total), with the total number of characters written; or Failure(exception) on error.

Inherited from AnyRef

Inherited from Any

Ungrouped