# Filters

Filters are one of the most important features of CherryPy. The CherryPy core can call user-defined functions at specific points during request processing; a filter is a class which defines those functions. Filters are designed to be called at a low level—the HTTP request/response level—and therefore should only be used in that context.

CherryPy comes with a set of built-in filters, but they're turned off by default. To enable them, you must use the configuration system as follows:

- First you must decide where to enable the filter. CherryPy maintains a tree of published objects; you must decide which branch should use the filter. The filter will then apply to that branch and all its children in the tree. Remember that the tree is accessed as a path and then mapped internally by the core to match the correct exposed object.
- Second in the config file you must turn the filter on like this : `filterName.on = True`

Turning on a default filter

            [/entries/view]
            tidyFilter.on = True
            tidyFilter.tmpDir = "/tmp"
            tidyFilter.strictXml = True

On the first line we define that the tidy filter will be used by the core whenever the path `/entries/view` (or one of its sub-paths) is called. On the two last lines we also define some parameters used by the filter.

CherryPy lets you write your own filters as we will see in the developer reference chapter. However, the way to use them is different from the default filters. You do not declare custom filters within the configuration file; instead, use the `_cpFilterList` attribute in your source code:

Using a non default filter

            import cherrypy
            from myfiltermodule import MyFilterClass

            class Entry:
              _cpFilterList = [ MyFilterClass() ]
              def view(self, id):
                # do suff...
              view.exposed = True

            class Root: pass

            cherrypy.root = Root()
            cherrypy.root.entries = Entry()
            cherrypy.server.start()

As all objects below `cherrypy.root.entries` will inherit the filter, there is no need to re-specify it in each `_cpFilterList` underneath.

Keep in mind that the user-defined filters are called in the order you add them to the list.