defsnippet은 html의 특정 부분과 만 일치하므로 (선택자를 인수로 사용하는 이유이기도 함) 변환합니다. deftemplate은 전체 HTML을 가져 와서 변환합니다. 또한 defsnippet은 Clojure 데이터 구조를 반환하고 deftemplates는 문자열 벡터를 반환하므로 일반적으로 defsippipp가 deftemplate 내에서 사용됩니다.
HEADER.html 현재 :
당신이 뭔가를하려는 경우
(enlive/html-snippet "<div id='foo'><p>Hello there</p></div>")
;=({:tag :div, :attrs {:id "foo"}, :content ({:tag :p, :attrs nil, :content ("Hello there")})})
:
는 다음과 같다 당신에게 데이터가 조각 (또는 선택)에 의해 반환 있는지 알려주기 위해
<div id="my-header-root">
...
</div>
의 Clojure 코드 :
,
(enlive/defsnippet header "path/to/header.html" [:#my-header-root] []
identity)
(enlive/defsnippet footer "path/to/footer.html" [enlive/root] []
identity)
(enlive/deftemplate layout "layout.html" [header footer]
[:head] (enlive/content header)
[:body] (enlive/append footer))
(defroutes home-routes
(GET "/" [] (layout (header) (footer))
코드 단편에 사용 된 identity 함수는 인수를 반환하며,이 경우 인수는 # my-header-root selector (변환하지 않습니다)에 의해 선택된 데이터 구조입니다. 모든 항목을 head.html에 포함 시키려면 확장과 함께 제공되는 루트 선택기 단계를 사용할 수 있습니다.
할 수 있습니다 이런 식으로 뭔가를 사용하여 defsnippet에서 생성 된 HTML을 볼 수 :
(print (apply str (enlive/emit* (my-snippet))))
나는 또한 튜토리얼 추천
: https://github.com/swannodette/enlive-tutorial/ 하는 방법 defsnippet 및 deftemplate 매크로 좀 더 자세한 내용은 브라이언 마릭으로 하나를 작업.
(enlive/sniptest "<p>Replace me</p>"
[:p] (enlive/content "Hello world!"))
;= "<p>Hello world!</p>"
감사 :
마지막 팁, 당신은 선택기 및 변환이 enlive와 함께 제공되는 sniptest 매크로를 사용하여 실험 할 수있다! 미래의 독자들을위한 교정. 이 줄 (selector/header/header.html [: # my-header-root] 신분증의 확장/defsnippet 헤더)은 선택 자 다음에 벡터를 가져야합니다. 따라서 그것은 다음과 같아야합니다 : (확장/defsnippet 헤더 "path/to/header.html"[: # my-header-root] [] identity). 꼬리말을위한 제비 뽑기와 동일합니다. –
아, 그리워. 그에 따라 예제를 업데이트했습니다. – ebaxt