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

Fixes and improvements #36

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open

Fixes and improvements #36

wants to merge 15 commits into from

Conversation

panosru
Copy link

@panosru panosru commented Jul 18, 2012

  • Fix related to typo mentioned on issue let instead of set? typo? #31 (also added let for legacy support)
  • Fix float filter so it return 0.0 instead of NaN
  • improved reverse filter supports number, string and array
  • Added outputUndefinedVariable flag
  • Added outputUndefinedVariableWithBrackets flag
  • Added npm-shrinkwrap.json for better dependency control

Panagiotis Kosmidis added 13 commits June 7, 2012 02:55
Added outputUndefinedVariable and outputUndefinedVariableWithBrackets flags
This reverts commit f2b3848.
Added support for let for compatibility purposes. (Deprecated)
Update tests
Added npm-shrinkwrap.json in order to better control the versions of dependencies (for instance pegjs 0.7.0 breaks tests so we make sure that we install 0.6.2)
@panosru
Copy link
Author

panosru commented Jul 18, 2012

Tests:

# ./test/parser.co -v

  ♢ Expressions Test Suite 

  The {% set %} tag
    ✓ Lets us assign to a variable that is later exported
    ✓ Does not output anything
    ✓ does not let us assign to reserved-words variables
    ✓ can have their results even across new lines
  The {% if %} and {% elseif %} tags
    ✓ displays text when its condition is met
    ✓ does not display text if its condition is not met
    ✓ displays its first close if its condition is met
    ✓ displays its else close if its condition is not met
    ✓ can have several ifelse clauses
    ✓ can have several elif clauses
    ✓ can not be condition-less
  The {% for %} tag
    ✓ is of the form <variable> in <array>
    ✓ is used to iterate over an array
    ✓ has a {{ loop.index0 }} variable
    ✓ Gives the index0 in the second variable if specified
    ✓ has a {{ loop.index }} variable
    ✓ has a {{ loop.first }} variable
    ✓ has a {{ loop.last }} variable
    ✓ is also of the form <key>, <value> in <object>
    ✓ outputs keys and values of objects
    ✓ accepts the empty object and does not output anything
    ✓ can have another {% for %} inside without messing up the variables
    ✓ does not leave its variable in the environment
    ✓ has an else clause that executes when the for loop did not run
    ✓ has an else clause that does not execute when the for loop did run
    ✓ Has a continue clause
    ✓ Has a break clause
    ✓ Has a break clause that can be nested in {% if %}
  The {{ print }} statement
    ✓ does not output undefined or null values
    ✓ displays the string result of an expression
  The |filters
    ✓ allow us to pass values through functions differently
    ✓ abs returns absolute value
    ✓ batch batches stuff
    ✓ capitalize capitalizes a string
    ✓ center centers a string
    ✓ date formats a date
    ✓ dictsort sorts an object
    ✓ escape or e escape an HTML string
    ✓ escapejs escape a string to make it javascript/or JSON safe
    ✓ groupby groups items
    ✓ indent indents a string
    ✓ join joins an array of strings or attributes of an object
    ✓ random selects a random item of a list
    ✓ replace replaces text
    ✓ round rounds numbers
    ✓ slice slices stuff
    ✓ sort sorts
    ✓ striptags removes HTML tags
    ✓ start does some funky sums
    ✓ title titleize text
    ✓ truncate truncates text
    ✓ urlize converts urls to links
    ✓ wordwrap wraps words
    ✓ xmlattr makes attributes
  The {% extends %} tag
    ✓ allows a template to extend another
    ✓ does not let us extend null or undefined variables
  The {% include %} tag
    ✓ allows us to include the contents of a template into another
    ✓ sends the current context to the included tag
    ✓ gets to modify the context
  The {% block %} tag
    ✓ can be nested
    ✓ can have redefined nested blocks
    ✓ defines a super() function for previous blocks
    ✓ defines a super() function for previous blocks that works with context
    ✓ can be redefined in child templates
    ✓ allows us to have blocks redefined in various child templates
  The {% import %} tag
    ✓ allows us to import variables from another template to another
    ✓ allows us to import a whole template as a module
    ✓ by default does not expose the current context to the imported module
    ✓ can pass the context to the imported template
  The {% abspath %} tag
    ✓ Outputs the absolute path of the given string
  The {% macro %} tag
    ✓ Allows us to define functions
    ✓ Can have arguments with default values
    ✓ Can have several arguments with default values
    ✓ have arguments which default values are overriden when specified in the function call
  The {% call %} tag
    ✓ Allows us to send additional, big content, to macros
    ✓ Allows us to send additional, big content, to macros, adding itself to its arguments

