Insert static files literally into Jinja templates without parsing them

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.


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') }}

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


Here's a solution in the form of a tag, which looks more like the standard "include" when you're writing templates. (This is how I do it for a static site generator called Combine.)

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
        template = parser.parse_expression()
        result = self.call_method("_render", [template], 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" %}

Tags:

Python

Jinja2