2010-01-04 64 views
0

我使用SHPAML(HAML为Python)Django的,然而,我所面临的问题转换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 %}&nbsp;{% endblock %} 
      #maincontent 
       {% block content %}&nbsp;{% endblock %} 
     #footer 
      &nbsp; 

</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 
       &nbsp; 
      #maincontent 

asd 
.test 
    .test2 | meh 

     #footer 
      &nbsp; 

</html> 

正如您所看到的,空格不会保留在块中。 index.shpaml中的下一行直接进入skeleton.shpaml中的下一行。我怎样才能防止这种情况,并通过扩展模板保留空白?

回答

1

它看起来像SHPAML预处理是没有得到调用之前的Django。我通常的做法是用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看到他们。我通常在进行“管理”之前使用Python脚本对它们进行预处理。。PY的runserver”

1

从文档:

spaceless

删除HTML标签之间的空白。这包括制表符和换行符。

用法示例:

{% spaceless %} 
    <p> 
     <a href="foo/">Foo</a> 
    </p> 
{% endspaceless %} 

这个例子将返回该HTML:

<p><a href="foo/">Foo</a></p> 

只有标签之间的空格被删除 - 标签和文本之间没有空格。

您也可以手动删除多余的空格/换行符,但这会降低模板的可读性。

+0

这不会这意味着模板扩展框架不包含SHPAML代码,并且SHPAML代码需要明显的空白(SHPAML本质上是pythonic HTML,就像HAML一样) – nubela 2010-01-04 21:55:44

+0

据我了解,它将把shpaml标签当作明文处理,并将保留所有的格式化。只有html标记会受到影响 – 2010-01-04 22:04:15

+0

你是对的,它仍然将shpaml标签当作明文处理,但是它并不能解决覆盖块内容时保留空白的问题t在skeleton.shpaml中。我只是试了一下。 – nubela 2010-01-04 22:09:40

0

ASD

你的意思是不对吗?那么,相应地调整你的index.shpaml:

{% extends "includes/skeleton.shpaml" %} 
{% block content %} 
      asd 
      .test 
       .test2 | meh 
{% endblock %} 
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()