Commit Graph

51 Commits

Author SHA1 Message Date
Michael Bayne 546f78318e Simple support for decompiling fragments.
There are limitations! See the javadoc for Template.Fragment.decompile.

Closes #75.
2016-04-14 08:32:07 -07:00
Michael Bayne 2b376d3189 Align "compact" Javadocs more sanely. 2015-03-06 12:24:13 -08:00
Michael Bayne 8618f89f7e Added strictSections configuration.
Section behavior used to be somewhat hackily determined from nullValue and
defaultValue, but it was messy. Now it has its own configuration and things are
more explicit.

Previously missing/null sections were treated as false, but if you set
nullValue to something, then missing sections became an error, but null
sections remained false. If you set defaultValue to something, missing sections
went back to being omitted. Kooky.

Now missing sections are omitted, unless you set strictSections to true, in
which case they become an error. nullValue and defaultValue have no impact on
section behavior.

There is no way to make null sections an error, but I don't see any demand for
that, so I'm going to leave that as a TODO.

Closes #60.
2014-12-20 11:06:46 -08:00
Michael Bayne f838c91f99 Attempt to resolve compound keys "whole" first.
This allows one to have a map with keys like "foo.bar.baz" even when not in
standards mode. If a whole key is missing, then we attempt to decompose it and
look it up piecewise.

Closes #55.
2014-05-08 09:49:33 -07:00
Michael Bayne 2f267b7199 Make NO_FETCHER_FOUND available to external parties.
Closes #54.
2014-05-02 11:30:31 -07:00
Michael Bayne 20abde5b1a Expose the context to a lambda fragment.
This allows the lambda to take the context and turn around and render it using
a totally different template, if desired.
2014-04-16 14:26:22 -07:00
Michael Bayne 9861b54aa0 Formatting, and small javadoc improvement. 2014-03-18 09:26:38 -07:00
robbytx 75609b42df Add ability for lambda to change the context for fragment execution 2014-03-14 09:50:19 -05:00
Michael Bayne 9372c7009e Cache a NOT_FOUND_FETCHER when no fetcher is found.
Closes #45.

This avoids the expensive process of repeatedly looking for a fetcher in a
context where one doesn't exist.
2014-01-02 11:24:36 -08:00
Michael Bayne 6cfc376e8d Rewrote array/list fetch-by-index using collectors.
That's what collectors are for. This also avoids the needless expense of doing
Integer.parseInt on every single key (the vast majority of which will not
actually be numbers), every single time we do a context lookup.
2013-10-07 14:25:58 -07:00
Mickael Jeanroy 98c7a5d38f Access Array item by index in template 2013-10-06 10:06:48 +02:00
Michael Bayne 9d77d492b0 Expose NO_FETCHER_FOUND as custom fetchers may wish to return it. 2013-04-05 11:01:37 -07:00
Yoryos Valotasios 2889ee8692 Added standard Mustache specs tests suite.
The groups with failing tests are currently commented out (everything except
interpolation). The main reason for failures are due to the requirement that
standalone lines that contain nothing but whitespace be stripped from the
output.
2013-04-02 12:54:20 -07:00
Michael Bayne 3a5da915e9 These days I always cuddle method braces. 2013-03-27 10:37:41 -07:00
Michael Bayne 43db03a26a Changed MustacheContextException -> MustacheException.Context.
Also made data into final fields. I don't like unnecessary getters.
2013-03-05 20:55:01 -08:00
pmenhart c61650734d Added MustacheContextException
Solving need to distinguish context errors from environment errors.
  The exception has fields for the offending key and line number. This
  is a first step towards internationalization of messages.
2013-03-05 23:33:48 -05:00
Michael Bayne acbddd07a2 Added basic lambda support. 2013-02-15 16:38:36 -08:00
Michael Bayne 8f87bf374d Some tweaks to the 'substitute name into null value' patch. 2013-01-23 17:43:46 -08:00
pmenhart 8b0d063ffd Show the name of variable that is null or missing
If Compliler.nullValue contains a substring "{{name}}", then this substring
will be replaced with the name of variable.
For example, if nullValue="?{{name}}?" and variable is resolved to null,
then string "?foo?" will be used.
2013-01-23 15:59:52 -05:00
Michael Bayne 6e71dad5f2 Simplify logic now that null is always returned. 2012-12-04 16:48:23 -08:00
Michael Bayne 65f8d12d62 Merge pull request #22 from tominsam/master
allow inner null values to mask parent context values
2012-12-04 16:38:13 -08:00
Michael Bayne 5ec24e947c Use ConcurrentHashMap to cache fetchers, on platforms that support it. 2012-10-01 09:27:36 -07:00
Tom Insam 2b337b8770 null values can shadow parent context now. Improve test a little. 2012-08-22 15:19:00 +01:00
Michael Bayne 718639127e Added comments and squeezed into 100 columns. 2011-11-22 11:40:09 -08:00
Jacob Lewallen 1a06a0233a Add a Template.execute that takes a parent context. 2011-11-22 11:20:59 -08:00
Michael Bayne f6c6f4916c Pass the context instance itself instead of its class.
In GWT we can't use Class.isAssignableFrom. We need the original instance so
that we can do (ctx instanceof Map<?,?>).
2011-10-29 15:29:18 -07:00
Michael Bayne adf116992c Modified compound variables to honor null/default configuration. Closes #10.
Kali/Bertrand convinced me that being consistent with other Mustache
implementations is more important than whatever wacky reasons I was harboring
for keeping compound variables different than singleton sections.
2011-10-29 14:38:56 -07:00
Michael Bayne 76e9d54683 Extracted the iteration over collections and creation of variable fetchers into
a pluggable component.

This allows scaling down to the GWT environment by hiding the reflection code
via GWT's super source mechanism.

And it allows scaling up to non-Java languages (like Scala) by allowing the
library client to extend the DefaultCollector and teach it about Scala
Iterators, Iterables and Maps.

I also just realized that GWT has no facilities for Reader and Writer, which is
going to complicate making things work in GWT even further. But for now, I'll
just leave things as is.
2011-10-13 20:35:46 -07:00
Michael Bayne a3eb9e22e1 Fixed bug where -last was not correctly handled on lists of a single element,
where said element is both -first and -last.
2011-08-29 13:51:11 -07:00
Michael Bayne 28978548c5 Wrapped some long lines. 2011-08-29 13:42:11 -07:00
Michael Bayne 7255cb7c09 We keep the @Override on the same line round these parts. 2011-07-17 08:38:20 -07:00
David Hoover b0737e3f2b Add missing @Overrides 2011-07-01 11:25:52 -07:00
Michael Bayne 9fae13e008 Added Compiler.nullValue() for situations where one wants to provide a value
for use when variables resolve to null, but still wishes for exceptions to be
thrown when values are not resolvable (i.e. are typos). Pre-existing behavior
is preserved if one uses defaultValue() or does not change the default
configuration.

Nota bene: there are complexities, so be sure to read the documentation.
2011-05-28 10:42:45 -07:00
Michael Bayne ef971015bd Resolves issue 7 regarding proper handling of nested contexts when processing a
nested context.
2011-05-26 14:52:45 -07:00
Michael Bayne 15ff48eabb Some tidying up, name changes and clarifications. I went with "defaultValue"
rather than "missingVariableValue" because the value is used any time a
variable resolves to null, not necessarily just when it is missing. If you
have a context like:

new Object() {
   String foo = null;
}

and reference {{foo}} in your template, foo is not missing, per se, but the
default value will still be used for it.
2011-04-15 12:35:33 -07:00
John Montgomery 5d59eada29 Added compiler option to provide defaults for missing values
Call missingVariableValue(value) on a Compiler object to get a new
version that will insert "value" for missing values (rather than
throwing an exception).  Useful when you have templates you would
rather generated *something* instead of erroring or for when objects may
have null values themselves.
2011-04-15 10:01:40 +01:00
Michael Bayne e8641ba4fa We have interned strings here, so we can use reference equality to determine
whether we're seeing {{.}} or not. Also reinstated treatment of ".foo" as a
compound key. I'm not 100% sure that makes more sense than treating ".foo" as a
simple key, but I'd rather change less behavior than more when making the {{.}}
fix.
2011-04-13 11:38:26 -07:00
Sean Scanlon b3f5e1e960 tighten up fix for {{.}}
realizing previous commit would treat {{.foo}} the same as {{.}}
2011-03-27 16:18:17 -07:00
Sean Scanlon 095c6f9a59 fix for using {{.}} in non-standards mode
in non-standards mode (which is the default), {{.}} would be treated as a compound key, resulting in key not found exceptions.
2011-03-27 16:07:20 -07:00
Michael Bayne f4cfa40c2b Added a "standards mode" based on patches from mrdon. In standards mode, we do
not search parent contexts for name bindings, nor do we support compound keys.
Also added support for {{.}} which has the same semantics as {{this}}. I must
have missed this when reading the Mustache spec.
2011-02-28 11:52:40 -08:00
Michael Bayne 773e1c5c6e Bah, let's just have the copyright in the LICENSE file. If that doesn't hold up
in a court of law, oh well. My sense of aesthetics trumps all!
2010-11-02 14:19:33 -07:00
Michael Bayne 84691b7ac3 Let's have a license and a proper source header. (Thanks Dave!) 2010-11-02 14:18:16 -07:00
Michael Bayne 7ec57412fa A patch from Charlie to provide line numbers when reporting variable related
errors during execution.
2010-10-23 16:35:38 +00:00
Michael Bayne 00113a5a36 Added -index special variable. 2010-10-22 05:29:31 +00:00
Michael Bayne 0b9b01dab6 Fixed some bugs with newline skipping. Improved error handling when a tag is
not closed properly and another tag follows it on the same line. Added support
for -first and -last special variables. Fixed problem with resolving outermost
component of a compound key in parent contexts.
2010-10-22 05:02:09 +00:00
Michael Bayne 5932123d79 Added support for nesting contexts, which allows us to resolve variables in
parent contexts if they're not available in our current context.
2010-10-22 01:06:06 +00:00
Michael Bayne 0bef6a06e5 Added support for composite keys, e.g. foo.bar.baz, which extracts foo from the
context, then extracts bar from that object, then extracts baz from that.
2010-10-21 23:06:15 +00:00
Michael Bayne 134a22bcfd Added support for "this" variable which refers to the context itself rather
than a member of the context.
2010-10-21 21:28:06 +00:00
Michael Bayne a7984b1594 Javadoc fixes, properly skip void methods. 2010-10-21 19:42:38 +00:00
Michael Bayne 1e89b87cb0 Added support for obtaining variable values from fields and methods. Added a
template-local variable fetching cache that avoids reresolving the source of a
variable when it remains monomorphic (e.g. it's always a field of class
Foo.class or always a method named getFoo(), etc.).
2010-10-21 19:26:40 +00:00