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

V0.11 #218

Merged
merged 66 commits into from
Jan 22, 2018
Merged

V0.11 #218

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
66 commits
Select commit Hold shift + click to select a range
fa89304
Write new parser using pest 1.0
Aug 27, 2017
6211fce
Whitespace handling
Sep 19, 2017
5ce0c10
Update Template to use the new parser
Sep 19, 2017
c28b16f
Update rendering for new parser
Sep 20, 2017
4391ea2
Allow comment tags before extend tag
Sep 24, 2017
c56f256
More tests + WS handling in macro def
Sep 24, 2017
3e0d0ea
Fix some WS issue and add recursive macro test
Sep 24, 2017
1b90616
Add tests for whitespace
Sep 24, 2017
bb70ed2
Make NaiveDatetime work with date filter
Sep 24, 2017
50197cc
Update docs
Sep 25, 2017
5f747cf
Fix doccomment for Tera::parse()
nabijaczleweli Oct 12, 2017
960fc28
Merge pull request #221 from nabijaczleweli/v0.11
Keats Oct 22, 2017
385eb4e
Make pluralize return plural for 0 thing
Oct 22, 2017
7725a6e
Add 0 pluralize to the changelog
Oct 22, 2017
17af1d2
Update deps
Oct 25, 2017
40df87e
Do not cache on appveyor
Oct 25, 2017
1b1ca79
Add a set_global tag
Oct 25, 2017
b5f16b3
Add filter split
upsuper Oct 27, 2017
94cafdb
Merge pull request #225 from upsuper/split
Keats Oct 31, 2017
867bb9f
Add tester starting_with and ending_with
upsuper Oct 27, 2017
7917239
Handle parsing errors
Nov 2, 2017
6b36899
Enable rendering error tests
Nov 2, 2017
9fd393b
Merge pull request #224 from upsuper/starts-ends-with
Keats Nov 3, 2017
79d466a
Add starting_with and ending_with to changelog
Nov 3, 2017
f47d0b9
Add json_encode filter to print any value as a JSON string
Alex-PK Oct 28, 2017
cedbeb1
Documentation for json_encode filter
Alex-PK Nov 5, 2017
3d71650
Merge pull request #226 from Alex-PK/filter-json-encode
Keats Nov 6, 2017
6c0c10f
Add json_encode to the changelog
Nov 6, 2017
f3357a0
Slight clippy tweak
Nov 7, 2017
2b8bb25
Derive PartialEq for Context
jturner314 Nov 12, 2017
5e0cec0
Remove Sized constraint on T in Context.add()
jturner314 Nov 12, 2017
46e203d
Merge pull request #227 from jturner314/derive-partialeq-for-context
Keats Nov 13, 2017
fe8aced
Merge pull request #228 from jturner314/context-add-unsized
Keats Nov 13, 2017
db07fe5
Add containing test
Nov 19, 2017
d327c8a
Uncomment some error tests
Nov 21, 2017
89d4199
Add a default filter
Nov 21, 2017
dd4e888
Actually add containing tests to Tera and move tests around a bit
Nov 22, 2017
e46db03
Publish a first beta
Nov 24, 2017
b87a920
Import macros in the renderer at the beginning
Nov 24, 2017
251a4cb
Remove stray println
Nov 24, 2017
98699eb
Beta 1
Nov 24, 2017
f4de64e
Not sure why that file was still there
Nov 24, 2017
6101859
Allowed other type of quotes for strings #236
ragne Nov 30, 2017
0e2614a
Add support for sort filter #233
Dec 1, 2017
08d934c
bumped lazy_static to 1.0
alex Dec 3, 2017
4a85de2
Merge pull request #239 from alex/patch-1
Keats Dec 4, 2017
ebd4326
Add missing docs for some filters
Dec 5, 2017
6d44aa5
Remove 'reverse' attribute in sort filter
Dec 9, 2017
dd21df7
Fix docs url in lib.rs
Keats Dec 15, 2017
6e29de6
Prevent panics in sort filter implementation.
Dec 29, 2017
ad00286
Merge pull request #237 from ragne/v0.11
Keats Dec 29, 2017
110deeb
Fix formatting of lexer test vec
Dec 29, 2017
bcd07f6
Remove empty string special case from get_json_pointer.
Dec 31, 2017
7b6a0cc
Add strings identifiers to changelog
Jan 1, 2018
d419e07
Change spaces in tera.pest
Jan 2, 2018
5162e42
Move utility code for sort filter into its own file.
Jan 5, 2018
5831e58
Remove hyper 0.10 example
Jan 6, 2018
79113b7
Add documentation for sort filter.
Jan 7, 2018
f92fb61
Merge pull request #238 from hoggetaylor/v0.11
Keats Jan 8, 2018
74c3b91
Mention sort filter in changelog
Jan 9, 2018
484d662
Beta 2
Jan 10, 2018
3b62da6
Allow math operation on filters again
Jan 12, 2018
b0f8249
Beta 3
Jan 17, 2018
2f9cf11
Update to pest rc
Jan 18, 2018
5991002
Beta 4
Jan 19, 2018
316c6c5
Pest 1.0 has been released
Jan 22, 2018
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
27 changes: 27 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,32 @@
# Changelog

## 0.11.0 (unreleased)

### Breaking changes

- Tests parentheses are now mandatory if there are arguments (`divisibleby 2` -> `divisibleby(2)`)
- Tests can be only used on variables now, not on expressions
- Escaping happens immediately now instead of waiting for the filters to be called, unless `safe` is first.
If you want the old behaviour you will need to start the a chain of filters with `| safe` as the first one

### Others

- Tests, global functions calls and macro calls are now expressions and can be combined like so: `if x is divisibleby(2) and x > 10`
- Add default arguments for macro arguments
- Add whitespace management similar to Liquid and Jinja2
- Add parentheses to expressions to remove ambiguities
- Block & macro end tag name are no longer mandatory and it doesn't error on mismatched names between
the start and end tag anymore
- Filters can now be applied to expressions
- Add modulo operator `%` for math expressions
- Allow comment tags before the extend tag
- Make `NaiveDateTime` work with the `date` filter
- `pluralize` filter now returns the plural suffix for 0 thing as it's apparently what English does
- Add a `set_global` tag that allows you to set something in the global context: meant to be used in forloops where
the normal `set` would put the value into the loop context
- Add `starting_with`, `ending_with` and `containing` tests
- Add `json_encode`, `default` and `sort` filters
- Strings can now also be contained in backticks and single quotes in templates

## 0.10.10 (2017-08-24)

Expand Down
11 changes: 6 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "tera"
version = "0.10.10"
version = "0.11.0"
authors = ["Vincent Prouillet <[email protected]>"]
license = "MIT"
readme = "README.md"
Expand All @@ -14,14 +14,15 @@ categories = ["template-engine"]
glob = "0.2"
serde = "1.0"
serde_json = "1.0"
pest = "0.4"
error-chain = "0.10"
pest = "^1"
pest_derive = "^1"
error-chain = "0.11"
# used in slugify filter
slug = "0.1"
# used in striptags filters
regex = "0.2"
# used for static regex
lazy_static = "0.2"
lazy_static = "1.0"
# used in urlencode filter
url = "1"
# used in filesizeformat filter
Expand All @@ -31,4 +32,4 @@ chrono = "0.4"

[dev-dependencies]
serde_derive = "1.0"
hyper = "0.10"
pretty_assertions = "0.4"
4 changes: 0 additions & 4 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@ install:
- rustc -Vv
- cargo -V

cache:
- C:\Users\appveyor\.cargo\registry
- target

build: false

test_script:
Expand Down
2 changes: 0 additions & 2 deletions benches/tera.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#![feature(test)]
extern crate test;
extern crate tera;
extern crate serde;
extern crate serde_json;
#[macro_use]
extern crate serde_derive;

Expand Down
4 changes: 2 additions & 2 deletions docs/content/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
```jinja2
<title>{% block title %}{% endblock title %}</title>
<ul>
{% for user in users %}
{% for user in users -%}
<li><a href="{{ user.url }}">{{ user.username }}</a></li>
{% endfor %}
{%- endfor %}
</ul>
```
2 changes: 1 addition & 1 deletion docs/content/docs/installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ order = 1
To use Tera in your Rust projects, simply add it to your `Config.toml`:

```toml
tera = "0.10"
tera = "0.11"
```

And add the following to your `lib.rs` or `main.rs`:
Expand Down
160 changes: 153 additions & 7 deletions docs/content/docs/templates.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ when it is rendered. The syntax is based on Jinja2 and Django templates.
There are 3 kinds of delimiter and those cannot be changed:

- `{{` and `}}` for expressions
- `{%` and `%}` for statements
- `{%` or `{%-` and `%}` or `-%}` for statements
- `{#` and `#}` for comments

## Variables
Expand All @@ -27,7 +27,7 @@ A magical variable is available in every template if you want to print the curre

## Expressions

Tera allows expressions everywhere.
Tera allows expressions almost everywhere.

### Math
You can do some basic math in Tera but it shouldn't be abused other than the occasional `+ 1` or similar.
Expand All @@ -38,6 +38,12 @@ You can use the following operators:
- `-`: performs a substraction, `{{ 2 - 1 }}` will print `1`
- `/`: performs a division, `{{ 10 / 2 }}` will print `5`
- `*`: performs a multiplication, `{{ 5 * 2 }}` will print `10`
- `%`: performs a modulo, `{{ 2 % 2 }}` will print `0`

The priority of operations is the following, from lowest to highest:

- `+` and `-`
- `*` and `/` and `%`

### Comparisons

Expand All @@ -61,7 +67,7 @@ Filters are separated from the variable by a pipe symbol (|) and may have named
Multiple filters can be chained: the output of one filter is applied to the next.

For example, `{{ name | lower | replace(from="doctor", to="Dr.") }}` will take a variable called name, make it lowercase and then replace instances of `doctor` by `Dr.`.
It is equivalent to `replace(lower(name), from="doctor", to="Dr.")` if we were to look at it as function
It is equivalent to `replace(lower(name), from="doctor", to="Dr.")` if we were to look at it as functions.

Calling filters on a incorrect type like trying to capitalize an array or using invalid types for arguments will result in a error.

Expand All @@ -71,6 +77,17 @@ Filters are functions with the `fn(Value, HashMap<String, Value>) -> Result<Valu
tera.register_filter("upper", string::upper);
```

While filters can be used in math operations, they will have the lowest priority and therefore might not do what you expect:


