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.
the name to associate with the attribute. This is the name by which the attribute can be referenced in the template
the value of the attribute
false
(the default) to wrap the value in a Java Bean.
true
to add it, as is.
this object, for chaining
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.
the spec, as described above
one or more values. The values are treated as discrete; that is, lists are not supported.
this object, for convenience
Add a map of objects (key=value pairs) to the template.
Add a map of objects (key=value pairs) to the template.
the attributes to add
false
(the default) to wrap all values in Java Beans.
true
to add it, as is.
this object, for chaining
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"
the attribute's name (i.e., the outermost name)
the map of attribute fields
this object, for convenience
Retrieve an attribute from the map.
Get a map of the attributes in the template.
Get a map of the attributes in the template.
a copy of the internal attributes
Determine whether this template is an anonymous sub-template.
Determine whether this template is an anonymous sub-template. See the StringTemplate documents for details.
true
if the template is an anonymous subtemplate, false
otherwise
Get the template's name.
Get the template's name.
the name, which will always be non-null
Get the underlying native StringTemplate API template.
Get the underlying native StringTemplate API template.
the template
Remove an attribute from the template.
Remove an attribute from the template.
the name of the attribute to remove
Render the template to a string.
Render the template to a string.
The locale to use
The line width
The rendered string
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
.
the name to associate with the attribute. This is the name by which the attribute can be referenced in the template
the value of the attribute
false
(the default) to wrap the value in a Java Bean.
true
to add it, as is.
this object, for chaining
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.
the attributes to add
false
(the default) to wrap all values in Java Beans.
true
to add it, as is.
this object, for chaining
Return a string representation of the template.
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
).
the STWriter
the locale
an implementation of the StringTemplate's
STErrorListener
interface, to receive an errors that
occur during the write
Success(total)
, with the total number of characters written;
or Failure(exception)
on error.
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 ontemplate
, 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:
Seq
(which includes lists and array buffers) is mapped to ajava.util.List
, so it's treated as a multivalued attribute by the underlying ST library.java.util.List
.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.