Ham with Django has HamlPy, but Python3 probably doesn't support it. In fact, Python3 doesn't seem to have many options, and I thought that hamlish-jinja, which is rather dead, is a good idea.
hamlish-jinja is just a haml-ish syntax, not Haml, but I felt that I could be quite happy with Pythonic. I think the learning cost is almost 0! https://github.com/Pitmairen/hamlish-jinja
Well, in order to do that, I have to use Jinja2 for the template engine, but well, it's a strange story to change for that, but I like Jinja2 purely, not for Haml, I change it by saying that I like shrines and temples.
What I did to use the following.
http://qiita.com/ryu22e/items/e50f8a3fbd6fe836c1b4
The setting method around the template has changed considerably since 1.8! !! !! !! Also, it seems that Jinja2 can be easily introduced! !! !! !! !!
However, it's quite difficult to do small things, so after all, as usual, pip django-jinja
can make it a lot easier. The setting method is also detailed. With match_regex, you can switch which backend to use depending on the URL. So I think this is fine.
http://niwinz.github.io/django-jinja/
pip install hamlish-jinja
So my setting.py looks like this.
from django_jinja.builtins import DEFAULT_EXTENSIONS
from hamlish_jinja import HamlishExtension
TEMPLATES = [
{
"BACKEND": "django_jinja.backend.Jinja2",
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'match_regex': r"^(?!admin/).*",
'match_extension': ".haml",
'extensions': DEFAULT_EXTENSIONS + [HamlishExtension]
},
},
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
Now, if you put XXX.haml in your usual template directory and specify XXX.haml in view, it will work normally.
%html
%head
%meta charset='utf-8'
%meta name='viewport' content='width=device-width'
%meta name='format-detection' content='telephone=no'
%meta name='description' content={{description}}
%meta name='keywords' content={{keywords}}
%title << {{title}}
%script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js'
How nice.
However, hamlish-jinja alone does not have any mechanism to compile it into a jinja template first, so it is probably hard to put it into production as it is. So I decided to use the cache mechanism of jinja2 itself. If you cache it with jinja's bytecode, it doesn't matter what haml looks like. Should be.
Now this.
from jinja2 import FileSystemBytecodeCache
...
...
TEMPLATES = [
{
"BACKEND": "django_jinja.backend.Jinja2",
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'match_regex': r"^(?!admin/).*",
'match_extension': ".haml",
'extensions': DEFAULT_EXTENSIONS + [HamlishExtension],
'bytecode_cache': FileSystemBytecodeCache(directory=os.path.join(BASE_DIR, 'project/cache'), pattern='%s.cache'),
},
},
...
Files will accumulate in the specified cache directory as appropriate. It's good because the time stamp of the file is updated only when haml is updated.
That's it.
Recommended Posts