Python
  Home arrow Python arrow Page 3 - 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 - Active Tags
    ( Page 3 of 4 )

    Spyce packs a few more features when it comes to embedding code. An interesting feature worth looking at is active tags, which are tags that basically take the place of Python code or other text, passing the code part off to a module. This can cut development time and reduce clutter in Spyce files, among other things. In my opinion, active tags are best understood if you skip the prose details and jump right into an example. First, let's define a few tags in color.spi:

    [[.tagcollection]]

    [[.begin name=red buffers=True]]
    <span style='color: red;'>[[=_content]]</span>
    [[.end]]

    [[.begin name=green buffers=True]]
    <span style='color: darkgreen;'>[[=_content]]</span>
    [[.end]]

    [[.begin name=blue buffers=True]]
    <span style='color: blue;'>[[=_content]]</span>
    [[.end]]

    Before we continue, notice that most of the above Spyce commands come in a “[[.X]]” structure. These are called Spyce directives, and they are used to communicate with Spyce. Here, we use it to declare the file to be a tag collection and then to describe individual tags. Let's now put the above tags to use in colortest.spy:

    [[.taglib from=color.spi as=color]]
    <color:red>Red</color:red><br />
    <color:green>Green</color:green><br />
    <color:blue>Blue</color:blue>

    When you run colortest.spy, active tags should start to make sense. In color.spi, we defined three tags, red, green and blue. Notice how we set buffers to True. This allows us to access the content inside the tags with _content. In colortest.spy, _content is “Red”, “Green” and then “Blue”.

    Active tags can also access attributes. Say, for example, that we want to define a tag that allows a custom color to be set through an attribute. We could add this definition on to color.spi:

    [[.begin name=cust buffers=True]]
    [[.attr name=code default='#000000']]
    <span style='color: [[=code]];'>[[=_content]]</span>
    [[.end]]

    Here's the new tag in action:

    [[.taglib from=color.spi as=color]]
    <color:cust code='goldenrod'>goldenrod</color:cust>

    There are some situations where a tag will not come in pairs. For example, let's say we wanted to add an information tag to color.spi. This is how it would be done:

    [[.begin name=information singleton=True]]
    Color Tag Collection<br />
    red -- displays text in red<br />
    green -- displays text in green<br />
    blue -- displays text in blue<br />
    cust -- displays text in the color specified in the code attribute<br />
    [[.end]]

    It's also possible to create active tags from Python classes. Using this method, we can duplicate color.spi as color.py:

    import spyceTag

    def genColorTag(color):

       class color(spyceTag.spyceTagPlus):

          name = color
          buffer = True

          def syntax(self):
             self.syntaxPairOnly()
          def begin(self):
             self.getOut().write("<span style='color: red;'>")
          def body(self, _content):
             self.getOut().write(_content)
          def end(self):
             self.getOut().write("</span>")

       return color

    red = genColorTag('red')
    green = genColorTag('green')
    blue = genColorTag('blue')

    class cust(spyceTag.spyceTagPlus):

       name = 'cust'
       buffer = True

       def syntax(self):
          self.syntaxPairOnly()
       def body(self, _content):
          if self._attrs.has_key('code'):
             code = self._attrs['code']
          else:
             code = '#000000'
          self.getOut().write("<span style='color: %s;'>%s</span>" %
    (code, _content))

    class information(spyceTag.spyceTagPlus):

       name = 'information'

       def syntax(self):
          self.syntaxSingleOnly()
       def body(self, _content):
          self.getOut().write("Color Tag Collection<br />")
          self.getOut().write("red -- displays text in red<br />")
          self.getOut().write("green -- displays text in
    green<br />")
          self.getOut().write("blue -- displays text in blue<br />")
          self.getOut().write("cust -- displays text in the color
    specified in the code attribute<br />")

    class color(spyceTag.spyceTagLibrary):
       tags = [red, green, blue, cust, information]

    Creating a tag library in Python can take more effort, but it's pretty straight-forward. We start off by generating the classes red, blue and green from the genColorTag function. The classes are subclasses of spyceTag.spyceTagPlus. Notice how we use the syntaxPairOnly method to indicate that the tag should come in a pair. Next, we create the cust tag. In the first three classes, we split the HTML generation between begin, body and end, but in cust, we do it all in body. There's no reason why everything just can't go in body to begin with, but I want to acknowledge the other two methods. Then, we create the information class, making sure to call syntaxSingleOnly. Finally, we create a color class and list all our tags in 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 6 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek