You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by bd...@apache.org on 2020/05/29 09:08:23 UTC
[sling-whiteboard] 01/02: Fix tag lists templates
This is an automated email from the ASF dual-hosted git repository.
bdelacretaz pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-whiteboard.git
commit 9beeb8d7b326a7c34afa0b7271e0de8779c31607
Author: Bertrand Delacretaz <bd...@apache.org>
AuthorDate: Fri May 29 11:04:04 2020 +0200
Fix tag lists templates
---
org.apache.sling.graphql.samples.website/README.md | 4 ++--
.../sling/scripting/handlebars/HandlebarsScriptEngine.java | 11 ++++++++++-
.../initial-content/apps/samples/article/article.hbs | 2 +-
.../SLING-INF/initial-content/apps/samples/tag/tag.hbs | 2 +-
4 files changed, 14 insertions(+), 5 deletions(-)
diff --git a/org.apache.sling.graphql.samples.website/README.md b/org.apache.sling.graphql.samples.website/README.md
index 16ee86f..f962b73 100644
--- a/org.apache.sling.graphql.samples.website/README.md
+++ b/org.apache.sling.graphql.samples.website/README.md
@@ -90,9 +90,9 @@ And open the above mentioned start page.
## Under the hood
The following explanations apply to the article and navigation pages. The (upcoming) single-page apps
-will use different mechanisms.
+will use similar mechanisms client-side.
-The scripts and source code mentioned below are foud in the source code and initial content of this
+The scripts and source code mentioned below are found in the source code and initial content of this
demo module.
The GraphQL core retrieves a schema for the current Sling Resource by making a request with
diff --git a/org.apache.sling.graphql.samples.website/src/main/java/org/apache/sling/scripting/handlebars/HandlebarsScriptEngine.java b/org.apache.sling.graphql.samples.website/src/main/java/org/apache/sling/scripting/handlebars/HandlebarsScriptEngine.java
index cf628ef..eb79f20 100644
--- a/org.apache.sling.graphql.samples.website/src/main/java/org/apache/sling/scripting/handlebars/HandlebarsScriptEngine.java
+++ b/org.apache.sling.graphql.samples.website/src/main/java/org/apache/sling/scripting/handlebars/HandlebarsScriptEngine.java
@@ -34,6 +34,7 @@ import javax.script.ScriptException;
import com.cedarsoftware.util.io.JsonReader;
import com.github.jknack.handlebars.Handlebars;
import com.github.jknack.handlebars.Template;
+import com.github.jknack.handlebars.helper.StringHelpers;
import org.apache.commons.io.IOUtils;
import org.apache.sling.api.resource.Resource;
@@ -50,6 +51,8 @@ import org.apache.sling.servlethelpers.MockSlingHttpServletResponse;
* The JSON representation of the current Resource is retrieved
* using an internal request with the Resource path + ".json", and
* the result converted to a Handlebars-friendly Map of Maps.
+ *
+ * See https://github.com/jknack/handlebars.java for Handlebars docs
*/
public class HandlebarsScriptEngine extends AbstractScriptEngine {
@@ -65,7 +68,7 @@ public class HandlebarsScriptEngine extends AbstractScriptEngine {
final PrintWriter out = (PrintWriter) context.getBindings(ScriptContext.ENGINE_SCOPE).get(SlingBindings.OUT);
try {
- final Handlebars handlebars = new Handlebars();
+ final Handlebars handlebars = setupHandlebars();
final Template template = handlebars.compileInline(script);
out.println(template.apply(getData(resource)));
} catch(IOException ioe) {
@@ -76,6 +79,12 @@ public class HandlebarsScriptEngine extends AbstractScriptEngine {
return null;
}
+ private Handlebars setupHandlebars() {
+ final Handlebars result = new Handlebars();
+ result.registerHelpers(StringHelpers.class);
+ return result;
+ }
+
/** We might do this with a BindingsValuesProvider? */
private Map<?, ?> getData(Resource r) throws ScriptException {
// Request resource.json and convert the result to Maps
diff --git a/org.apache.sling.graphql.samples.website/src/main/resources/SLING-INF/initial-content/apps/samples/article/article.hbs b/org.apache.sling.graphql.samples.website/src/main/resources/SLING-INF/initial-content/apps/samples/article/article.hbs
index 84980f0..612db9a 100644
--- a/org.apache.sling.graphql.samples.website/src/main/resources/SLING-INF/initial-content/apps/samples/article/article.hbs
+++ b/org.apache.sling.graphql.samples.website/src/main/resources/SLING-INF/initial-content/apps/samples/article/article.hbs
@@ -47,7 +47,7 @@
<a href="/tags/{{this}}.html">{{this}}</a>
</span>
{{/each}}
- - Find articles <a href="/tags/{{data.article.tagsQuery}}.html">with all these tags</a>
+ - Find articles <a href='/tags/{{join data.article.tags "+"}}.html'>with all these tags</a>
<hr/>
{{/if}}
</div>
diff --git a/org.apache.sling.graphql.samples.website/src/main/resources/SLING-INF/initial-content/apps/samples/tag/tag.hbs b/org.apache.sling.graphql.samples.website/src/main/resources/SLING-INF/initial-content/apps/samples/tag/tag.hbs
index f8b6b3e..19bf581 100644
--- a/org.apache.sling.graphql.samples.website/src/main/resources/SLING-INF/initial-content/apps/samples/tag/tag.hbs
+++ b/org.apache.sling.graphql.samples.website/src/main/resources/SLING-INF/initial-content/apps/samples/tag/tag.hbs
@@ -34,7 +34,7 @@
<hr/>
</div>
- <h1>Articles with tags "{{data.tagQuery.query}}"</h1>
+ <h1>Articles with tags "{{join data.tagQuery.query ','}}"</h1>
<ul>
{{#each data.tagQuery.articles}}
<li class="seeAlso">