Python
  Home arrow Python arrow Page 2 - Using Spyce
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Moblin 
JMSL Numerical Library 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PYTHON

Using Spyce
By: Peyton McCullough
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 9
    2006-01-17

    Table of Contents:
  • Using Spyce
  • Basic Syntax
  • Active Tags
  • Handling Form Data

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Using Spyce - Basic Syntax


    (Page 2 of 4 )

    Let's start with a form example to learn the basics of Spyce. Create a file called form.spy and place it in either Spyce's www directory if you are using the Spyce web server or somewhere in Apache if you are not. We'll start off with some simple HTML to describe the layout of the form:

    <html>
       <head>
          <title>Profile</title>
       </head>
       <body>
           <form method='POST'>
           First Name: <input type='text' name='first' /><br />
           Last Name: <input type='text' name='last' /><br />
           E-Mail: <input type='text' name='email' /><br />
           <input type='submit' value='Submit Information' />
           </form>
       </body>
       </head>
    </html>

    At the basis of your Spyce documents, assuming that you are using Spyce for embedding Python within HTML (it can be used for other purposes), will be your plain old vanilla HTML document. With Spyce, however, we are able to add functionality to the mix by using special tags. Let's add a Spyce comment to the file:

    [[-- Just a simple profile/personal information form --]]
    <html>
       <head>
          <title>Profile</title>
       </head>
       <body>
           <form method='POST'>
           First Name: <input type='text' name='first' /><br />
           Last Name: <input type='text' name='last' /><br />
           E-Mail: <input type='text' name='email' /><br />
           <input type='submit' value='Submit Information' />
           </form>
       </body>
       </head>
    </html>

    If you request the file, you'll see that the comment is not displayed. Obviously, this is the proper behavior of comments. Next come statements, which, as their name suggests, are made of Python statements. Brackets can be used where indentation afterward would normally be required, such as a conditional statement. We'll use a conditional statement to determine whether the form has been submitted or not:

    [[-- Just a simple profile/personal information form --]]
    [[ if request.post('first', None): { ]]
    [[-- The form has been submitted --]]
    [[ } else: { ]]
    <html>
       <head>
          <title>Profile</title>
       </head>
       <body>
           <form method='POST'>
           First Name: <input type='text' name='first' /><br />
           Last Name: <input type='text' name='last' /><br />
           E-Mail: <input type='text' name='email' /><br />
           <input type='submit' value='Submit Information' />
           </form>
       </body>
       </head>
    </html>
    [[ } ]]

    We make use of the request module here, which can be accessed from every Spyce script. We use post to check whether a variable named first has been passed via the “POST” method. If it has, the value is returned inside a list, which equates to True. If it hasn't, we set the default as None, which equates to False.

    Next up are Python chunks, which simply contain sections of Python code. We'll use one in our script to set variables for the form data:

    [[-- Just a simple profile/personal information form --]]
    [[ if request.post('first', None): { ]]
    [[\
    first = request.post('first')[0]
    last = request.post('last')[0]
    name = first + ' ' + last
    email = request.post('email')[0]
    ]]
    [[ } else: { ]]
    <html>
       <head>
          <title>Profile</title>
       </head>
       <body>
           <form method='POST'>
           First Name: <input type='text' name='first' /><br />
           Last Name: <input type='text' name='last' /><br />
           E-Mail: <input type='text' name='email' /><br />
           <input type='submit' value='Submit Information' />
           </form>
       </body>
       </head>
    </html>
    [[ } ]]

    The last instruction we'll use in our script is an expression, which simply displays the end result of a particular Python expression. We'll use it to display the information back to the user:

    [[-- Just a simple profile/personal information form --]]
    [[ if request.post('first', None): { ]]
    [[\
    first = request.post('first')[0]
    last = request.post('last')[0]
    name = first + ' ' + last
    email = request.post('email')[0]
    ]]
    Name: [[=name]]<br />
    E-Mail: [[=email]]
    [[ } else: { ]]
    <html>
       <head>
          <title>Profile</title>
       </head>
       <body>
           <form method='POST'>
           First Name: <input type='text' name='first' /><br />
           Last Name: <input type='text' name='last' /><br />
           E-Mail: <input type='text' name='email' /><br />
           <input type='submit' value='Submit Information' />
           </form>
       </body>
       </head>
    </html>
    [[ } ]]

    Our script is now complete. Try running and modifying it.

    More Python Articles
    More By Peyton McCullough


       · Suprise, suprise, this article covers Spyce. While some of the basics are covered,...
       · From what I saw I am not sure if would use it. Having all those brackets to encase...
       · Great article, I am looking for a powerful, easy-to-install alternative to Perl and...
       · Jumped the gun - the if-else example isn't intended to be run without some code in...
       · I was about to go with Webware and Cheetah but I found Spyce in the nick of time. ...
       · Having some experience from PHP I have found python by 'accident' and I fall in love...
     

       

    PYTHON ARTICLES

    - SSH with Twisted
    - Mobile Programming in Python using PyS60: UI...
    - Python: Count on It
    - Python Strings: Spinning Yarns
    - Python: More Fun with Strings
    - Python: Stringing You Along
    - Python Operators
    - Bluetooth Programming in Python: Network Pro...
    - Python Sets
    - Python Conditionals, Lists, Dictionaries, an...
    - Python: Input and Variables
    - Introduction to Python Programming
    - Mobile Programming in Python using PyS60: Ge...
    - Bluetooth Programming using Python
    - Finishing the PyMailGUI Client: User Help To...





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway