django模板的类似haml的语法。
django-hamlp的Python项目详细描述
为什么输入:
<divclass="left"id="banner"> Greetings! </div>
当您只需键入:
.left#banner Greetings!
…做些更有趣的事情,省去了打字的时间。 括号和记住关闭标签?
上面的语法是Haml-a模板 ruby on rails社区中广泛使用的语言。这个图书馆 让django开发人员在模板中使用类似haml的语法。是的 不是模板引擎本身,而是一个编译器 将“hamlpy”文件转换为django可以理解的模板。
这个项目是一个不再维护的分支 HamlPy。它引入了python 3支持,支持新的django版本,以及许多新特性 以及错误修复。注意,包名现在是django hamlpy。
安装
最新的稳定版本可以使用 pip:
pip install django-hamlpy
最新的开发版本可以直接从 Github:
pip install git+https://github.com/nyaruka/django-hamlpy
注意:如果遇到生成错误,则可能需要安装 python’s development package。
语法
几乎所有的haml语法都被保留了下来。
#profile(style="width: 200px") .left.column#date 2010/02/18 #address Toronto, ON .right.column< #bio Jesse Miller
变成:
<divid='profile'style="width: 200px"><divclass='left column'><divid='date'>2010/02/18</div><divid='address'>Toronto, ON</div></div><divclass='right column'><divid='bio'>Jesse Miller</div></div></div>
主要的区别在于我们没有解释ruby,甚至python 相反,可以创建django标记和变量。例如:
%ul#athletes-forathleteinathlete_list%li.athlete{'id':'athlete_#{ athlete.pk }'}= athlete.name
变成…
<ulid='athletes'>{%forathleteinathlete_list%}<liclass='athlete'id='athlete_{{athlete.pk}}'>{{athlete.name}}</li>{%endfor%}</ul>
用法
使用这个库有两种不同的方法。
选项1:模板加载程序
这些是django模板加载程序,它将用 .haml或.hamlpy扩展到常规django模板 他们是被一个Django的观点要求的。要使用它们,请将它们添加到列表中 Django设置中的模板加载程序,例如
TEMPLATES=[{'BACKEND':'django.template.backends.django.DjangoTemplates','DIRS':['./templates'],'OPTIONS':{'loaders':('hamlpy.template.loaders.HamlPyFilesystemLoader','hamlpy.template.loaders.HamlPyAppDirectoriesLoader',...),}}]
确保在标准django模板加载程序或 这些加载程序将尝试处理您的haml模板。
模板缓存
您可以将这些加载程序与模板缓存一起使用-只需添加 django.template.loaders.cached.Loader添加到加载程序列表中,例如
'loaders':(('django.template.loaders.cached.Loader',('hamlpy.template.loaders.HamlPyFilesystemLoader','hamlpy.template.loaders.HamlPyAppDirectoriesLoader',...)),)
设置
您可以使用以下django设置配置haml编译器:
- HAMLPY_ATTR_WRAPPER–应该包装元素的字符 属性。默认为'(撇号)。
- HAMLPY_DJANGO_INLINE_STYLE–是否支持={...}语法 对于除了#{...}之外的内联变量。默认为 False。
选项2:观察者
库也可以用作独立程序。有一个 监视程序脚本,用于监视给定目录中的haml文件和 编辑时将它们转换为HTML。
usage: hamlpy_watcher.py [-h] [-v] [-i EXT [EXT ...]] [-ext EXT] [-r S] [--tag TAG] [--attr-wrapper {",'}] [--django-inline] [--jinja] [--once] input_dir [output_dir] positional arguments: input_dir Folder to watch output_dir Destination folder optional arguments: -h, --help show this help message and exit -v, --verbose Display verbose output -i EXT [EXT ...], --input-extension EXT [EXT ...] The file extensions to look for. -ext EXT, --extension EXT The output file extension. Default is .html -r S, --refresh S Refresh interval for files. Default is 3 seconds. Ignored if the --once flag is set. --tag TAG Add self closing tag. eg. --tag macro:endmacro --attr-wrapper {",'} The character that should wrap element attributes. This defaults to ' (an apostrophe). --django-inline Whether to support ={...} syntax for inline variables in addition to #{...} --jinja Makes the necessary changes to be used with Jinja2. --once Runs the compiler once and exits on completion. Returns a non-zero exit code if there were any compile errors.
创建要翻译的消息文件
hamlpy必须首先包含在django的应用程序列表中,即
INSTALLED_APPS=[...'hamlpy'...]
然后把你的haml模板和所有其他文件一起 包含可翻译字符串,例如
python manage.py makemessages --extension haml,html,py,txt
参考
查看 reference 文件中有完整的语法参考和更多示例。
基于类的视图
这个库还提供the same class based generic views than django 他们从寻找模板结尾开始 除了默认模板之外,还有*.haml和*.hamlpy。 除此之外,它们是完全相同的基于类的泛型视图。 例如:
fromhamlpy.views.genericimportDetailView,ListViewfrommy_app.modelsimportSomeModel# will look for the templates `my_app/somemodel_detail.haml`,# `my_app/somemodel_detail.hamlpy` and `my_app/somemodel_detail.html`DetailView.as_view(model=SomeModel)# will look for the templates `my_app/somemodel_list.haml`,# `my_app/somemodel_list.hamlpy` and `my_app/somemodel_list.html`ListView.as_view(model=SomeModel)
可用的视图类有:
显示视图:
编辑视图:
日期相关视图:
- DateDetailView
- ArchiveIndexView
- YearArchiveView
- MonthArchiveView
- WeekArchiveView
- DayArchiveView
- TodayArchiveView
所有视图都可从hamlpy.views.generic导入并生成 使用^ {tt13}$$MIXIN,您可以使用 使用视图创建您自己的自定义haml。例如:
fromhamlpy.views.genericimportHamlExtensionTemplateViewclassMyNewView(HamlExtensionTemplateView,ParentViewType):pass
注释:HamlExtensionTemplateViewneeds在 继承列表。
贡献
我们很高兴能为这个项目做出贡献。开始 您需要克隆项目并安装依赖项:
virtualenv env source env/bin/activate pip install -r requirements/base.txt pip install -r requirements/tests.txt
请为任何新功能编写测试,并始终确保 测试通过。要运行测试,请使用:
py.test hamlpy
要运行性能测试,请使用:
python -m hamlpy.test.test_templates