Skip to content

Instantly share code, notes, and snippets.

@tmeissner
Created December 16, 2012 23:34
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tmeissner/4314218 to your computer and use it in GitHub Desktop.
Save tmeissner/4314218 to your computer and use it in GitHub Desktop.
Yet another template engine: a collection of simple functions to generate HTML content (taken from 'Head first Python' and expanded)
def start_response(resp="text/html"):
return('Content-type: ' + resp + '\n\n')
def html_header(title="default"):
return("""
<head>
<meta charset="utf-8" />
<title>""" + title + """</title>
</head>"""
)
def start_form(the_url, form_type="POST"):
return('<form action="' + the_url + '" method="' + form_type + '">')
def end_form(submit_msg="Submit"):
return('<p></p><input type=submit value="' + submit_msg + '"></form>')
def radio_button(rb_name, rb_value):
return('<input type="radio" name="' + rb_name +
'" value="' + rb_value + '"> ' + rb_value + '<br />')
def text_field(tf_name, tf_value, tf_size=30, tf_maxlength=30):
return('<input type="text" name="' + tf_name + '" value="' + tf_value +
'" size="' + tf_size + '" maxlength="' + tf_maxlength + '" >')
def u_list(items):
u_string = '<ul>'
for item in items:
u_string += '<li>' + item + '</li>'
u_string += '</ul>'
return(u_string)
def header(header_text, header_level=2):
return('<h' + str(header_level) + '>' + header_text +
'</h' + str(header_level) + '>')
def para(para_text):
return('<p>' + para_text + '</p>')
def include_footer(the_links):
link_string = ''
for key in the_links:
link_string += '<a href="' + the_links[key] + '">' + key + '</a>&nbsp;&nbsp;&nbsp;&nbsp;'
return(link_string)
@yfe404
Copy link

yfe404 commented Mar 9, 2015

I'm currently working on this chapter ; ) Thanks !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment