Filters For Variable Output #4

Merged
merged 11 commits into from Aug 7, 2011

Conversation

Projects
None yet
2 participants
@paularmstrong
Contributor

paularmstrong commented Aug 7, 2011

Added filters for variable output. Handy for transcoding raw data into something useful for display. This is definitely just a start, as they are relatively easy to implement. It'd be great to also look into a way to add custom filters.

Usage

{{ foo|filter }}
{{ foo|filter('options') }}

Example:

{{ myDate|date('F jS, Y') }} // August 6th, 2011
{{ foo|title }} // converts 'my title' to 'My Title'
{{ myArray|join(', ')|title }} // join array and then title case

Currently Implemented

  1. lower
  2. upper
  3. capitalize
  4. title
  5. join
  6. length
  7. url_encode
  8. url_decode
  9. json_encode
  10. striptags
  11. date

Tests!

Also added node unit as a devDependency and wrote simple unit tests for all filters.

@paularmstrong

This comment has been minimized.

Show comment
Hide comment
@paularmstrong

paularmstrong Aug 7, 2011

Contributor

Forgot to note: Also added pre-commit and post-merge hooks that will, once you've run make once, always auto-update themselves. The pre-commit hook strips trailing whitespace from all changed files and ensures JSLint and all tests pass before allowing any commits.

Contributor

paularmstrong commented Aug 7, 2011

Forgot to note: Also added pre-commit and post-merge hooks that will, once you've run make once, always auto-update themselves. The pre-commit hook strips trailing whitespace from all changed files and ensures JSLint and all tests pass before allowing any commits.

skid added a commit that referenced this pull request Aug 7, 2011

Merge pull request #4 from paularmstrong/master
Filters For Variable Output

@skid skid merged commit 89a5cb8 into skid:master Aug 7, 2011

@skid

This comment has been minimized.

Show comment
Hide comment
@skid

skid Aug 7, 2011

Owner

Merged this one. Great job.

P.S. It's good to do this when you're creating the compiled code.

    output = '__filters.' + matches[1] + '(' + variable + ', ' + matches[2] + ')';

instead of

    output = '__filters["' + matches[1] + '"](' + variable + ', ' + matches[2] + ')';

Since we already have the filters predefined in the module and dot-based property lookup is faster.
I'll get to that and escaping the filter arguments in a few days if you don't do it first :)

Owner

skid commented Aug 7, 2011

Merged this one. Great job.

P.S. It's good to do this when you're creating the compiled code.

    output = '__filters.' + matches[1] + '(' + variable + ', ' + matches[2] + ')';

instead of

    output = '__filters["' + matches[1] + '"](' + variable + ', ' + matches[2] + ')';

Since we already have the filters predefined in the module and dot-based property lookup is faster.
I'll get to that and escaping the filter arguments in a few days if you don't do it first :)

@paularmstrong

This comment has been minimized.

Show comment
Hide comment
@paularmstrong

paularmstrong Aug 7, 2011

Contributor

Yeah, I went with brackets as the "safer" bet, but I guess that it's an arguable tradeoff: should there be an enforcement mechanism for (future) custom-filters to require being able to be used with dot notation? (I think yes)

Contributor

paularmstrong commented Aug 7, 2011

Yeah, I went with brackets as the "safer" bet, but I guess that it's an arguable tradeoff: should there be an enforcement mechanism for (future) custom-filters to require being able to be used with dot notation? (I think yes)

@skid

This comment has been minimized.

Show comment
Hide comment
@skid

skid Aug 7, 2011

Owner

I think it's better to make filter names only valid JS variable names and we can check while compiling.

Owner

skid commented Aug 7, 2011

I think it's better to make filter names only valid JS variable names and we can check while compiling.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment