美文网首页
500 Lines Series

500 Lines Series

作者: 唐瀚 | 来源:发表于2018-02-11 18:02 被阅读0次

    A Template Engine

    A template engine automatically generate codes by introducing programming flow-control method.

    In an interpretation model, parsing produces a data structure representing the structure of the template. The rendering phase walks that data structure, assembling the result text based on the instructions it finds. For a real-world example, the Django template engine uses this approach.
    In a compilation model, parsing produces some form of directly executable code. The rendering phase executes that code, producing the result. Jinja2 and Mako are two examples of template engines that use the compilation approach.
    Our implementation of the engine uses compilation: we compile the template into Python code. When run, the Python code assembles the result.

    1. Faster performance, by using shortcuts
    # The way we're used to seeing it:
    result.append("hello")
    
    # But this works the same:
    append_result = result.append
    append_result("hello")
    
    1. Use "*" to control para
    self.code.extend([" " * self.indent_level, line, "\n"])
    

    相关文章

      网友评论

          本文标题:500 Lines Series

          本文链接:https://www.haomeiwen.com/subject/azqltftx.html