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  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
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? 
Google.com  
PYTHON

Using Spyce
By: Peyton McCullough
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 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:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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
     

       

    PYTHON ARTICLES

    - Tuples and Other Python Object Types
    - The Dictionary Python Object Type
    - String and List Python Object Types
    - Introducing Python Object Types
    - Mobile Programming using PyS60: Advanced UI ...
    - Nested Functions in Python
    - Python Parameters, Functions and Arguments
    - Python Statements and Functions
    - Statements and Iterators in Python
    - Sequences and Sets in Python
    - Python Expressions and Operators
    - Dictionaries, Variables and Statements in Py...
    - Data Types in Python
    - The Python Language
    - SSH with Twisted





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek