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  
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 
Actuate Whitepapers 
VeriSign Whitepapers 
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

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    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


       · 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 3 hosted by Hostway