# Template API for writing templates
Here are the various functions you might use in your templates.
For each function, documentation specifies mandatory arguments at the beginning while optional ones are
annoted with `[]`.
Most of them support the optional dc attribute to use data from another datacenter. If the `dc`
attribute is not specified, the function will output data from the current datacenter.
To ease template development, `consul-templaterb` supports HOT reload of templates, thus it is possible to
develop the templates interactively. While developing, it is possible to use the switch `--hot-reload=keep`,
thus the application will display a warning if the template is invalid and won't stop
(`--hot-reload=die` is the default, thus if the hot-reloaded template has issue, the application will die).
Have a look to [samples/](samples/) directory to start writing your own templates.
## Common structure of returned objects
All objects returned by those functions described below all share the same structure:
* `.result` : handle the result
* `.endpoint` : get technical information about how data was retrieved and statistics.
## Accessing to statistics to monitor your cluster
All endpoints implement technical interface that allow to get information about what
is going on in your Consul Cluster.
On each object, you can access the `.endpoint` object that includes several informations about the endpoint being queried:
* `myresult.endpoint.x_consul_index` return the current index on blocking query
* `myresult.endpoint.stats` a object with interresting fields: `bytes_per_sec`,
`bytes_per_sec_human`, `successes`, `errors`, `body_bytes`, `last_modified`. All stats details
are available in the file [lib/consul/async/stats.rb](lib/consul/async/stats.rb).
Using those statistics might be useful to trigger alerts very easily when something
is going on. Have a look to [samples/metrics.erb](samples/metrics.erb) that exposes
most of those metrics to [Prometheus](https://prometheus.io/).
## Common re-implemented functions for all objects
Most objects returned by all those functions are contained within a `.result` object. However, in order
to avoid having to write .result in all templates, some shortcuts have been added: `[]` allow to either access values for map-based data or arrays.
Also available for all results:
### Common methods available for all objects
* .all?
* .any?
* .assoc
* .chunk
* .chunk_while
* .class
* .clear
* .collect
* .collect_concat
* .compact
* .count
* .cycle
* .detect
* .dig
* .display
* .drop
* .drop_while
* .dup
* .each
* .each_cons
* .each_entry
* .each_slice
* .each_with_index
* .each_with_object
* .empty?
* .entries
* .enum_for
* .eql?
* .equal?
* .extend
* .fetch
* .find
* .find_all
* .find_index
* .first
* .flat_map
* .flatten
* .grep
* .grep_v
* .group_by
* .hash
* .include?
* .index
* .inject
* .inspect
* .is_a?
* .itself
* .keep_if
* .kind_of?
* .lazy
* .length
* .map
* .max
* .max_by
* .member?
* .min
* .min_by
* .minmax
* .minmax_by
* .nil?
* .none?
* .object_id
* .one?
* .partition
* .pp
* .rassoc
* .reduce
* .reject
* .replace
* .reverse_each
* .select
* .shift
* .size
* .slice
* .slice_after
* .slice_before
* .slice_when
* .sort
* .sort_by
* .sum
* .take
* .take_while
* .tap
* .to_a
* .to_enum
* .to_h
* .to_s
* .uniq
* .values_at
* .yield_self
* .zip
### Methods available for Array objects
* .append
* .at
* .bsearch
* .bsearch_index
* .combination
* .concat
* .each_index
* .fill
* .insert
* .join
* .last
* .pack
* .permutation
* .pop
* .prepend
* .product
* .push
* .repeated_combination
* .repeated_permutation
* .reverse
* .rindex
* .rotate
* .sample
* .shuffle
* .to_ary
* .transpose
* .unshift
### Methods available for hash objects
* .compare_by_identity
* .compare_by_identity?
* .each_key
* .each_pair
* .each_value
* .fetch_values
* .has_key?
* .has_value?
* .invert
* .key
* .key?
* .keys
* .merge
* .rehash
* .store
* .to_hash
* .to_proc
* .transform_keys
* .transform_values
* .update
* .value?
* .values
## coordinate
The coordinate object allow to interact with the coordinates of DCs and nodes as explained in
[Network Coordinates](https://www.consul.io/docs/internals/coordinates.html).
### coordinate.datacenters([dc: datacenter])
[List the Wan Coordinates](https://www.consul.io/api/coordinate.html#read-wan-coordinates) from local DC to
other DCs. If dc is set, it will perform the same operation but from another DC.
### coordinate.nodes([dc: datacenter])
[Read all LAN nodes coordinates](https://www.consul.io/api/coordinate.html#read-lan-coordinates-for-all-nodes).
If If dc is set, it will perform the same operation but for another DC.
### coordinate.rtt(nodeA, nodeB)
Computes the rtt between 2 nodes returned by `coordinate.nodes` or `coordinate.datacenters`. A re-implementation of Golang sample code
[Working with Coordinates](https://www.consul.io/docs/internals/coordinates.html#working-with-coordinates).
## datacenters()
[Get the list of datacenters as string array](https://www.consul.io/api/catalog.html#list-datacenters).
Examples
Examples
Examples
Examples