Questions about automatic HTML escaping for webpy templates

Source: Internet
Author: User

Note: web.py any variables used will be escaped, so when you name set the value to be a piece of HTML, it will be escaped and displayed as plain text. If you want to turn off this option, you can write it $:name instead $name .

What if we want to move it partially?

Webpy obviously provides a transfer function that we can call directly on the application layer.

From web.net import htmlquote

Htmlquote (Raw_text)

-------------------------------

  1. #/usr/bin/python
  2. #coding =utf-8
  3. Import htmlparser
  4. Import sys
  5. Reload(sys)
  6. SYS. Setdefaultencoding(' utf-8 ')
  7. Html_parser = htmlparser. Htmlparser()
  8. Title = ' Eclipse features <template> learning. E.g: quickly insert timestamp in code-segmentfault '
  9. Newtitle = html_parser. Unescape(title)
  10. Print Newtitle
  11. You need to load the SYS module, reset the default encoding to UTF8, and there will be no error. However, the content to be processed is only the title part of an article, and the usual HTML escape content is as follows
  12. It was decided to implement a simple escape function using Python's replace function, as follows:

    1. #/usr/bin/python
    2. #coding =utf-8
    3. def replace_html(s):
    4. s = s. Replace(' " ' ,' "')
    5. s = s. Replace(' & ' ,' & ')
    6. s = s. Replace(' &lt; ' ,' < ')
    7. s = s. Replace(' &gt; ' ,' > ')
    8. s = s. Replace(' &nbsp; ' ,")
    9. s = s. Replace('-361way.com ',')
    10. print s
    11. Replace_html(title)

    The advantage is that it is fast and concise, does not depend on the module, and does not need to specify the default encoding reload SYS module.

Questions about automatic HTML escaping for webpy templates

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.