✓ OK » 76 honored (0.149s) 
# ./test/expr.co -v

  ♢ Expressions Test Suite 

  Simple operations don't change the operands
    ✓ When adding 2 + 2
  Strings and Regexps
    ✓ Single quote
    ✓ Single quote with escaping
    ✓ Double quotes
    ✓ Double quotes with escaping
    ✓ Regexp
    ✓ Regexp with / escaping
    ✓ Regexp with post qualifiers (gim)
  Numbers
    ✓ comprise also negative values
  Function Calls
    ✓ OOC function call
    ✓ Multiple arguments
    ✓ Function calling a function
    ✓ Function calling of a function result
  Dot operator tests
    ✓ IC leader is not prefixed, member neither
    ✓ OOC leader is prefixed, not member
    ✓ Can do function calls after dotted expression
    ✓ Function call with multiple argument
    ✓ Function call added with dotted expression
    ✓ More complex test mixing functions and dots
    ✓ Function chaining
    ✓ Function chaining II
  Filters
    ✓ can be named even with reserved words
    ✓ An OOC variable through a filter
    ✓ Dotted variable through a filter
    ✓ Filter taking an argument
    ✓ Filter with several arguments, one of which an OOC var
    ✓ Simple filter chaining
    ✓ More complex filter chaining + arguments
    ✓ Another complex filtering expression
    ✓ Arithmetics with a filter
    ✓ More complex arithmetics with filters
    ✓ Filtered function call
  Arrays and indexing
    ✓ Empty Array
    ✓ Empty Array with spaces
    ✓ Simple Array
    ✓ Array indexed with variable
    ✓ allow us to use . after them
    ✓ Allow us to write a[a][a]
  Assignment
    ✓ Complex Test
  The 'in' operator
    ✓ Gets replaced by a function
    ✓ Doesn't mess it up with the ternary operator
  The string operators (in, instanceof, etc..)
    ✓ are not eating up themselves
    ✓ Can tolerate having ( or ) instead of a space around them
  Object literals
    ✓ Can be empty (without space)
    ✓ Can be empty (with space)
    ✓ Can be created with regular labels, string labels and complex expressions
  More complex tests involving mixing of previous ones
    ✓ Function calls + filtered expression as argument
  Expressions
    ✓ Can have space around them
    ✓ can be empty
  Combination tests
    ✓ Chaining a function call and array call
    ✓ Lots of dots, arrays, and function chaining together
    ✓ Filter can't be directly chained with properties

✓ OK » 52 honored (0.023s)

Added options support but also left previous methods for legacy compatibility reasons.
@panosru
Copy link
Author

panosru commented Jul 19, 2012

Options method added


Now instead of having multiple methods when initializing jinjs engine you can have only options method which handles the rest. The jinjs.registerExtension and outputUndefinedVariable methods are also available for compatibility reasons with previous versions.

For instance instead of this:

require("jinjs");
    jinjs.registerExtension(".tpl", function (txt) {
      return my_transformation_module.transform_text(txt);
    });
    jinjs.registerExtension(".jin");
    jinjs.registerExtension(".html", function (txt) { return txt; });
    jinjs.outputUndefinedVariable(true, true);

you can have this:

require("jinjs").options({
  extensions : [
    { ext : '.tpl', filter : function (txt) {
      return my_transformation_module.transform_text(txt);
    }},
    { ext : '.jin' },
    { ext : '.html', filter : function (txt) { return txt; }}
  ],
  outputUndefinedVariable : true,
  outputUndefinedVariableWithBrackets : true
});

@Morgul
Copy link

Morgul commented Nov 16, 2012

Just a +1 to this. I'd really like to see these changes merged back into master.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants