Skip to content

Commit

Permalink
fix: fragments without dynamic content
Browse files Browse the repository at this point in the history
  • Loading branch information
erdos committed Oct 1, 2019
1 parent e977bb7 commit ef97abd
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 4 deletions.
3 changes: 2 additions & 1 deletion examples/Multipart Template/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,18 @@
public class Main {

public static void main(String... args) throws IOException {

// prepare template file
PreparedTemplate template = API.prepare(findFile("template.docx"));

// prepare header fragment
PreparedFragment header = API.fragment(findFile("header.docx"));
PreparedFragment footer = API.fragment(findFile("footer.docx"));
PreparedFragment staticContent = API.fragment(findFile("static.docx"));

Map<String, PreparedFragment> fragments = new HashMap<>();
fragments.put("header", header);
fragments.put("footer", footer);
fragments.put("static", staticContent);

// assemble template data
Map<String, Object> dataMap = new HashMap<>();
Expand Down
Binary file added examples/Multipart Template/static.docx
Binary file not shown.
Binary file modified examples/Multipart Template/template.docx
Binary file not shown.
8 changes: 6 additions & 2 deletions src/stencil/model.clj
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,10 @@
(assert (::path part))
(if (:dynamic? (:executable part))
(eval-model-part-exec (:executable part) data functions)
{:writer (resource-copier part)}))
{:writer (resource-copier part)
:xml-delay (delay
(with-open [reader (io/input-stream (:source-file part))]
(update (xml/parse reader) :content doall)))}))


(defn- style-file-writer [template]
Expand Down Expand Up @@ -417,7 +420,8 @@
evaled (eval-template-model fragment-model local-data-map {} {})

;; write back
evaled-parts (-> evaled :main :result :xml (doto assert) extract-body-parts)]
get-xml (fn [x] (or (:xml x) @(:xml-delay x)))
evaled-parts (-> evaled :main :result get-xml extract-body-parts)]
(swap! *inserted-fragments* conj frag-name)
(swap! *extra-files* into relation-ids-rename)
[{:text (->FragmentInvoke {:frag-evaled-parts evaled-parts})}])
Expand Down
3 changes: 2 additions & 1 deletion test/stencil/api_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,10 @@
(deftest test-render-with-fragments
(let [footer (fragment "./examples/Multipart Template/footer.docx")
header (fragment "./examples/Multipart Template/header.docx")
static (fragment "./examples/Multipart Template/static.docx")
template (prepare "./examples/Multipart Template/template.docx")
data {:companyName "ACME" :companyAddress "Moon"}
fs-map {:footer footer :header header}]
fs-map {:footer footer :header header :static static}]
(testing "Rendering multipart template"
(render! template data :fragments fs-map))
(testing "Can not render when fragments can not be found."
Expand Down

0 comments on commit ef97abd

Please sign in to comment.