Home arrow Python arrow Page 3 - Using Spyce

Active Tags - Python

If you are looking for a way to embed Python within HTML you might want to take a look at a web environment called Spyce. Two of its advantages are its features and its flexibility. Keep reading to learn how you can Spyce up your programs.

TABLE OF CONTENTS:
  1. Using Spyce
  2. Basic Syntax
  3. Active Tags
  4. Handling Form Data
By: Peyton McCullough
Rating: starstarstarstarstar / 9
January 17, 2006

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PYTHON ARTICLES

- Python Big Data Company Gets DARPA Funding
- Python 32 Now Available
- Final Alpha for Python 3.2 is Released
- Python 3.1: String Formatting
- Python 3.1: Strings and Quotes
- Python 3.1: Programming Basics and Strings
- 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

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: