### Example 3.1: Class-level Query Templates

Select Layers: State Boundaries County Boundaries Cities Federal/Indigenous Lands Water Features Roads Select Background:
Map Mode: |  | Map Control: | Reference: Legend:
Check the "Cities" layer and click on the "Refresh" button. The map will refresh and the Cities layer should now be displayed. Change the Map Mode to "Query Single Layer" or "Query Multiple Layers" then click on one of the cities. You've just performed a non-spatial query. The result might not make any sense but there is a result. We will format the query results in the next two exercises but for now we'll look at the changes we added to the mapfile and the HTML template. Here's what the cities layer looks like (with some comments removed):
            LAYER # Urban areas polygon layer begins here
              NAME cities_poly
              GROUP cities
              TYPE Polygon
              STATUS on
              DATA urban_ugl

              # The keyword TOLERANCE provides a measure of sensitivity for point-based
              # queries.  In this example, a mouse-click will have a radius of 3 pixels.
              # TOLERANCE has and associated keyword, TOLERANCEUNITS.  When TOLERANCEUNITS
              # isn't defined, MapServer assumes the units to be pixels.
              TOLERANCE 3

              CLASS
                NAME 'Urban Areas'

                # The TEMPLATE keyword within a CLASS object is used to define the
                # HTML query template to use in displaying database records when the
                # query or nquery mode in MapServer is invoked.
                TEMPLATE '../templates/cities_query.html'

                STYLE
                  COLOR 255 240 115
                END
              END  # CLASS

              PROJECTION
                "init=epsg:4326"
              END
            END  # Urban areas polygon layer ends here
As you can see, we only added two parameters on the mapfile--TOLERANCE, within the cities_poly layer, and TEMPLATE, within the CLASS object of the cities_poly layer. And here's what the Query TEMPLATE, cities_query.html, looks like:
            <!-- MapServer Template -->
            <tr>
              <td>[lrn]</td>
              <td>[NAME]</td>
              <td>[STATE]</td>
            </tr>
Important: templates must contain the magic string ‘mapserver template’ in the first line of the template. Often this takes the form of an HTML, javascript or XML comment. This line is not written to the client. The magic string is not case sensitive. See Templating for more information. Note that the template looks just like a fragment of an HTML table. The "[NAME]" and "[STATE]" MapServer tags are actually attributes from urban_ugl.dbf which is the non-spatial database associated with out shapefile. When defining shapefile attributes in MapServer, we always capitalize the names (this is true on shapefile but isn't true in all cases). We've also used this before when we defined CLASSITEM and LABELITEM in section 1. The other tag, "[lrn]", is another internal MapServer variable that displays the result count within a layer. If you used the "Query Multiple Layers", the MapServer CGI program will apply this query template to each record. View the MapFile | View the HTML Template

Back to Section 2 | Back to the Sections Page | Proceed to Example 3.2