spyce
         
home     documentation     download     Spyce logo


Examples

examples/cookie.spy
[[.import name=cookie]]
<html><body>
  Managing cookies is simple. Use the following forms 
  to create and destroy cookies. Remember to refresh 
  once, because the cookie will only be transmitted on 
  the <i>following</i> request.<br>
  [[-- input forms --]]
  <hr>
  <form action="[[=request.uri('path')]]" method=post>
    <table><tr>
      <td align=right>Cookie name:</td>
      <td><input type=text name=name></td>
      <td>(required)</td>
    </tr><tr>
      <td align=right>value:</td>
      <td><input type=text name=value></td>
      <td>(required for set)</td>
    </tr><tr>
      <td align=right>expiration:</td>
      <td><input type=text name=exp> seconds.</td>
      <td>(optional)</td>
    </tr><tr>
      <td colspan=3>
        <input type=submit name=operation value=set>
        <input type=submit name=operation value=delete>
        <input type=submit name=operation value=refresh>
      </td>
    </tr></table>
  </form>
  <hr>
  [[-- show cookies --]]
  Cookies: [[=len(cookie.get().keys())]]<br>
  <table>
    <tr>
      <td><b>name</b></td>
      <td><b>value</b></td>
    </tr>
    [[for c in cookie.get().keys(): {]]
      <tr>
        <td>[[=c]]</td>
        <td>[[=cookie.get(c)]]</td>
      </tr>
    [[ } ]]
  </table>
  [[-- set cookies --]]
  [[\
    operation = request.post('operation')
    if operation:
      operation = operation[0]
      name = request.post('name')[0]
      value = request.post('value')[0]
      if operation == 'set' and name and value:
        cookie.set(name, value)
      if operation == 'delete' and name:
        cookie.delete(name)
  ]]
</body></html>
Run this code

Back to List of Examples


Spyce logo
Python Server Pages
version 2.1.3
Spyce Powered SourceForge Logo