diff options
author | Kristian Lyngstol <kly@kly.no> | 2016-11-15 06:36:17 +0100 |
---|---|---|
committer | Kristian Lyngstol <kly@kly.no> | 2016-11-15 06:36:17 +0100 |
commit | 502c46a7299cac16018a8a2e7b376f139627ecae (patch) | |
tree | 73830ff34598b38ab6e82909731bddc14704a3ff /web | |
parent | 5543e7dc3072df61910350b46879908f985f94b4 (diff) |
More tweaks on template-examples/documentation
Diffstat (limited to 'web')
-rw-r--r-- | web/templates/HOWTO.txt | 30 | ||||
-rw-r--r-- | web/templates/switches.txt | 9 |
2 files changed, 33 insertions, 6 deletions
diff --git a/web/templates/HOWTO.txt b/web/templates/HOWTO.txt index de98b6a..ed47926 100644 --- a/web/templates/HOWTO.txt +++ b/web/templates/HOWTO.txt @@ -83,6 +83,36 @@ Other worth-while api-endpoints: - public/switches - All switches - read/snmp - All SNMP data +Filters +======= + +Jinja2 uses a number of filters to transform variables. They can be used to +do anything from upper-case all text to pretty-print JSON objects. They are +used by piping a variable. For example: options|pprint . This will "pretty +print" the options-object: + +{{ options|pprint }} + +See http://jinja.pocoo.org/docs/dev/templates/#builtin-filters for a list +of available filters. + +Loops +===== + +You can easily loop over objects, such as switches, using a for-loop. +Combine this with the "dictsort" filter to get a sorted list. + +{% for switch in objects["public/switches"].switches|dictsort %} + Switch {{ loop.index }} is {{ switch[0] }} or {{ switch[1] }} or full version: {{ switch }} +{% endfor %} + +To avoid having to worry about indices of tuples (e.g.: switch[0]), you can +also use a simpler style: + +{% for key, value in objects["public/ping"].switches|dictsort %} + Switch {{ loop.index }} is {{ key}} with latency4 of {{ value.latency4 }}ms +{% endfor %} + Work flow ========= diff --git a/web/templates/switches.txt b/web/templates/switches.txt index 0cc203b..b710066 100644 --- a/web/templates/switches.txt +++ b/web/templates/switches.txt @@ -1,7 +1,4 @@ -{% for switch in objects["public/switches"].switches %} - {% set mg = objects["read/switches-management"].switches[switch] %} - {% set s = objects["public/switches"].switches[switch] %} - {% if mg.subnet4 -%} - {{ switch }} {{mg.subnet4 }} {{ mg.subnet6 }} {{ mg.mgmt_v4_addr }} {{ mg.mgmt_v6_addr }} {{ mg.traffic_vlan }} {{ s.distro_name }} - {% endif %} +{% for (switch, s) in objects["public/switches"].switches|dictsort %} + {% set mg = objects["read/switches-management"].switches[switch] -%} + {{ switch }} {{mg.subnet4 }} {{ mg.subnet6 }} {{ mg.mgmt_v4_addr }} {{ mg.mgmt_v6_addr }} {{ mg.traffic_vlan }} {{ s.distro_name }} {% endfor %} |