Insert static files literally into Jinja templates without parsing them

前端 未结 5 1735
逝去的感伤
逝去的感伤 2020-12-15 07:09

I\'m trying to insert file into a page using Jinja 2.6 using the include tag. This worked fine until I started using characters in the file that are reminiscent

相关标签:
5条回答
  • 2020-12-15 07:17

    As an update to @Alex's answer, you can use Jinja's @contextfunction decorator to remove some of the reliance on global variables. The updated code would look like:

    import jinja2                                                                                                                                                                                                  
    
    @jinja.contextfunction                                                                                                                                                                                         
    def include_file(ctx, name):                                                                                                                                                                                   
        env = ctx.environment                                                                                                                                                                                      
        return jinja2.Markup(env.loader.get_source(env, name)[0])                                                                                                                                                  
    
    
    def main():                                                                                                                                                                                                    
        loader = jinja2.PackageLoader(__name__, 'templates')                                                                                                                                                       
        env = jinja2.Environment(loader=loader)                                                                                                                                                                    
        env.globals['include_file'] = include_file                                                                                                                                                                 
    
        env.get_template('page.html').render()                                                                                                                                                                     
    
    
    if __name__ == '__main__':                                                                                                                                                                                     
        print main()
    

    And just as before, call it from your template like:

    {{ include_file('file.txt') }}
    
    0 讨论(0)
  • 2020-12-15 07:20

    Try putting the syntax in the other files in {% raw %} {% endraw %}

    You can use jQuery if you dont want to edit the external files: Make a dive to contain the content <div id="contentoffile"></div>

    and use jquery to load the file : $("#contentoffile").load("url to file") << the url can be relative

    0 讨论(0)
  • 2020-12-15 07:21

    Here's a solution in the form of a tag, which looks more like the standard "include" when you're writing templates.

    from jinja2 import nodes
    from jinja2.ext import Extension
    from jinja2 import Markup
    
    
    class IncludeRawExtension(Extension):
        tags = {"include_raw"}
    
        def parse(self, parser):
            lineno = parser.stream.expect("name:include_raw").lineno
            filename = nodes.Const(parser.parse_expression().value)
            result = self.call_method("_render", [filename], lineno=lineno)
            return nodes.Output([result], lineno=lineno)
    
        def _render(self, filename):
            return Markup(self.environment.loader.get_source(self.environment, filename)[0])
    
    # Use the extension when setting up Jinja
    your_jinja_env = jinja2.Environment(
        extensions=[IncludeRawExtension],
    )
    

    Functionally it's the same as other answers here, but I think it helps to keep the same {% syntax as the regular "include" tag.

    {% include "template.html" %}
    {% include_raw "template.html" %}
    
    0 讨论(0)
  • 2020-12-15 07:32

    You can define a function to load the text file and render it in the template:

    import jinja2
    
    def include_file(name):
        return jinja2.Markup(loader.get_source(env, name)[0])
    
    loader = jinja2.PackageLoader(__name__, 'templates')
    env = jinja2.Environment(loader=loader)
    env.globals['include_file'] = include_file
    
    def render():
        return env.get_template('page.html').render()
    
    if __name__ == '__main__':
        print render()
    

    In the template, call it like this:

    {{ include_file('file.txt') }}
    
    0 讨论(0)
  • 2020-12-15 07:34

    If you are using Flask it can be written like this:

    from jinja2 import Markup
    
    ...
    
    app.jinja_env.globals['include_raw'] = lambda filename : Markup(app.jinja_loader.get_source(app.jinja_env, filename)[0])
    

    And used like this:

    {{ include_raw('js-inline/modernizr.min.js') }}
    

    Path of the included file is relative to your template folder as for normal includes.

    0 讨论(0)
提交回复
热议问题