Harp 沒有復雜的功能集,只有一些遵循工作的簡單規(guī)則。Harp 是相對簡單的武士刀,而非功能復雜的瑞士軍刀。通過理解規(guī)則,你將學會如何有效使用 Harp。
Harp 少到只需要一個 public/index.html
文件就可以工作,并不需要任何的配置。添加額外的路由只需添加額外的文件。通過學習這些剩余的規(guī)則你將會發(fā)現(xiàn),所有的 Harp 的功能都是基于約定的。 Harp 致力于提供一個合理的開發(fā)框架,遵循成型的最佳實踐。Harp 并非設計用于給很多人做很多事情,而是專注于自己擅長的事情。
秉行多約定,少配置 ,Harp易于上手,讓你更好地開發(fā)產(chǎn)品。
myapp.harp.io/ <-- root of your application
|- harp.json <-- configuration, globals goes here.
+- public/ <-- your application assets belong in the public dir
|- _layout.jade <-- optional layout file
|- index.jade <-- must have an index.html or index.jade file
|- _shared/ <-- arbitrary directory for shared partials
| +- nav.jade <-- a partial for navigation
+- articles/ <-- pages in here will have "/articles/" in URL
|- _data.json <-- articles metadata goes here
+- hello-world.jade <-- should have at least one .html, .jade, .md or .ejs file.
Harp 是一個網(wǎng)頁服務器,所以它可以伺服任何目錄,無論是包含一個大的 Harp 應用,還是只有單個 index.html
文件。
myapp.harp.io/
|- index.html <-- will be served
Harp 應用可以選擇性地以框架風格運行。當一個項目包含一個 harp.json
文件和一個 public 目錄,public 目錄會取代根目錄做為伺服目錄??蚣茱L格中,公共文件放在 public 目錄中。public 目錄之外的文件不會被伺服。
myapp.harp.io/
|- harp.json <-- required harp.json file
|- README.md <-- won't be served
|- secrets.txt <-- won't be served
+- public/ <-- explicit public directory
+- index.html <-- will be served
任何以下劃線開頭的文件都會被服務器忽略。這是布局和局部視圖文件的推薦命名約定。Harp 將會對文件和目錄都遵循這個規(guī)則。
通過一個簡單的約定,指定和鑒別哪些文件不對終端用戶服務變得相當簡單。
myapp.harp.io/
+- public/
|- index.html <-- will be served
|- _some-partial.jade <-- won't be served
+- _shared-partials/ <-- won't be served
+- nav.jade
Harp 支持 ?jade
?, ?ejs
?, ?stylus
?, ?less
?以及 ?coffee
?腳本。只需添加 .jade
, .ejs
, .styl
, .less
或者 .coffee
后綴,?Harp asset pipeline
? 會負責剩余的事情。 只需添加文件擴展名,引用類庫,Harp 便會自動預編譯。
myfile.md -> myfile.html
myfile.jade -> myfile.html
myfile.ejs -> myfile.html
myfile.less -> myfile.css
myfile.styl -> myfile.css
myfile.scss -> myfile.css
myfile.sass -> myfile.css
myfile.coffee -> myfile.js
如果你高興的話,通過在擴展名前加上想要的擴展,可以特別指定支持這種類型的文件。
myfile.jade -> myfile.html
myfile.xml.jade -> myfile.xml
然而,這是可選的,就像每一個擴展名都已經(jīng)有了一個默認的擴展類型。下面的兩個文件都會被當作 myfile,css
進行伺服,例如:
myfile.less -> myfile.css
myfile.css.less -> myfile.css
_data.json
文件比較特殊,里面的數(shù)據(jù)對模板文件可用。
myapp.harp.io/
+- public/
|- index.jade
+- articles/
|- _data.json <-- articles metadata goes here
|- hello-world.jade <-- hello world article
+- hello-brazil.jade <-- hello brazil article
_data.json
文件可能包含這樣的內(nèi)容:
{
"hello-world": {
"title": "Hello World.",
"date": "Feb 28, 2013"
},
"hello-brazil": {
"title": "Hello Brazil.",
"date": "March 4, 2013"
}
}
此信息將在模板文件中可以這樣使用:
public.articles._data
此外,由于 hello-world 匹配文件名 hello-world.jade
,這些變量可以在 hello-world.jade
模板文件中使用。這個對象也可以作為 public.articles._data.hello-world
在所有的模板文件中使用。
在模板文件中,可以通過下面方式,在Jade文件中迭代 articles 變量:
for article, slug in public.articles._data
a(href="/articles/#{ slug }")
h2= article.title
更多建議: