我正在为Django使用SHPAML(HAML for python),但是,我正面临转换SHPAML的问题 - > HTML是因为在覆盖某些块时会出现空白问题,这是一个例子:
在skeleton.shpaml:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>{{ title }}</title>
{% comment %}
<link rel="shortcut icon" href="/static/images/favicon.ico" type="image/x-icon"/>
{% endcomment %}
{% if css_list %}
{% for css in css_list %}
<link type="text/css" rel="stylesheet" href="{{css_relative}}{{ css }}">
{% endfor %}
{% endif %}
{% if js_list %}
{% for js in js_list %}
<script type="text/javascript" src="{{js_relative}}{{ js }}">
</script>
{% endfor %}
{% endif %}
{% if no_cache %}
<meta http-equiv="Pragma" content="no-cache" />
<meta http-equiv="Cache-Control" content="no-cache" />
{% endif %}
</head>
body
#nonFooter
#content
{% block header %} {% endblock %}
#maincontent
{% block content %} {% endblock %}
#footer
</html>
在index.shpaml:
{% extends "includes/skeleton.shpaml" %}
{% block content %}
asd
.test
.test2 | meh
{% endblock %}
最后,我的输出是:
!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Home | Zineified</title>
<link type="text/css" rel="stylesheet" href="/media/css/base.css">
<script type="text/javascript" src="/media/js/jquery-1.3.2.min.js">
</script>
<script type="text/javascript" src="/media/js/jquery.form.js">
</script>
<script type="text/javascript" src="/media/js/base.js">
</script>
</head>
body
#nonFooter
#content
#maincontent
asd
.test
.test2 | meh
#footer
</html>
如您所见,块中未保留空格。 index.shpaml中的下一行直接进入skeleton.shpaml的下一行。如何通过模板扩展来阻止这种情况并保留空格?
答案 0 :(得分:1)
来自docs:
spaceless
删除HTML标记之间的空格。这包括制表符和换行符。
使用示例:
{% spaceless %}
<p>
<a href="foo/">Foo</a>
</p>
{% endspaceless %}
此示例将返回此HTML:
<p><a href="foo/">Foo</a></p>
只删除标记之间的空格 - 而不是标记和文本之间的空格。
您也可以手动删除多余的空格/换行符,但这会降低模板的可读性。
答案 1 :(得分:1)
在Django之前看起来没有调用SHPAML预处理器。我通常做的是在SHPAML中使用.shpaml扩展名编写我的所有文档,然后将它们转换为扩展名为.html的Django,然后让Django发挥其魔力。因此,您需要使用“extends”和“include”等语句来引用已经过预处理的.html文档。
您的基本shpaml文档将如下所示:
html body #main_page {% block body %} {% endblock %}
然后扩展它的文档看起来像这样:
{% extends 'base.html' %} {% block body %} p This is a paragraph about {{ book }}... {% endblock %}
然后你想在Django看到它们之前对它们进行预处理。在执行“manage.py runserver”之前,我通常使用Python脚本对它们进行预处理。
答案 2 :(得分:0)
#maincontent
ASD
你的意思是错位吗?好吧,相应地对齐你的index.shpaml:
{% extends "includes/skeleton.shpaml" %}
{% block content %}
asd
.test
.test2 | meh
{% endblock %}
答案 3 :(得分:0)
我写了一个简单的脚本来递归浏览目录并查找所有shpaml文件并将它们转换为* .htm。以为我会分享它:
#!/usr/bin/env python
#===============================================================================
# Recursively explore this entire directory,
# and convert all *.shpaml files to *.htm files.
#===============================================================================
import shpaml
import os, glob
count = 0
def main():
global count
cwd = os.path.dirname(os.path.abspath(__file__))
convert_and_iterate(cwd)
print("Done. Converted "+str(count)+" SHPAML files.")
def convert_and_iterate(path):
global count
for file in glob.glob(os.path.join(path,'*.shpaml')):
#getting generic name
file_basename = os.path.basename(file)
gen_name = os.path.splitext(file_basename)[0]
#opening shpaml file and converting to html
shpaml_file = open(file)
shpaml_content = shpaml_file.read()
html_content = shpaml.convert_text(shpaml_content)
#writing to *.htm
html_file = open(os.path.join(path,gen_name+".htm"),"w")
html_file.write(html_content)
#incrementing count
count += 1
#transverse into deeper directories
dir_list = os.listdir(path)
for possible_dir in dir_list:
if os.path.isdir(os.path.join(path,possible_dir)):
convert_and_iterate(os.path.join(path,possible_dir))
if __name__ == "__main__":
main()