Skip to content

Commit

Permalink
Merge branch 'freemarker-template-db-fix' into jbake-mm
Browse files Browse the repository at this point in the history
  • Loading branch information
manikmagar committed May 29, 2017
2 parents 9a95fea + 6be3e91 commit 0d7985d
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 14 deletions.
1 change: 1 addition & 0 deletions src/main/java/org/jbake/app/Crawler.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ interface Status {
String ALLTAGS = "alltags";
String PUBLISHED_DATE = "published_date";
String BODY = "body";
String DB = "db";
}

private static final Logger LOGGER = LoggerFactory.getLogger(Crawler.class);
Expand Down
38 changes: 25 additions & 13 deletions src/main/java/org/jbake/template/FreemarkerTemplateEngine.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
package org.jbake.template;


import java.io.File;
import java.io.IOException;
import java.io.Writer;
import java.util.Collection;
import java.util.Date;
import java.util.Map;

import org.apache.commons.configuration.CompositeConfiguration;
import org.jbake.app.ConfigUtil.Keys;
import org.jbake.app.ContentStore;
import org.jbake.app.Crawler;

import freemarker.ext.beans.BeansWrapper;
import freemarker.ext.beans.BeansWrapperBuilder;
import freemarker.template.Configuration;
import freemarker.template.DefaultObjectWrapper;
import freemarker.template.SimpleCollection;
Expand All @@ -14,19 +28,6 @@
import freemarker.template.TemplateModel;
import freemarker.template.TemplateModelException;

import org.apache.commons.configuration.CompositeConfiguration;
import org.jbake.app.ConfigUtil.Keys;
import org.jbake.app.Crawler;

import java.io.File;
import java.io.IOException;
import java.io.Writer;
import java.util.Collection;
import java.util.Date;
import java.util.Map;

import org.jbake.app.ContentStore;

/**
* Renders pages using the <a href="http://freemarker.org/">Freemarker</a> template engine.
*
Expand Down Expand Up @@ -79,6 +80,17 @@ public LazyLoadingModel(final Map<String, Object> eagerModel, final ContentStore
@Override
public TemplateModel get(final String key) throws TemplateModelException {
try {

// GIT Issue#357: Accessing db in freemarker template throws exception
// When content store is accessed with key "db" then wrap the ContentStore with BeansWrapper and return to template.
// All methods on db are then accessible in template. Eg: ${db.getPublishedPostsByTag(tagName).size()}
if(key.equals(Crawler.Attributes.DB)) {
BeansWrapperBuilder bwb = new BeansWrapperBuilder(Configuration.DEFAULT_INCOMPATIBLE_IMPROVEMENTS);
BeansWrapper bw = bwb.build();
return bw.wrap(db);
}


return extractors.extractAndTransform(db, key, eagerModel.toMap(), new TemplateEngineAdapter<TemplateModel>() {

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,5 +81,26 @@ public void shouldFallbackToRenderSingleIndexIfNoPostArePresent() throws Excepti
assertTrue("index file exists",indexFile.exists());

}

@Test
public void checkDbTemplateModelIsPopulated() throws Exception {

config.setProperty(Keys.PAGINATE_INDEX, true);
config.setProperty(Keys.POSTS_PER_PAGE, 1);

outputStrings.put("dbSpan", Arrays.asList("<span>3</span>"));

db.deleteAllByDocType("post");

renderer.renderIndexPaging("index.html");

File outputFile = new File(destinationFolder, "index.html");
String output = FileUtils.readFileToString(outputFile, Charset.defaultCharset());

for (String string : getOutputStrings("dbSpan")) {
assertThat(output).contains(string);
}

}

}
2 changes: 1 addition & 1 deletion src/test/resources/fixture/freemarkerTemplates/index.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<p>${post.body}</p>
</#if>
</#list>

<span>${db.getPublishedPages().size()}</span>
<hr/>

<#if (config.index_paginate!false) >
Expand Down

0 comments on commit 0d7985d

Please sign in to comment.