def html_options(env, **kwds): if not kwds: return u'' opts = [] for n, v in kwds.iteritems(): opts.append(u'%s="%s"' % (jinja2.escape(n), jinja2.escape(v))) result = u' %s ' % u' '.join(opts) return to_markup(env, result)
def link(env, name, path, **kwds): options = dict([(k[1:], kwds.pop(k)) for k in kwds.keys() if k.startswith('_')]) options_str = link_options(env, **options) if options else u'' if not path.startswith('/') and not path.startswith('http'): path = url(path, **kwds) result = '<a href="%s"%s>%s</a>' % ( jinja2.escape(path), options_str, jinja2.escape(name)) return to_markup(env, result)
def mail_tag(env, mail, encode=None, **kwds): ### from symfony options = { 'cc': None, 'bcc': None, 'subject': None, 'body': None, } options.update(kwds) name = mail extras = [] htmloptions = [] for key, value in options.iteritems(): if not value: continue elif 'cc' == key or 'bcc' == key: value = value.strip() if value: value = url_quote_plus(value) value = value.replace('+', '%20') value = value.replace('%40', '@') value = value.replace('%2C', ',') extras.append('%s=%s' % (key, value)) elif 'body' == key or 'subject' == key: value = to_str(value).strip() if value: value = url_quote_plus(value) value = value.replace('+', '%20') extras.append('%s=%s' % (key, value)) elif 'name' == key: name = value else: htmloptions.append('%s=%s' % (jinja2.escape(key), jinja2.escape(value))) extras = ('?' + '&'.join(extras)) if extras else '' htmloptions = (' ' + ' '.join(htmloptions)) if htmloptions else '' if encode is None: e_mail = jinja2.escape(mail) e_name = jinja2.escape(name) result = '<a href="mailto:%s%s"%s>%s</a>' % (e_mail, extras, htmloptions, e_name) else: o_mail = obfuscate(env, 'mailto:%s' % mail) o_name = obfuscate(env, name) result = '<a href="%s%s"%s>%s</a>' % (o_mail, extras, htmloptions, o_name) if 'js' == encode: o_str = obfuscate(env, 'document.write(\'%s\');' % result, js=True) result = '<script type="text/javascript">eval(unescape(\'%s\'));</script>' % o_str return to_markup(env, result)
def input_tag(env, typ, name, value='', **kwds): value = _form_get(name, value) e_name = jinja2.escape(name) e_typ = jinja2.escape(typ) e_value = jinja2.escape(value) tag_id = kwds.pop('id', None) or ('form_' + e_name) options = html_options(env, **kwds) if 'textarea' == typ: result = u'<textarea id="%s" name="%s" %s >%s</textarea>' % ( tag_id, e_name, options, e_value) else: result = u'<input id="%s" type="%s" name="%s" value="%s" %s />' % ( tag_id, e_typ, e_name, e_value, options) return to_markup(env, result)
def image_tag(env, src, **kwds): if not src: return '' if not src.startswith('/') and not src.startswith('http://') and\ not src.startswith('https://'): src = current_app.static_dir + src result = u'<img src="%s"%s />' % (jinja2.escape(src), html_options(env, **kwds)) return to_markup(env, result)
def check_or_radio_tag_one(env, typ, name, choices, value=None): elem, choice = choices elem = to_unicode(elem) choice = to_unicode(choice) value = _to_form_list(value) tag_id = u'%s-%s' % (name, elem) params = {'id': tag_id} if elem in value: params['checked'] = u'checked' input = input_tag(env, typ, name, elem, **params) label_text = jinja2.Markup(jinja2.Markup(input) + jinja2.escape(choice)) label = label_tag(env, tag_id, label_text, **{'class': typ}) return to_markup(env, label)
def select_tag(env, name, choices, value=None, blank=True, **kwds): value = _form_get_list(name, value) e_name = jinja2.escape(name) options = html_options(env, **kwds) output = [u'<select id="%s" name="%s"%s>' % (e_name, e_name, options)] if blank: output.append(u' <option value=""></option>') choices = choices or [] for elem, choice in _iter_choices(choices): elem = to_unicode(elem) choice = to_unicode(choice) e_elem = jinja2.escape(elem) e_choice = jinja2.escape(choice) selected_html = u' selected="selected"' if elem in value else u'' output.append(u' <option value="%s"%s>%s</option>' % ( e_elem, selected_html, e_choice)) output.append(u'</select>') result = u'\n'.join(output) return to_markup(env, result)
def link_if(env, condition, name, *args, **kwds): if condition: return link(env, name, *args, **kwds) result = '<span>%s</span>' % jinja2.escape(name) return to_markup(env, result)
def label_tag(env, id, value, **kwds): e_id = jinja2.escape(id) e_value = jinja2.escape(value) options = html_options(env, **kwds) result = u'<label for="%s" %s>%s</label>' % (e_id, options, e_value) return to_markup(env, result)