mod-python-windows

mod-python-windows Git Source Tree


Root/Doc/ssi.rst

Page rendered in 0.08254s using 11 queries. The lifetime of any global data is for the current request only. If data must persist between requests, it must reside in external modules and as necessary be protected against multithreaded access in the event that a multithreaded Apache MPM is used. .. _ssi-globals: Pre-populating Globals ====================== Any Python code which appears within the page has to be compiled each time the page is accessed before it is executed. In other words, the compiled code is not cached between requests. To limit such recompilation and to avoid duplication of common code amongst many pages, it is preferable to pre-populate the global data from within a mod_python handler prior to the page being processed. In most cases, a fixup handler would be used for this purpose. For this to work, first need to configure Apache so that it knows to call the fixup handler.:: PythonFixupHandler _handlers The implementation of the fixup handler contained in ``_handlers.py`` then needs to create an instance of a Python dictionary, store that in the mod_python request object as ``ssi_globals`` and then populate that dictionary with any data to be available to the Python code executing within the page.:: from mod_python import apache import cgi, time def _escape(object): return cgi.escape(str(object)) def _header(filter): print >> filter, '...' def _footer(filter): print >> filter, '...' def fixuphandler(req): req.ssi_globals = {} req.ssi_globals['time'] = time req.ssi_globals['_escape'] = _escape req.ssi_globals['_header'] = _header req.ssi_globals['_footer'] = _footer return apache.OK This is most useful where it is necessary to insert common information such as headers, footers or menu panes which are dynamically generated into many pages.::
   
   
Page rendered in 0.08254s using 11 queries. .. _ssi-conditionals: Conditional Expressions ======================= SSI allows for some programmability in its own right through the use of conditional expressions. The structure of this conditional construct is::: A test condition can be any sort of logical comparison, either comparing values to one another, or testing the 'truth' of a particular value. The source of variables used in conditional expressions is distinct from the set of global data used by the Python code executed within a page. Instead, the variables are sourced from the ``subprocess_env`` table object contained within the request object. The values of these variables can be set from within a page using the SSI ``'set'`` directive, or by a range of other Apache directives within the Apache configuration files such as ``BrowserMatchNoCase`` and ``SetEnvIf``. To set these variables from within a mod_python handler, the ``subprocess_env`` table object would be manipulated directly through the request object.:: from mod_python import apache def fixuphandler(req): debug = req.get_config().get('PythonDebug', '0') req.subprocess_env['DEBUG'] = debug return apache.OK If being done from within Python code contained within the page itself, the request object would first have to be accessed via the filter object.:: DEBUG ENABLED DEBUG DISABLED Page rendered in 0.08254s using 11 queries. .. _ssi-output-filter: Enabling INCLUDES Filter ======================== SSI is traditionally enabled using the ``AddOutputFilter`` directive in the Apache configuration files. Normally this would be where the request mapped to a static file.:: AddOutputFilter INCLUDES .shtml When mod_python is being used, the ability to dynamically enable output filters for the current request can instead be used. This could be done just for where the request maps to a static file, but may just as easily be carried out where the content of a response is generated dynamically. In either case, to enable SSI for the current request, the :meth:`request.add_output_filter` method of the mod_python request object would be used.:: from mod_python import apache def fixuphandler(req): req.add_output_filter('INCLUDES') return apache.OK

Archive Download this file

Branches

Number of commits:
Page rendered in 0.08254s using 11 queries.