Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/ktravis/temple.js
A micro-library for partial template loading with AJAX.
https://github.com/ktravis/temple.js
Last synced: about 2 months ago
JSON representation
A micro-library for partial template loading with AJAX.
- Host: GitHub
- URL: https://github.com/ktravis/temple.js
- Owner: ktravis
- License: mit
- Created: 2014-01-20T03:23:02.000Z (almost 11 years ago)
- Default Branch: master
- Last Pushed: 2014-01-21T21:33:43.000Z (almost 11 years ago)
- Last Synced: 2023-03-25T11:18:06.695Z (almost 2 years ago)
- Language: JavaScript
- Homepage:
- Size: 141 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
temple.js
=========A micro-library for partial template loading with AJAX.
Depends on jQuery, and some simple backend sorcery.
serving needs
-------------There are many ways to accomplish this, I chose to check for a 'stub' flag in request data sent to relevant views, prior to rendering the template. This manifest as a custom decorator I added on to Flask routes.
def stubbable(f):
@wraps(f)
def decorated_function(*args, **kwargs):
ctx = f(*args, **kwargs)
if ctx is None:
ctx = {}
elif isinstance(ctx, dict):
ctx['stub'] = request.args.get('stub', False)
return ctx
return decorated_functionUse looks like this:
@app.route('/blog')
@templated('blog.html') # a decorator that accepts a context and returns a rendered template
@stubbable
def blog_view():
...
return { ... } # note that the custom decorator must intercept the context
# before the template is rendered
Finally, within your base template, wrap everything that isn't an empty block to be extended (i.e. not needed for the stub) in conditional tags:{% if not stub %}
...
{% endif %}
{% block body %}
{% endblock %}
{% if not stub %}
{% endif %}
Any templates that extend the base can now be rendered without the repetitive elements (essentially a conditional extension.)