Adding AJAX to Grok with KSS ============================ :Author: kteague If you want to use Ajax functionality inside a Grok application with an approach that insulates you as much as possible from coding in Javascript, then KSS (kinetic style sheets) is for you. KSS uses an interpreter, implemented with Javascript, that runs the KSS rules. KSS rules themselves look very much like CSS (Cascading style sheets) rules. So a web developer familiar with CSS feels quite comfortable with the KSS syntax. KSS is an independent project which can be used with different web frameworks. You can find information, documentation and tutorial about KSS on: http://kssproject.org Create a sample KSS project +++++++++++++++++++++++++++ Create a buildout for your sample kss application with grokproject:: grokproject ksssample Edit the Configuration file +++++++++++++++++++++++++++ Add megrok.kss to setup.py: .. code-block:: python ... install_requires=['setuptools', 'grok', 'megrok.kss', # <--- ], ... Now run `buildout` with: .. code-block:: bash $ bin/buildout Introductory Example -------------------- Lets first design a very simple example. On a simple web page, add a `div` with text inside. We want that when the user clicks inside this div, its text is changed into something which comes from the server. This is the classic Ajax server request example. To begin with this example, we first have to prepare the template to make the page KSS aware: You add the reference to KSS engine Javascript files in your application template `index.pt`. (grokproject should have created it in the `app_templates` subdirectory of src/ksssample.) In the `
` section of the template, include the following lines: .. code-block:: htmlME GROK KISSED !
') Now, you can run your application and test whether KSS is working by clicking the `div` and seeing whether the text is replaced by `ME GROK KISSED`, which should occur without the page having to be refreshed! Client-side action ------------------ In some cases, modifications of the UI do not need to notify or get information from the server. KSS offers client-side actions: `action-client` in the KSS rules. Let's build an example where clicking on a tag toggles on or off the presence of a class on the given tag (and of its CSS styling). Add the needed markup in the application template: .. code-block:: html