Skip to content

Instantly share code, notes, and snippets.

@c4urself
Created May 19, 2011 08:54
Show Gist options
  • Save c4urself/980431 to your computer and use it in GitHub Desktop.
Save c4urself/980431 to your computer and use it in GitHub Desktop.
make an html tag in python
#tag('ul',''.join([tag('li',i) for i in xrange(1,6)]),{'class':'required'})
#>>>'<ul class="required"><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li></ul>'
def tag(tag_name='div', content='', attrs_dict={}):
"""
Makes an html tag.
"""
template = '<%s>' % ('%s>%s</%s' % ('%s' % (tag_name + '%s'),'%s','%s' % tag_name))
return template % (' '.join([' %s="%s"' %(k,v) for k,v in attrs_dict.iteritems()]), content)
@c4urself
Copy link
Author

Doesn't work with self-closing html tags like "img" btw

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