2

I am using django on the server side and obviously javascript on the client side. Now i want to use the plate template engine on the client.

What's the best way so serve django templates to the client? We taught of some ways doing that.

  1. Create a view that serves the raw templates.
    • probably not the best method
  2. Copy the needed templates to the static folder.
    • this could be done with a custom static files finder
    • the broser is able to cache the templates
  3. Provide the templates using a template tag which puts the raw template into a javascript variables.
    • templates received this way can not be cached seperatly

is a django app out there that makes this easyer?

The reason i need the templates on the client is, that i want to use the same clients on the server and the client side. When the page is first loaded, the full template is rendered on the server, when navigating trough the application only the needed data gets loaded and the page change is done using push state.

1 Answer 1

2

If you need to be able to have A) dynamically generated plate templates, or B) dynamically created plate templates (e.g., entered into the DB via the admin, etc.), You'll want to go with 1 (not a bad thing - django is made for serving text content, so as long as you need to have it in a dynamic manner, there's no problem doing it). 3 is a bad choice, because it means that a browser can't cache the static resource (if it's output into each page)... unless you need different plate templates for each page of course.

If you don't need A or B from above, I'd just stick the templates in your static dir, as you mentioned (e.g., collectstatic or simply add them to your repo, if they're a part of your app).

Regarding an app that makes this easy - you could look at Django Chunks (output a static chunk into a place in the page, like `{% chunk "header-snippet" %}), but I don't think you need that.

Sign up to request clarification or add additional context in comments.

2 Comments

I need the templates on the server side too. So storing them in a database is not an option. I'll add the relevant info to my question and will look into django chunks.
@frog32 - I see now what you're trying to accomplish. The best way to do this might be to have the templates exist as normal templates and then use the low level template API (load_template_source) combined with a simple view to return JSON with the template's source - then use $.getJSON to get the template from your server.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.