The Artima Developer Community
Sponsored Link

Python Buzz Forum
Kid by Example

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Ryan Tomayko

Posts: 408
Nickname: rtomayko
Registered: Nov, 2004

Ryan Tomayko is an instance of a human being...
Kid by Example Posted: Jan 26, 2005 6:51 PM
Reply to this message Reply

This post originated from an RSS feed registered with Python Buzz by Ryan Tomayko.
Original Post: Kid by Example
Feed Title: Ryan Tomayko (weblog/python)
Feed URL: http://tomayko.com/feed/
Feed Description: Entries classified under Python.
Latest Python Buzz Posts
Latest Python Buzz Posts by Ryan Tomayko
Latest Posts From Ryan Tomayko (weblog/python)

Advertisement

Kid 0.5 is available and I think I finally like it enough to start dumping out some example templates and API usage.

Let's just throw everything at you at once:

<?xml version='1.0' encoding='utf-8'?>
<?python 
# python blocks can be embedded anywhere in a template 
# but cannot output anything directly..

# import some modules
import os, time

# define some functions, too
def percent_of(iz, ove):
    return (iz / ove)

# bind some names..
title = "Hello World"
fruits = ["apple", "orange", "kiwi", "M&M"]
ts = time.gmtime()

?>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:py="http://naeblis.cx/ns/kid#">
<head>
  <!-- 
  curly braces can be used to interpolate python expressions in
  text items and attribute values. 
  -->
  <title>naeblis.cx - {title}</title>
  <meta name="generator" 
        value="kid template v{kid.__version__}" />
</head>
<body>

  <!-- 
  you can also use design-tool friendly attributes 
  to evaluate expressions for element content. 
  -->
  <h1 py:content="title.upper()">
    This text is replaced with the uppercased title..
  </h1>


  <!-- 
  full python expressions let you express yourself fully. :) 
  -->
  <p>We're {round(percent_of(ts.tm_yday, 365.0) * 100, 2)}% 
  through the year.</p>


  <!-- 
  iterate over sequences. the li element is repeated 
  for each item in the list of fruits 
  -->
  <ul>
    <li py:for="fruit in fruits">
      I like {fruit}s..
    </li>
  </ul>


  <!-- control output conditionally -->
  <p py:if="ts.tm_wday == 0">
    Somebody's got the case of the <em>Muundays</em>.. :(
  </p>


  <!-- 
  define template functions that can be imported and 
  called from other templates
  -->
  <table py:def="display_dict(name_title, val_title, mapping)">
    <tr><th>{name_title}</th><th>{val_title}</th></tr>
    <tr py:for="name, val in mapping.values()">
      <td>{name}</td>
      <td>{val}</td>
    </tr>
  </table>

</body>
</html>

The last bit of this template defines a template function, which is a normal python function that generates XML content. Kid templates can be used like normal python modules so let's say we called the previous example example1.kid and right next to it we have the following in example2.kid:

<?xml version="1.0" encoding="utf-8"?>
<?python
# you can treat templates just like python modules
from example1 import display_dict

lil_bears = { 'PHP' : 'Too soft',
              'XSLT' : 'Too hard',
              'Kid' : 'Just right!' }

?>
<html xmlns:py="http://naeblis.cx/ns/kid#">
  <head>
    <title>Let's use that template function</title>
  </head>
  <body>
    <h1>Here's a table of stuff</h1>

    {display_dict('Language', 'Rating', lil_bears)}
  </body>
</html>

The result is that the display_dict() function is called and all generated XML information items are pulled into the calling template at the point the function is called. This results in a two column / three row table being output using the values of the lil_bears mapping we defined earlier.

We can also do some interesting things by passing values from python into the template. In the first example, we bound the name title to the value "Hello World". We can override module level bindings like that by passing names into the template like so:

import example1
result = example1.serialize(title="Goodbye, world!", foo=123)

Here we pass title and foo into the template. Those names are automatically available to template as it executes. I've also passed a foo argument to show that there's no limit on the number or types of arguments that can be passed to templates.

Need to sleep now. Thanks for stopping by...

Read: Kid by Example

Topic: Wax core controls Previous Topic   Next Topic Topic: Use your Magi-Nation

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use