2011-08-02 44 views
0

我正在爲我的項目使用webpy框架。我想從我的webpy程序傳遞一個文件並將其顯示在html頁面上(文件可能是任何文本文件/程序文件)。我使用了我的webpy程序中的以下函數傳遞了一個文本文件。在HTML頁面上顯示文件,因爲它是

class display_files: 
    def GET(self): 
     wp=web.input() 
     file_name=wp.name 
     repo_name=wp.repo 
     repo_path=os.path.join('repos',repo_name) 
     file_path=os.path.join(repo_path,file_name) 
     fp=open(file_path,'rU')    #reading file from file path 
     text=fp.read()      #no problem found till this line. 
     fp.close()    
     return render.file_display(text) #calling file_display.html 

當我試圖顯示文件(這裏是「文」),從「file_display.html」,它不斷地顯示不承認newline.Here是我的HTML文件。

$def with(content) 
<html> 
<head> 
    <meta http-equiv="Content-Type" content="text/string;charset=utf-8" > 
    <title>File content</title> 
</head> 
<body> 
    <form name="file_content" id="file_content" methode="GET"> 
     <p> $content</p> 
    </form> 
</body> 
<html> 

如何顯示文件,因爲它是在HTML頁面。

回答

0

HTML對待的空白字符量爲單個空格。如果要顯示該文件包含這樣的文字:

line one 
    line two with indent 

渲染file_display.html將包含此HTML:

<p> line one 
    line two with indent</p> 

儘管如此,換行和兩個空間將被視爲一個單一的空間,並在瀏覽器,它看起來就像這樣:

line one line two with indent 

The pre element告訴瀏覽器,它裏面的文本是預先格式化,所以換行和空格應該是保持。因此,您的模板應該如下所示:

<form name="file_content" id="file_content" methode="GET"> 
    <pre>$content</pre> 
</form> 

至於Joseph的建議,web.py模板系統將爲您處理轉義。如果您的文件包含<>等字符,則會將其替換爲&lt;&gt;

+0

非常感謝Helgi。你的回答是絕對正確的。也感謝約瑟的回答。 – Unni

0

貌似您可能需要全局替換<或>與& lt;或& gt;分別爲:

http://jsfiddle.net/BaULp/

+0

我很確定'file_display.html'是一個web.py模板文件,而不是Umni要顯示的文件。無需轉義模板。 – Helgi

+0

@Helgi啊。我不知道關於web.py。我只是想弄清楚在'pre'標籤之間顯示HTML的方式,因爲我認爲這是個問題。 :P感謝您的信息! –

+0

Helgi是對的。 file_display.html是我的web.py模板。 – Unni

相關問題