今年很早的时候就试过PyJade在Django上面运行,当时仅仅是把Django部署到SAE上面跑。后来发现太多限制了,就玩了几个小时就没有兴趣玩了,而且各种奇怪的配置搞了好久。但是这次就不一样了,是把Django部署到自己的VPS上面,爱干嘛干嘛。当然是把各种爽的工具弄到工程里面来。由于Node.js的原因,喜欢上了Jade模板引擎,而且发现开发的效率是奇高。对于敏捷开发的Django来说,搭配一个强悍的模板引擎,我当然毫无置疑地使用了Jade。
下面说说简单的配置过程,以及使用方式。 官方的地址是:https://github.com/SyrusAkbary/pyjade。感觉现在好多Django的项目已经不在维护,但是发现PyJade在最近这些天还在更新。而且今年早期的时候跟对方提过Issue,都非常快地得到了回复。解决了我的问题。
在Django下的配置方式是,在Setting.py文件中做出修改:
TEMPLATE_LOADERS = ( ('pyjade.ext.django.Loader',( 'django.template.loaders.filesystem.Loader', 'django.template.loaders.app_directories.Loader', )), )
设置TEMPLATE_DIRS
import os TEMPLATE_DIRS = ( os.path.join(os.path.dirname(__file__), '../website/templates').replace('\\','/') )
在Views中的设置跟使用普通的模板并没有任何区别
from django.http import HttpResponse from django.template import RequestContext from django.shortcuts import render_to_response def render(request, template, context): return render_to_response(template, context, context_instance=RequestContext(request)) def index(request): return render(request, 'index.jade', {}) #return HttpResponse("Hello, world. You're at the website index.")
当然少不了你模板文件*.jade啦
!!! 5 html(lang="en") head title= pageTitle script(type='text/javascript') if (foo) { bar() } body h1.title Jade - node template engine #container if youAreUsingJade p You are amazing else p Get on it!