How to make Web element generation easier analysis in Python

Source: Internet
Author: User
Tags html header

1. Reference CSS. This is probably the most common practice. It defines a specific style for some specific elements. To use it, you need
Add the <link> label to the page.
2. Introduce Js. Many special effects can also be processed using JavaScript, such as dynamic display effects or element encapsulation. Use
You need to add the <SCRIPT> tag to the HTML page, and add some javascript when necessary. Code .
3. HTML elements. You need to set some special attributes, such as class = a certain attribute. This is relatively simple.

Therefore, from the analysis above, we can see that adding a nice Web element may be modified in many places. Therefore, I
I am thinking about how to simplify this process. The trouble is how to deal with these resources and how to make these resources
What about the combination of HTML? The final solution I came up with was code assembly.

For CSS, JavaScript links, and code, they can be assembled into a piece of text in the order of calling, and then inserted before the . Then, the HTML code is output directly in the template. For CSS, the Javascript link can check whether the link is repeated.

So how to define the Web element class and how to process it in the template?

A Web element class is defined as follows:

Class snippet (object ):
CSS =''
Csslink =''
Jslink =''
Html =''
JS =''

Def render (Self ):
Return''

Def _ STR _ (Self ):
Return self. Render ()

If it is defined as a class property, it will be output to the HTML header, and the result of render () will be displayed in the place where the class is called in the template. First take a look at the Template
For example:

<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en"
Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd>
<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Title> HTML helper </title>
<SCRIPT type = "text/JavaScript" src = "/static/JS/jquery. js"> </SCRIPT>
</Head>
<Body>
{{
Htmlbuf }}
</Body>
</Html>

Here you can see what htmlbuf is, which is used to collect class attributes of each snippet class. <
Add the snippet object to htmlbuf. At the same time, it will output the snippet HTML code at the call position.

How to output? First, the uliweb template will be converted into Python code. It has a built-in out object that can be called to output
HTML code. The htmlbuf object will be created before the template is called and processed after the template is called. The write
Attribute. All of this is implemented through the plugin method in simpleframe. py, but this only defines a call point, such:

Fname, code = template. render_file (filename, vars, ENV, dirs)
Out = template. Out ()
Template. _ prepare_run (vars, ENV, out)
Callplugin (self, 'before _ render_template ', ENV, out)

If isinstance (Code, (STR, Unicode )):
Code = compile (Code, fname, 'exec ')
Exec code in env, vars
TEXT = out. getvalue ()
Output = execplugin (self, 'after _ render_template ', text, vars, ENV)

Before_render_template will be called before the template is called. After_render_template will be called after the template is called. Because
You can use the plugin mechanism to add additional processing. This is defined in settings. py, for example:

@ Plugin ('before _ render_template ')
Def before_render_template (sender, ENV, out ):
From uliweb. Core import JS
From uliweb. Core. simpleframe import url_for
From uliweb. helpers import htmlwidgets

Htmlbuf = Js. htmlbuf (write = out. noescape, static_suffix = url_for ('portal. Views. Static ',
Filename = ''))
Env ['htmlbuf'] = htmlbuf
Env ['htmlwidgets'] = htmlwidgets

Htmlbuf and htmlwidgets are injected to the template's ENV Environment, so they can be directly used in the template. In htmlwidgets
Defined some snippet. The out. noescape method is used when htmlbuf is created, and the code in snippet is not converted.
. Static_suffix indicates the prefix of the static file. The default value is/static/. Because the static service is used
To obtain the static URL prefix.

@ Plugin ('after _ render_template ')
Def after_render_template (sender, text, vars, ENV ):
Import re
R_links = Re. Compile ('<link \ s .*? \ Shref \ s * = \ s *"? (.*?) ["\ S>] | <SCRIPT \ s .*? \ SSRC \ s * = \ s *"?
(.*?) ["\ S>] ', re. I)
If 'htmlbuf' in env:
Htmlbuf = env ['htmlbuf']
If htmlbuf. modified:
B = Re. Search ('(? I) If B:
Pos = B. Start ()
# Find links
Links = [X or Y for X, Y in r_links.findall (Text [: POS])]
Htmlbuf. remove_links (LINKS)
T = htmlbuf. Render ()
If t:
Return ''. Join ([Text [: POS], T, text [pos:])
Else:
Return T + Text
Return text

Here, the In this example, the existing link in the original HTML is determined. If yes, the link is deleted. This is handled through remove_links.
.

After such processing, you only need to define a snippet, and uliweb will automatically process CSS for you. js links include code and HTML code.
. Therefore, you can simply:

{{
Htmlbuf }}

Generates a message.

I will gradually expand this htmlwidgets library.

Briefly describe how to configure:

1. In settings. py

Installed_apps = ['documents', 'examples ', 'portal', 'post ',
'Uliweb. builtins. auth', 'uliweb.helpers.html widgets']

Add 'uliweb.helpers.html widgets' to make the static directory take effect.

2. Add:

@ Plugin ('before _ render_template ')
Def before_render_template (sender, ENV, out ):

And

@ Plugin ('after _ render_template ')
Def after_render_template (sender, text, vars, ENV ):

3. You can use it.

Related Article

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.