WebPart中使用NVelocity來進行呈現(續)

來源:互聯網
上載者:User
在上一篇中,我遺留下了選擇模板的練習。
現在抽空測試了一下。
原理很簡單:在EditorPart中指定WebPart模板所在的檔案夾(相對路徑),並通過輸入的檔案夾路徑擷取該檔案夾下的templates.xml(即模板描述檔案)。
然後列出該模板描述檔案中的模板列表供選擇。模板描述檔案格式及內容如下(如果要在項目中應用,可能還需要作相應的更改):Code
 1<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
 2<TemplateFiles>
 3  <TemplateFile>
 4   <FileName>
 5     <![CDATA[default.vm]]>
 6   </FileName> 
 7   <TemplateName>預設</TemplateName>
 8   <Description>描述111111</Description>
 9   <Author>pcitxzz</Author>
10   <CreateDate>2009-08-18</CreateDate>
11  </TemplateFile>
12    <TemplateFile>
13   <FileName><![CDATA[new2.vm]]></FileName> <!--檔案名稱-->
14   <TemplateName>新的</TemplateName><!--模板名-->
15   <Description>描述2222</Description><!--模板描述-->
16   <Author>pcitxzz</Author><!--建立者-->
17   <CreateDate>2009-08-18</CreateDate><!--建立日期-->
18  </TemplateFile>
19</TemplateFiles>

在編寫EditorPart時,讀取這個xml檔案並分析即可。
完成EditorPart的編寫後,就可以在webpart中進行應用了。
------------------------------------------
以下是我測試的相關:
No1:EditorPart中選擇模板的效果:

No2:使用預設範本時的效果:

No3:使用“新的”模板時的效果:

可以看出,通過這種方式為同一個webpart應用不同的模板十分方便。
尤其是在實現CSS樣式無法實現的布局時或需要一些特殊功能時,您可以考慮這種實現方式。
----
相關源碼:/Files/McJeremy/NVelocityTestPart.rar
----
轉載請註明出處:http://www.cnblogs.com/mcjeremy

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.