including handlebars templates within a django generated page - python

I am using django to serve a page that includes a handlebars template. Mixing server and client side templating creates some ambiguity. If I have a template
<script id="my-script" type="text/x-handlebars-template">
<p> {{clientSideContent}} </p>
</script>
How can I tell the django templating engine that the {{clientSideContent}} tag isn't intended for it (since django and handlebars use the same tags)? Is there a raw strings tag in django, or an alternate good way to address this?

I think this should solve your problem
https://gist.github.com/ericflo/629508
so use something like
{% verbatim %} {{clientSideContent}} {% endverbatim %}

Never versions of django (starting from version 1.5) have support for the {% verbatim %} tag:
So something like this should work:
<script id="my-script" type="text/x-handlebars-template">
{% verbatim %}
<p> {{clientSideContent}} </p>
{% endverbatim %}
</script>
Hope it helps.

Related

Django templating define a variable

Hey i have a problem with defining a variable in Django templates. I donĀ“t know hat im doing wrong.
{% set name="World" %}
<html>
<div>Hello {{name}}!</div>
</html>
Django Invalid block tag on line 1: 'set'. Did you forget to register or load this tag?
I think you want to be using a "with" rather than "set".
https://docs.djangoproject.com/en/1.11/ref/templates/builtins/#with
{% with name='world'%}
<html>
<div>Hello {{name}}!</div>
</html>
{% endwith %}

Build HTML files using Django Template System or Static Site Generator?

I am trying to set up a proper workflow for a personal website.
I am using the Cactus static site generator which makes use of the Django template system.
I know what I want to do, but I am not sure how to do it as the tutorials for Cactus are limited.
This is what my directory structure looks like:
/mysite/pages/
/mysite/templates/
/mysite/mycontent/
/mysite/plugins/
My template, /mysite/pages/menu.html, looks like this:
<p>Welcome to the page!</p>
{% block body %}
{% endblock %}
And one of my page articles, /mysite/pages/testpage.html, looks like this
{% extends "menu.html" %}
{%block body %}
<p> Test Content </p>
{% endblock %}
But what I am trying to do is set this up so that whatever I want to write for Test Content can be written somewhere else and the copied in to the folder. Something like this:
{% extends "menu.html" %}
{%block body %}
{%include "../mysite/mycontent/TestContent.html}
{% endblock %}
Is this something that Django templates needs to manage? Like I said, I am using Cactus which uses Django templates but I have looked around and am not sure what the standard way of doing this is, even though it seems to work with MVC/MVT philosophy.
There is also an option to use Django plugins with Cactus.
Thanks!
I figured out what I was trying to do. I just had to include the html I wanted using the template language.
{% extends "menu.html" %}
{%block body %}
{% include "./file.html" %}
{% endblock %}

How to use django base extension along with angularjs ng-include?

I'm using ng-include like this
HTML
<div ng-app="myapp" ng-controller="myctrl" ng-include="/static/html/main.html"></div>
main.html
{% extends "app\base.html" %}
{% block content %}
<div ng-controller="myctrl1">
some content
</div>
{% endblock %}
base.html
<div ng-controller="myapp2">some content </div>
but this is not working, it is showing an errorr "SyntaxError: Unexpected token % at Object.parse (native)"
Please suggest me a better way to implement this.
I don't quite know what you're trying to do here. Angular templates and Django ones are quite different, even if they superficially share the same {{ var }} syntax. You can't inherit an Angular template from a Django one, or vice versa.
But there's no reason to want to do that anyway. Angular is already loading the template as a partial, which means it will be inserted into the div in base.html. There's no need for inheritance here at all.

Is there an equivalent of the "blocktrans" in tag in jinja2?

I am using jinja2 with my django application, I am working on porting some existing django templates over to jinja2. For the most part I am not having any issues; however, I have a statement like so in one of my django templates:
{% blocktrans %}
<p>Some stuff here</p>
{% endblocktrans %}
This causes jinja2 to become very unhappy. I have looked far and wide to see if there is an equivalent of the "blocktrans" tag for jinja2. All I can find is that enabling gettext let's you use:
{{ trans("Some String") }}
This is useful for short strings like in the title tag, but not for blocks. What am I missing, any help would be appreciated!
Use {% trans %} and its complement {% endtrans %} once the i18n extension is enabled.

In Django, what is the best way to manage both a mobile and desktop site?

I'd like everything to function correctly, except when it's mobile, the entire site will used a set of specific templates.
Also, I'd like to autodetect if it's mobile. If so, then use that set of templates throughout the entire site.
Have two sets of templates, one for mobile, one for desktop. Store the filenames in a pair of dictionaries, and use the User-agent header to detect which set should be used. Also allow manual selection of which site to use via a session entry.
If you place a class on your body (Django uses something similar to specify what column style to use), you could use the same templates but simply use different stylesheets. I'm not sure what main differences you are using separate templates for, but this might allow you to cut down on re-coding the templates multiple times.
best practice: use minidetector to add the extra info to the request, then use django's built in request context to pass it to your templates like so.
from django.shortcuts import render_to_response
from django.template import RequestContext
def my_view_on_mobile_and_desktop(request)
.....
render_to_response('regular_template.html',
{'my vars to template':vars},
context_instance=RequestContext(request))
then in your template you are able to introduce stuff like:
<html>
<head>
{% block head %}
<title>blah</title>
{% if request.mobile %}
<link rel="stylesheet" href="{{ MEDIA_URL }}/styles/base-mobile.css">
{% else %}
<link rel="stylesheet" href="{{ MEDIA_URL }}/styles/base-desktop.css">
{% endif %}
</head>
<body>
<div id="navigation">
{% include "_navigation.html" %}
</div>
{% if not request.mobile %}
<div id="sidebar">
<p> sidebar content not fit for mobile </p>
</div>
{% endif %>
<div id="content">
<article>
{% if not request.mobile %}
<aside>
<p> aside content </p>
</aside>
{% endif %}
<p> article content </p>
</aricle>
</div>
</body>
</html>
There are different strategies.
If you've a lot of views that renders to template files for the web version, and don't want to rewrite all views checking if the request is coming from a mobile user-agent, you'd be better writing a Middleware.
A workflow could be like this:
def process request:
if from_mobile:
settings.TEMPLATE_DIRS=settings.TEMPLATE_MOBILE_DIRS
else:
settings.TEMPLATE_DIRS=settings.TEMPLATE_WEB_DIRS
There is only a little problem here: As Django Documentation reports, it's not correct to change settings at runtime: http://docs.djangoproject.com/en/dev/topics/settings/#altering-settings-at-runtime
So you may want to call
django.conf.settings.configure(default_settings, **settings)
The answer depends heavily on the type of your target audience. If you target for modern mobile browsers equivalents to their desktop counterparts (such as WebKit-based), all you need is specific stylesheet with appropriate media query (you are basically designing for low-res rather than mobile).
Totally different strategy is needed if your site (e.g. airline schedules) must to be accessible widest possible range of mobile devices, some of running very old / limited browsers. Then custom (html) templates may be easiest way to go.
You might want to check out mobilesniffer and django-bloom to see if they fit your purposes.

Categories

Resources