freemarker.rst 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. .. _tutorial_freemarkertemplate:
  2. Freemarker Templates
  3. ====================
  4. Introduction
  5. ------------
  6. This tutorial will introduce you to a more in depth view of what FreeMarker templates are and how you can use the data provided to templates by GeoServer.
  7. `Freemarker <http://www.freemarker.org/>`_ is a simple yet powerful template engine that GeoServer uses to allow user customization of outputs. In particular, at the time of writing it's used to allow customization of GetFeatureInfo, GeoRSS and KML outputs.
  8. Freemarker allows for simple variable expansions, as in ``${myVarName}``, expansion of nested properties, such as in ``${feature.myAtt.value}``, up to little programs using loops, ifs and variables.
  9. Most of the relevant information about how to approach template writing is included in the Freemarker's `Designer guide <http://www.freemarker.org/docs/dgui.html>`_ and won't be repeated here: the guide, along with the :ref:`getutorial_kmlplacemark` and :ref:`tutorials_getfeatureinfo` tutorials should be good enough to give you a good grip on how a template is built.
  10. Template Lookup
  11. ```````````````
  12. GeoServer looks up templates in three different places, allowing for various levels of customization. For example given the ``content.ftl`` template used to generate WMS GetFeatureInfo content:
  13. * Look into ``GEOSERVER_DATA_DIR/workspaces/<workspace>/<datastore>/<featuretype>/content.ftl`` to see if there is a feature type specific template
  14. * Look into ``GEOSERVER_DATA_DIR/workspaces/<workspace>/<datastore>/content.ftl`` to see if there is a store specific template
  15. * Look into ``GEOSERVER_DATA_DIR/workspaces/<workspace>/content.ftl`` to see if there is a workspace specific template
  16. * Look into ``GEOSERVER_DATA_DIR/workspaces/content.ftl`` looking for a global override
  17. * Look into ``GEOSERVER_DATA_DIR/templates/content.ftl`` looking for a global override
  18. * Look into the GeoServer classpath and load the default template
  19. Each templated output format tutorial should provide you with the template names, and state whether the templates can be type specific, or not. If you can't find the source files for the default template, look in the service jar of the geoserver distribution (for example, wms-x.y.z.jar). You just have to unzip it, and you'll find the xxx.ftl files GeoServer is using as the default templates.
  20. Common Data Models
  21. ``````````````````
  22. Freemarker calls "data model" the set of data provided to the template. Each output format used by GeoServer will inject a different data model according to the information it's managing. There are three very common elements that appear in almost every template: Feature, FeatureType and FeatureCollection. Here we provide a data model of each.
  23. The data model is a sort of tree, where each element has a name and a type. Besides basic types, we'll use:
  24. * list: a flat list of items that you can scan thru using the FreeMarker ``<#list>`` directive;
  25. * map: a key/value map, that you usually access using the dot notation, as in ``${myMap.myKey``}, and can be nested;
  26. * listMap: a special construct that is, at the same time, a Map, and a list of the values.
  27. Here are the data models (as you can see there are redundancies, in particular in attributes, we chose this approach to make template building easier):
  28. **FeatureType (map)**
  29. * name (string): the type name
  30. * attributes (listMap): the type attributes
  31. * name (string): attribute name
  32. * namespace (string): attribute namespace URI
  33. * prefix (string): attribute namespace prefix
  34. * type (string): attribute type, the fully qualified Java class name
  35. * isGeometry (boolean): true if the attribute is geometric, false otherwise
  36. **Feature (map)**
  37. * fid (string): the feature ID (WFS feature id)
  38. * typeName (string): the type name
  39. * attributes (listMap): the list of attributes (both data and metadata)
  40. * name (string): attribute name
  41. * namespace (string): attribute namespace URI
  42. * prefix (string): attribute namespace prefix
  43. * isGeometry (boolean): true if the attribute is geometric, false otherwise
  44. * value: a string representation of the attribute value
  45. * isComplex (boolean): true if the attribute is a feature (see :ref:`app-schema.complex-features`), false otherwise
  46. * type (string or FeatureType): attribute type: if isComplex is false, the fully qualified Java class name; if isComplex is true, a FeatureType
  47. * rawValue: the actual attribute value (is isComplex is true rawValue is a Feature)
  48. * type (map)
  49. * name (string): the type name (same as typeName)
  50. * namespace (string): attribute namespace URI
  51. * prefix (string): attribute namespace prefix
  52. * title (string): The title configured in the admin console
  53. * abstract (string): The abstract for the type
  54. * description (string): The description for the type
  55. * keywords (list): The keywords for the type
  56. * metadataLinks (list): The metadata URLs for the type
  57. * SRS (string): The layer's SRS
  58. * nativeCRS (string): The layer's coordinate reference system as WKT
  59. **FeatureCollection (map)**
  60. * features (list of Feature, see above)
  61. * type (FeatureType, see above)
  62. **request (map)**
  63. Contains the GetFeatureInfo request parameters and related values.
  64. **environment (map)**
  65. Allows accessing several environment variables, in particular those defined in:
  66. * JVM system properties
  67. * OS environment variables
  68. * web.xml context parameters
  69. **Math (map)**
  70. Allows accessing math functions.
  71. Examples
  72. ``````````````````
  73. **request**
  74. * ${request.LAYERS}
  75. * ${request.ENV.PROPERTY}
  76. **environment**
  77. * ${environment.GEOSERVER_DATA_DIR}
  78. * ${environment.WEB_SITE_URL}
  79. **Math**
  80. * ${Math.max(request.NUMBER1,request.NUMBER2)}