|
|||||||||||||||||||||
1. Introduction
2. Installing Karrigell 3. The Web server 4. Configuration options 5. Working with Apache, LightTPD or Xitami 6. Programming 7. Debugging 8. Python scripts 9. Karrigell Services 10. Python Inside HTML 10.1 Python variables 10.2 Strings for translation 10.3 Indentation 10.4 PIH as a templating system 10.5 Debugging 11. HTML Inside Python 12. HTMLTags - generate HTML in Python 13. Including documents 14. Sessions 15. Authentication 16. Translation and Unicode |
10. Python Inside HTMLPython Inside HTML behaves much like Microsoft's Active Server Pages, Sun's Java Server Pages and PHP : it's basically a HTML document, in which you insert portions of code written in a programming language - here Python In Python Inside HTML, these portions of code are separated from the HTML code inside special tags : <% and %> Suppose you want to display the current date, you'll mix html and Python code this way :
With a text editor, write the code above and save it under time.pih in your Root Directory. Enter http://localhost/time.pih and see what happens You'll notice that the code inside the 10.1 Python variablesWhen you only want to print a variable's value, instead of<% print var %> you can use the shortcut
<%= var %> :
os module : for convenience, it is already in the namespace
when you execute the script ; so are two other modules,
string and Cookie , because they will probably be
used in many scripts (but of course, if you explicitely write import
string your script will work as well)
10.2 Strings for translationSince internationalization is important in Karrigell, there is a shortcut for the strings or string variables you'll want to be translated according to user preferences : use<%_ string %>
If you have prepared a translation for the string See Karrigell support for internationalization 10.3 IndentationThe result of processing a PIH file is Python code ; this code must be indented. Since a PIH script is a mixture of HTML, in which indentation has no other meaning than readability, and of chunks of Python code, it may be difficult to produce a code that is easily readable and correctly indented 10.3.1 BasicsPythonInsideHTML follows simple rules :
A simple condition example :
and a
Without this A last one with two levels of indentation
Note that after the 1st line the tag must be closed by %>, if not, the indentation after the second line will be only 1 10.3.3 The
For longer or more complex code the repetitive use of |
# indentation |
|
<indent> <% if hour<12: %> Good morning <% elif hour<18: %> Good afternoon <% else: %> Good evening Ladies and Gentlemen </indent> |
# 0 # 0 # 1 # 0 # 1 # 0 # 1 # 0 |
Second one :
# indentation |
|
<table border=1> <tr> <th>Number</th> <th>Square</th> </tr> <indent> <% for i in range(10): %> <tr> <td><% print i %></td> <td><% print i**2 %></td> </tr> </indent> </table> |
# 0 # 0 # 0 # 0 # 0 # 0 # 0 (A) # 1 # 1 # 1 # 1 # next one : 0 # 0 |
On the line noted (A) above you see that the indentation of the line
is relative to the indentation of the <indent>
tag
Also note that after an indented part (after the
</indent>
tag) indentation returns to zero
An example with embedded loops :
<indent> <table border=1> <% for i in ['h']+range(10): %> <tr> <% for j in ['h']+range(10): %> <% if i!='h' and j!='h': %> <td><%= i*j %></td> <% elif i!='h': %> <th><%= i %></th> <% elif j!='h': %> <th><%= j %></th> <% else: %> <td>*</td> </tr> </table> </indent>