Don't wrap geometry columns in st_astext, since we're dealing with systems that understand geom columns.
Simplify, since there are always params.
Refactor out the table columns declaration.
Pass the filters for the grids, rather than passing javascript-generated sql.
Create a mapnikplaceholder column type.
Use the placeholder column, rather than string substitution.
Use placeholders to adjust the snap-to-grid size automagically.