```css
{{ 1 + a | length }}
// is equal to
{{ (1 + a) | length } // this will probably error

// This will do what you wanted initially
{{ a | length + 1 }}
```
Tera has many [built-in filters](./docs/templates.md#built-in-filters) that you can use.

### Filter sections
Expand All @@ -93,7 +110,7 @@ are made in `if` blocks using the `is` keyword.
For example, you would write the following to test if an expression is odd:

```jinja2
{% if my_number + 1 is odd %}
{% if my_number is odd %}
Odd
{% endif %}
```
Expand Down Expand Up @@ -160,6 +177,16 @@ assignments outside of those will be set in the global context.
{% set my_var = global_fn() %}
```

If you want to assign a value in the global context while in a forloop, you can use `set_global`:

```jinja2
{% set_global my_var = "hello" %}
{% set_global my_var = 1 + 4 %}
{% set_global my_var = some_var %}
{% set_global my_var = macros::some_macro() %}
{% set_global my_var = global_fn() %}
```

## Comments
To comment out part of the template, wrap it in `{# #}`. Anything in between those tags
will not be rendered.
Expand Down Expand Up @@ -376,6 +403,32 @@ render what's inside. Useful if you have text that contains Tera delimiters.
```
would be rendered as `Hello {{ name }}`.

## Whitespace control

Tera comes with easy to use whitespace control: use `{%-` if you want to remove all whitespace
before a statement and `-%}` if you want to remove all whitespace after.

For example, let's look at the following template:

```jinja2
{% set my_var = 2 %}
{{ my_var }}
```

will have the following output:

```html

2
```

If we want to get rid of the empty line, we can write the following:

```jinja2
{% set my_var = 2 -%}
{{ my_var }}
```

## Built-in filters

Tera has the following filters built-in:
Expand Down Expand Up @@ -406,17 +459,26 @@ If value is "I'm using Tera", the output will be "I\'m using Tera".
Transform a string into ASCII, lowercase it, trim it, converts spaces to hyphens and
remove all characters that are not numbers, lowercase letters or hyphens.

Example: `{{ value | slugify}}`
Example: `{{ value | slugify }}`

If value is "-Hello world! ", the output will be "hello-world".

### title
Capitalizes each word inside a sentence.

Example: `{{ value | title}}`
Example: `{{ value | title }}`

If value is "foo bar", the output will be "Foo Bar".

### trim
Remove leading and trailing whitespace if the variable is a string.

### truncate
Truncates a string to the indicated length. If the string has a smaller length than
the `length` argument, the string is returned as is.

Example: `{{ value | truncate(length=10) }}`

### striptags
Tries to remove HTML tags from input. Does not guarantee well formed output if input is not valid HTML.

Expand Down Expand Up @@ -445,6 +507,43 @@ Returns the length of an array or a string, 0 if the value is not an array.
### reverse
Returns a reversed string or array.

### sort
Sorts an array into ascending order.

The values in the array must be a sortable type:
- numbers are sorted by their numerical value.
- strings are sorted in alphabetical order.
- arrays are sorted by their length.
- bools are sorted as if false=0 and true=1

If you need to sort a list of structs or tuples, use the `attribute`
argument to specify which field to sort by.

Example:

Given `people` is an array of Person

```rust
struct Name(String, String);

struct Person {
name: Name,
age: u32
}
```

The `attribute` argument can be used to sort by last name:

```jinja2
{{ people | sort(attribute="name.1") }}
```

or by age:

```jinja2
{{ people | sort(attribute="age") }}
```

### urlencode
Percent-encodes a string.

Expand Down Expand Up @@ -503,6 +602,24 @@ Currently the position of the safe filter does not matter, e.g.
Access a value from an object when the key is not a Tera identifier.
Example: `{{ sections | get(key="posts/content") }}`

### split
Split a string into an array of strings, separated by a pattern given.
Example: `{{ path | split(pat="/") }}`

### json_encode
Transforms any value into a JSON representation. This filter is better used together with `safe` or when automatic escape is disabled.

Example: `{{ value | safe | json_encode() }}`

It accepts a parameter `pretty` (boolean) to print a formatted JSON instead of a one-liner.

Example: `{{ value | safe | json_encode(pretty=true) }}`

### default
Returns the default value given if the variable evaluated is not present in the context.

Example: `{{ value | default(value=1) }}`

## Built-in tests

Here are the currently built-in tests:
Expand Down Expand Up @@ -530,14 +647,43 @@ Returns true if the given expression is divisible by the arg given.

Example:
```jinja2
{% if rating is divisibleby 2 %}
{% if rating is divisibleby(2) %}
Divisible
{% endif %}
```

### iterable
Returns true if the given variable can be iterated over in Tera (ie is an array/tuple).

### starting\_with
Returns true if the given variable is a string starts with the arg given.

Example:
```jinja2
{% if path is starting_with("x/") %}
In section x
{% endif %}
```

### ending\_with
Returns true if the given variable is a string ends with the arg given.

### containing
Returns true if the given variable contains the arg given.

The test works on:

- strings: is the arg a substring?
- arrays: is the arg given one of the member of the array?
- maps: is the arg given a key of the map?

Example:
```jinja2
{% if username is containing("xXx") %}
Bad
{% endif %}
```

## Built-in global functions
Tera comes with some built-in global functions.

Expand Down
Loading