Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement .collectWithParameters() and .collectAllParameters() #637

Open
wants to merge 25 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
b4323ab
Implement collectAllParameters() and collectWithParams() with Ilya
andrewboni Jun 6, 2018
50b44a7
fix for Variable
dontgitit Jun 6, 2018
61b42d8
Update comments and add the TagType for TagWithParams.
andrewboni Jun 8, 2018
d213d17
Update comments
andrewboni Jun 8, 2018
46d885b
Add in tagType and fix test
andrewboni Jun 8, 2018
8ee631e
Add additional test cases
andrewboni Jun 11, 2018
6678deb
Update tests
andrewboni Jun 11, 2018
8ba0497
Add built snapshot JAR to use until changes merged upstream
Jun 12, 2018
95a12c7
remove handlebars jar
Jun 12, 2018
223e7c7
Update tests, add in custom .toString()
andrewboni Jun 21, 2018
4cbdfee
Merge branch 'master' of github.com:Iterable/handlebars.java
andrewboni Jun 21, 2018
775086e
Add comments, fix style
andrewboni Jun 22, 2018
ded97c1
refactor name and package
dontgitit Oct 31, 2018
6bce684
Merge remote-tracking branch 'upstream/master'
dontgitit Oct 31, 2018
acecec6
mark collect as deprecated
dontgitit Oct 31, 2018
8b46b71
fix whitespace
dontgitit Oct 31, 2018
1cd3eac
Merge remote-tracking branch 'upstream/master'
dontgitit Mar 3, 2020
fb4e295
add internal data
dontgitit Mar 3, 2020
5ed8e52
add tests
dontgitit Mar 4, 2020
25f80ba
add tests against direct context lookup
dontgitit Mar 4, 2020
ca2937d
fix test name
dontgitit Mar 4, 2020
e0f7722
Merge pull request #1 from Iterable/create-internal-storage
dontgitit Mar 4, 2020
ae4e84a
Merge remote-tracking branch 'upstream/master'
dontgitit Aug 8, 2022
888f789
Merge branch 'jknack:master' into master
vbabenkoru Feb 23, 2024
3b6506f
[TNT-7113] Use commit hashes for github repo actions
dennisiterable Apr 30, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/build-matrix.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ jobs:

steps:
- name: Checkout
uses: actions/checkout@v1
uses: actions/checkout@50fbc622fc4ef5163becd7fab6573eac35f8462e
- name: Mustache Specs
run: |
git submodule update --init --recursive
- name: Set up JDK ${{ matrix.java_version }}
uses: actions/setup-java@v1
uses: actions/setup-java@b6e674f4b717d7b0ae3baee0fbe79f498905dfde
with:
java-version: ${{ matrix.java_version }}
- name: Install
Expand Down
48 changes: 48 additions & 0 deletions handlebars/src/main/java/com/github/jknack/handlebars/Context.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ protected BlockParam(final Context parent, final Map<String, Object> hash) {
this.extendedContext.resolver = parent.resolver;
this.parent = parent;
this.data = parent.data;
this.internalData = parent.internalData;
this.resolver = parent.resolver;
}

Expand Down Expand Up @@ -153,6 +154,7 @@ protected PartialCtx(final Context parent, final Object model, final Map<String,
this.extendedContext.extendedContext = new Context(Collections.emptyMap());
this.parent = parent;
this.data = parent.data;
this.internalData = parent.internalData;
this.resolver = parent.resolver;
}

Expand Down Expand Up @@ -432,6 +434,11 @@ public Object eval(final ValueResolver resolver, final Context context, final Ob
*/
protected Map<String, Object> data;

/**
* Functions similarly to data, but not resolved during rendering.
*/
protected Map<String, Object> internalData;

/**
* Additional, data can be stored here.
*/
Expand Down Expand Up @@ -470,6 +477,7 @@ private static Context root(final Object model) {
root.data.put(INLINE_PARTIALS, partials);
root.data.put(INVOCATION_STACK, new LinkedList<TemplateSource>());
root.data.put("root", model);
root.internalData = new HashMap<>();
return root;
}

Expand Down Expand Up @@ -535,6 +543,41 @@ public Context data(final Map<String, ?> attributes) {
return this;
}

/**
* Read the attribute from the internal data storage.
*
* @param name The attribute's name.
* @param <T> Data type.
* @return The attribute value or null.
*/
@SuppressWarnings("unchecked")
public <T> T internalData(final String name) {
return (T) internalData.get(name);
}

/**
* Set an attribute in the internal data storage.
*
* @param name The attribute's name. Required.
* @param value The attribute's value. Required.
* @return This context.
*/
public Context internalData(final String name, final Object value) {
internalData.put(name, value);
return this;
}

/**
* Store the map in the internal data storage.
*
* @param attributes The attributes to add. Required.
* @return This context.
*/
public Context internalData(final Map<String, ?> attributes) {
internalData.putAll(attributes);
return this;
}

/**
* Resolved as '.' or 'this' inside templates.
*
Expand Down Expand Up @@ -692,6 +735,9 @@ public void destroy() {
if (data != null) {
data.clear();
}
if (internalData != null) {
internalData.clear();
}
}
if (extendedContext != null) {
extendedContext.destroy();
Expand Down Expand Up @@ -790,6 +836,7 @@ private Context newChild(final Object model) {
child.setResolver(this.resolver);
child.parent = this;
child.data = this.data;
child.internalData = this.internalData;
return child;
}

Expand All @@ -813,6 +860,7 @@ protected Context newChildContext(final Object model) {
public static Context copy(final Context context, final Object model) {
Context ctx = Context.newContext(model);
ctx.data = context.data;
ctx.internalData = context.internalData;
ctx.resolver = context.resolver;
return ctx;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,10 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.jknack.handlebars.internal;
package com.github.jknack.handlebars;

import java.io.IOException;

import com.github.jknack.handlebars.Context;

/**
* Helper or hash param.
*
Expand Down
86 changes: 86 additions & 0 deletions handlebars/src/main/java/com/github/jknack/handlebars/Tag.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/**
* Copyright (c) 2012-2015 Edgar Espina
*
* This file is part of Handlebars.java.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.jknack.handlebars;

import java.util.List;

/**
* A Handlebars tag with its associated parameters and tag type.
*
*/
public class Tag {

/**
* The tag itself.
*/
private String tag;

/**
* A list of params for the tag.
*/
private List<Param> params;

/**
* The tag type of the tag.
*/
private TagType tagType;

/**
* Creates a new {@link Tag}.
*
* @param tag The tag itself.
* @param params The associated parameters for the tag.
* @param tagType The tag type for the tag.
*/
public Tag(final String tag, final List<Param> params, final TagType tagType) {
this.tag = tag;
this.params = params;
this.tagType = tagType;
}

/**
* The tag.
* @return The tag.
*/
public String getTag() {
return tag;
}

/**
* The tag's parameters.
* @return The tag's parameters.
*/
public List<Param> getParams() {
return params;
}

/**
* The tag type.
* @return The tag's type.
*/
public TagType getTagType() {
return tagType;
}

@Override
public String toString() {
return "Tag(" + this.tag + ", "
+ this.params.toString() + ", " + this.tagType + ")";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,21 @@ public List<String> collect(final TagType... tagType) {
return Collections.emptyList();
}

@Override
public List<Tag> collectWithParameters(final TagType... tagType) {
return Collections.emptyList();
}

@Override
public List<String> collectReferenceParameters() {
return Collections.emptyList();
}

@Override
public List<Param> collectAllParameters() {
return Collections.emptyList();
}

};

/**
Expand Down Expand Up @@ -201,9 +212,42 @@ public List<String> collectReferenceParameters() {
*
* @param tagType The tag type. Required.
* @return A list with tag names.
*
* @deprecated Use {@link #collectWithParameters(TagType...)} instead.
*/
@Deprecated
List<String> collect(TagType... tagType);

/**
* Collect all the tag names under the given tagType and their parameters.
* <p>
* Usage:
* </p>
*
* <pre>
* {{hello}}
* {{var 1}}
* {{{tripleVar}}}
* </pre>
* <p>
* <code>collectWithParameters(TagType.VAR)</code> returns
* <code>[Tag(hello, [], TagType.VAR), Tag(var, [1], TagType.VAR)]</code>
* </p>
* <p>
* <code>collect(TagType.TRIPLE_VAR)</code> returns
* <code>[Tag(tripleVar, [], TagType.TRIPLE_VAR)]</code>
* </p>
* <p>
* <code>collect(TagType.VAR, TagType.TRIPLE_VAR)</code> returns
* <code>[Tag(hello, [], TagType.VAR), Tag(var, [1], TagType.VAR),
* Tag(tripleVar, [], TagType.TRIPLE_VAR)]</code>
* </p>
*
* @param tagType The tag type. Required.
* @return A list of Tag.
*/
List<Tag> collectWithParameters(TagType... tagType);

/**
* Collects all the parameters which are also variables.
* <p>
Expand All @@ -222,6 +266,24 @@ public List<String> collectReferenceParameters() {
*/
List<String> collectReferenceParameters();

/**
* Collects all the parameters.
* <p>
* Usage:
* </p>
*
* <pre>
* {{#if v1}}{{/if}}
* {{#each v2 "test"}}{{/each}}
* </pre>
* <p>
* <code>collectAllParameters()</code> returns <code>[v1, v2]</code>
* </p>
*
* @return A list of Param.
*/
List<Param> collectAllParameters();

/**
* @return The template file's name.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@
import com.github.jknack.handlebars.Handlebars;
import com.github.jknack.handlebars.HandlebarsError;
import com.github.jknack.handlebars.HandlebarsException;
import com.github.jknack.handlebars.Param;
import com.github.jknack.handlebars.Tag;
import com.github.jknack.handlebars.TagType;
import com.github.jknack.handlebars.Template;
import com.github.jknack.handlebars.TypeSafeTemplate;
Expand Down Expand Up @@ -353,6 +355,16 @@ public List<String> collect(final TagType... tagType) {
return new ArrayList<>(tagNames);
}

@Override
public List<Tag> collectWithParameters(final TagType... tagType) {
isTrue(tagType.length > 0, "At least one tag type is required.");
List<Tag> tagsWithParams = new ArrayList<>();
for (TagType tt : tagType) {
collectWithParameters(tagsWithParams, tt);
}
return new ArrayList<>(tagsWithParams);
}

/**
* Child classes might want to check if they apply to the tagtype and append them self to the
* result list.
Expand All @@ -363,19 +375,40 @@ public List<String> collect(final TagType... tagType) {
protected void collect(final Collection<String> result, final TagType tagType) {
}

/**
* @param result The result list of Tag.
* @param tagType The matching tagtype.
*/
protected void collectWithParameters(final Collection<Tag> result,
final TagType tagType) {
}

@Override
public List<String> collectReferenceParameters() {
Set<String> paramNames = new LinkedHashSet<>();
collectReferenceParameters(paramNames);
return new ArrayList<>(paramNames);
}

@Override
public List<Param> collectAllParameters() {
Set<Param> params = new LinkedHashSet<>();
collectAllParameters(params);
return new ArrayList<>(params);
}

/**
* @param result The result list to add new parameters to.
*/
protected void collectReferenceParameters(final Collection<String> result) {
}

/**
* @param result The result list of Params to add new Params to.
*/
protected void collectAllParameters(final Collection<Param> result) {
}

@Override
public String toJavaScript() {
if (javaScript == null) {
Expand Down
Loading