Compare commits
85 Commits
0.6.0-beta
...
0.9.0
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
65a461d0a1 | ||
|
|
89256b96f4 | ||
|
|
a8d680b30e | ||
|
|
6f48fe2d91 | ||
|
|
4f9063c147 | ||
|
|
5541eae818 | ||
|
|
d1717df6ff | ||
|
|
dc8759ba34 | ||
|
|
233dcfc59a | ||
|
|
19e4f6e506 | ||
|
|
e2f33d4337 | ||
|
|
20b9476e4b | ||
|
|
cf9714ffd0 | ||
|
|
5e78d6cc46 | ||
|
|
99efba56e9 | ||
|
|
39517b7514 | ||
|
|
fdde1dec02 | ||
|
|
8f6b403aa9 | ||
|
|
2331b11a52 | ||
|
|
26f30cbd9d | ||
|
|
d7b152089e | ||
|
|
d3706f074d | ||
|
|
aa7c36296b | ||
|
|
2d507e7c11 | ||
|
|
15facd97fb | ||
|
|
d75db241ac | ||
|
|
49936c36d4 | ||
|
|
2e04a71d59 | ||
|
|
6d05832997 | ||
|
|
6871387671 | ||
|
|
46bc1242f3 | ||
|
|
24359489ce | ||
|
|
f90597fba1 | ||
|
|
9e2a061795 | ||
|
|
2be672c6a5 | ||
|
|
2ebb79df8b | ||
|
|
63c2b935f7 | ||
|
|
abae80d39d | ||
|
|
d024da5567 | ||
|
|
98edad3566 | ||
|
|
872784f9b3 | ||
|
|
1a01ec592e | ||
|
|
f0591408be | ||
|
|
b7e200a8a0 | ||
|
|
b1da85b140 | ||
|
|
679344f53b | ||
|
|
ada4e81082 | ||
|
|
c99a40c5d9 | ||
|
|
c59b263446 | ||
|
|
ab6f1a032d | ||
|
|
e989317929 | ||
|
|
111306fb60 | ||
|
|
3eb2657a62 | ||
|
|
6ad609e562 | ||
|
|
38d7ec87f6 | ||
|
|
9af9cf4005 | ||
|
|
1975cfd627 | ||
|
|
429290e0b7 | ||
|
|
5ca1b78854 | ||
|
|
a2673bd66b | ||
|
|
9b6ee14aa3 | ||
|
|
3b5e8f2468 | ||
|
|
e84f8a41d4 | ||
|
|
2324808dca | ||
|
|
9fdbbc99e9 | ||
|
|
dfd57e9571 | ||
|
|
3293d8a526 | ||
|
|
393dc88a10 | ||
|
|
a014fecd23 | ||
|
|
a13401b046 | ||
|
|
60b378d482 | ||
|
|
1e3afc0dd5 | ||
|
|
72f3cb579a | ||
|
|
68e6ce3022 | ||
|
|
65c3052aee | ||
|
|
7bbd4f2817 | ||
|
|
7416e6150d | ||
|
|
feff3b18b1 | ||
|
|
f393efbd0b | ||
|
|
df650c6b20 | ||
|
|
3285bac373 | ||
|
|
80427a51e6 | ||
|
|
7bfb69cc82 | ||
|
|
5007ba2c9a | ||
|
|
2d73c58df6 |
@@ -1 +1 @@
|
|||||||
DEVELOPMENT-SNAPSHOT-2016-01-25-a
|
3.1
|
||||||
|
|||||||
10
.travis.yml
10
.travis.yml
@@ -1,11 +1,11 @@
|
|||||||
os:
|
os:
|
||||||
- osx
|
- osx
|
||||||
- linux
|
- linux
|
||||||
language: generic
|
language: generic
|
||||||
sudo: required
|
sudo: required
|
||||||
dist: trusty
|
dist: trusty
|
||||||
osx_image: xcode7.2
|
osx_image: xcode8
|
||||||
install:
|
install:
|
||||||
- eval "$(curl -sL https://gist.githubusercontent.com/kylef/5c0475ff02b7c7671d2a/raw/02090c7ede5a637b76e6df1710e83cd0bbe7dcdf/swiftenv-install.sh)"
|
- eval "$(curl -sL https://gist.githubusercontent.com/kylef/5c0475ff02b7c7671d2a/raw/9f442512a46d7a2af7b850d65a7e9bd31edfb09b/swiftenv-install.sh)"
|
||||||
script:
|
script:
|
||||||
- make test
|
- swift test
|
||||||
|
|||||||
175
ARCHITECTURE.md
175
ARCHITECTURE.md
@@ -1,175 +0,0 @@
|
|||||||
Stencil Architecture
|
|
||||||
====================
|
|
||||||
|
|
||||||
This document outlines the architecture of Stencil and how it works internally.
|
|
||||||
|
|
||||||
Stencil uses a three-step process for rendering templates. The first step is tokenising the template into an array of Token’s. Afterwards, the array of token’s are transformed into a collection of Node’s. Once we have a collection of Node’s (objects conforming to the `Node` protocol), we then call `render(context)` on each Node instructing it to render itself inside the given context.
|
|
||||||
|
|
||||||
## Token
|
|
||||||
|
|
||||||
Token is an enum which has four members. These represent a piece of text, a variable, a comment or a template block. They are parsed using the `TokenParser` which takes the template as a string as input and returns an array of Token’s.
|
|
||||||
|
|
||||||
### Values
|
|
||||||
|
|
||||||
#### Text
|
|
||||||
|
|
||||||
A text token represents a string which will be rendered in the template. For example, a text token with the string `Hello World` will be rendered as such in the output.
|
|
||||||
|
|
||||||
#### Variable
|
|
||||||
|
|
||||||
A variable token represents a variable inside a context. It will be evaluated and rendered in the output. It is created from the template using `{{ string }}`.
|
|
||||||
|
|
||||||
#### Comment
|
|
||||||
|
|
||||||
The comment token represents a comment inside the source. It is created using `{# This is a comment #}`.
|
|
||||||
|
|
||||||
#### Block
|
|
||||||
|
|
||||||
A block represents a template tag. It is created using `{% this is a template block %}` inside a template. The template tag in this case would be called `this`. See “Block Token” below for more information.
|
|
||||||
|
|
||||||
### Parsing
|
|
||||||
|
|
||||||
A template is parsed using the TokenParser into an array of Token’s. For example:
|
|
||||||
|
|
||||||
```html+django
|
|
||||||
Hello {{ name }}
|
|
||||||
```
|
|
||||||
|
|
||||||
Would be parsed into two tokens. A token representing the string, `Hello ` and a token representing the variable called `name`. So, in Swift it would be represented as follows:
|
|
||||||
|
|
||||||
```swift
|
|
||||||
let tokens = [
|
|
||||||
Token.Text("Hello "),
|
|
||||||
Token.Variable("name"),
|
|
||||||
]
|
|
||||||
```
|
|
||||||
|
|
||||||
## Node
|
|
||||||
|
|
||||||
Node is a protocol with a single method, to render it inside a context. When rendering a node, it is converted into the output string, or an error if there is a failure. Token’s are converted to Node’s using the `TokenParser` class.
|
|
||||||
|
|
||||||
For some Token’s, there is a direct mapping from a Token to a Node. However block node’s do not have a 1:1 mapping.
|
|
||||||
|
|
||||||
### Token Parsing
|
|
||||||
|
|
||||||
#### Text Token
|
|
||||||
|
|
||||||
A text token is converted directly to a `TextNode` which simply returns the text when rendered.
|
|
||||||
|
|
||||||
#### Variable Token
|
|
||||||
|
|
||||||
Variable Token’s are transformed directly to a `VariableNode`, which will evaluate a variable in the given template when rendered.
|
|
||||||
|
|
||||||
#### Comment Token
|
|
||||||
|
|
||||||
A comment token is simply omitted, a comment token will be dropped when it is converted to a Node.
|
|
||||||
|
|
||||||
#### Block Token
|
|
||||||
|
|
||||||
Block token’s are slightly different from the other tokens, there is no direct mapping. A block token is made up of a string representing the token. For example `now` or `for article in articles`. The `TokenParser` will pull out the first word inside the string representation and use that to look-up a parser for the block. So, in this example, the template tag names will be `now` or `for`.
|
|
||||||
|
|
||||||
The template tag’s are registered with a block of code which deals with the parsing for the given tag. This allows the parser to parse a set of tokens ahead of the block tone. This is useful for control flow, such as the `for` template tag will want to parse any following tokens up until the `endblock` block token.
|
|
||||||
|
|
||||||
For example:
|
|
||||||
|
|
||||||
```html+django
|
|
||||||
{% for article in articles %}
|
|
||||||
An Article
|
|
||||||
{% endfor %}
|
|
||||||
```
|
|
||||||
|
|
||||||
Or as a set of tokens:
|
|
||||||
|
|
||||||
```swift
|
|
||||||
let tokens = [
|
|
||||||
Token.Block("for article in articles"),
|
|
||||||
Token.Text(" An Article")
|
|
||||||
Token.Block("endfor")
|
|
||||||
]
|
|
||||||
```
|
|
||||||
|
|
||||||
Will result in a single Node (a `ForNode`) which contains the sub-node containing the text. The `ForNode` class has a property called `forNodes` which contains the text node representing the text token (` An Article`).
|
|
||||||
|
|
||||||
When the `ForNode` is rendered in a context, it will look up the variable `articles` and if it’s an array it will loop over it. Inserting the variable `article` into the context while rendered the `forNodes` for each article.
|
|
||||||
|
|
||||||
### Custom Nodes
|
|
||||||
|
|
||||||
There are two ways to register custom template tags. A simple way which allows you to map 1:1 a block token to a Node. You can also register a more advanced template tag which has it’s own block of code for handling parsing if you want to parse up until another token such as if you are trying to provide flow-control.
|
|
||||||
|
|
||||||
The tags are registered with a `Namespace` passed when rendering your `Template`.
|
|
||||||
|
|
||||||
#### Simple Tags
|
|
||||||
|
|
||||||
A simple tag is registered with a string for the tag name and a block of code which is evaluated when the block is rendered in a given context.
|
|
||||||
|
|
||||||
Here’s an example. Registering a template tag called `custom` which just renders `Hello World` in the rendered template:
|
|
||||||
|
|
||||||
```swift
|
|
||||||
namespace.registerSimpleTag("custom") { context in
|
|
||||||
return "Hello World"
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
You would use it as such in a template:
|
|
||||||
|
|
||||||
```html+django
|
|
||||||
{% custom %}
|
|
||||||
```
|
|
||||||
|
|
||||||
#### Advanced Tags
|
|
||||||
|
|
||||||
If you need more control or functionality than the simple tag’s above, you can use the node based API where you can provide a block of code to deal with parsing. There are a few examples of this in use over at `Node.swift` inside Stencil. There is an implementation of `if` and `for` template tags.
|
|
||||||
|
|
||||||
You would register a template tag using the `registerTag` API inside a `Namespace` which accepts a name for the tag and a block of code to handle parsing. The block of code is invoked with the parser and the current token as an argument. This allows you to use the API on `TokenParser` to parse node’s further in the token array.
|
|
||||||
|
|
||||||
As an example, we’re going to create a template tag called `debug` which will optionally render nodes from `debug` up until `enddebug`. When rendering the `DebugNode`, it will only render the nodes inside if a variable called `debug` is set to `true` inside the template Context.
|
|
||||||
|
|
||||||
```html+django
|
|
||||||
{% debug %}
|
|
||||||
Debugging is enabled!
|
|
||||||
{% enddebug %}
|
|
||||||
```
|
|
||||||
|
|
||||||
This will be represented by a `DebugNode` which will have a property containing all of the Node’s inside the `debug`/`enddebug` block. In the above example, this will just be a TextNode containing ` Debugging is enabled!`.
|
|
||||||
|
|
||||||
When the `DebugNode` is rendered, it will determine if debug is enabled by introspecting the context and if it is enabled. We will render the nodes, otherwise just return an empty string to hide the debug output.
|
|
||||||
|
|
||||||
So, our `DebugNode` would look like as following:
|
|
||||||
|
|
||||||
```swift
|
|
||||||
class DebugNode : Node {
|
|
||||||
let nodes:[Node]
|
|
||||||
|
|
||||||
init(nodes:[Node]) {
|
|
||||||
self.nodes = nodes
|
|
||||||
}
|
|
||||||
|
|
||||||
func render(context: Context) throws -> String {
|
|
||||||
// Is there a debug variable inside the context?
|
|
||||||
if let debug = context["debug"] as? Bool {
|
|
||||||
// Is debug set to true?
|
|
||||||
if debug {
|
|
||||||
// Let's render the given nodes in the context since debug is enabled.
|
|
||||||
return renderNodes(nodes, context)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Debug is turned off, so let's not render anything
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
We will need to write a parser to parse up until the `enddebug` template block and create a `DebugNode` with the nodes in-between. If there was another error form another Node inside, then we will return that error.
|
|
||||||
|
|
||||||
```swift
|
|
||||||
namespace.registerTag("debug") { parser, token in
|
|
||||||
// Use the parser to parse every token up until the `enddebug` block.
|
|
||||||
let nodes = try until(["enddebug"]))
|
|
||||||
return DebugNode(nodes)
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
## Context
|
|
||||||
|
|
||||||
A Context is a structure containing any templates you would like to use in a template. It’s somewhat like a dictionary, however you can push and pop to scope variables. So that means that when iterating over a for loop, you can push a new scope into the context to store any variables local to the scope.
|
|
||||||
187
CHANGELOG.md
Normal file
187
CHANGELOG.md
Normal file
@@ -0,0 +1,187 @@
|
|||||||
|
# Stencil Changelog
|
||||||
|
|
||||||
|
## 0.9.0
|
||||||
|
|
||||||
|
### Enhancements
|
||||||
|
|
||||||
|
- `for` block now can contain `where` expression to filter array items. For example `{% for item in items where item > 1 %}` is now supported.
|
||||||
|
- `if` blocks may now contain else if (`elif`) conditions.
|
||||||
|
|
||||||
|
```html+django
|
||||||
|
{% if one or two and not three %}
|
||||||
|
one or two but not three
|
||||||
|
{% elif four %}
|
||||||
|
four
|
||||||
|
{% else %}
|
||||||
|
not one, two, or four
|
||||||
|
{% endif %}
|
||||||
|
```
|
||||||
|
|
||||||
|
- `for` block now allows you to iterate over array of tuples or dictionaries.
|
||||||
|
|
||||||
|
```html+django
|
||||||
|
{% for key, value in thing %}
|
||||||
|
<li>{{ key }}: {{ value }}</li>
|
||||||
|
{% endfor %}
|
||||||
|
```
|
||||||
|
|
||||||
|
### Bug Fixes
|
||||||
|
|
||||||
|
- You can now use literal filter arguments which contain quotes.
|
||||||
|
[#98](https://github.com/kylef/Stencil/pull/98)
|
||||||
|
|
||||||
|
|
||||||
|
## 0.8.0
|
||||||
|
|
||||||
|
### Breaking
|
||||||
|
|
||||||
|
- It is no longer possible to create `Context` objects. Instead, you can pass a
|
||||||
|
dictionary directly to a `Template`s `render` method.
|
||||||
|
|
||||||
|
```diff
|
||||||
|
- try template.render(Context(dictionary: ["name": "Kyle"]))
|
||||||
|
+ try template.render(["name": "Kyle"])
|
||||||
|
```
|
||||||
|
|
||||||
|
- Template loader are no longer passed into a `Context`, instead you will need
|
||||||
|
to pass the `Loader` to an `Environment` and create a template from the
|
||||||
|
`Environment`.
|
||||||
|
|
||||||
|
```diff
|
||||||
|
let loader = FileSystemLoader(paths: ["templates/"])
|
||||||
|
|
||||||
|
- let template = loader.loadTemplate(name: "index.html")
|
||||||
|
- try template.render(Context(dictionary: ["loader": loader]))
|
||||||
|
+ let environment = Environment(loader: loader)
|
||||||
|
+ try environment.renderTemplate(name: "index.html")
|
||||||
|
```
|
||||||
|
|
||||||
|
- `Loader`s will now throw a `TemplateDoesNotExist` error when a template
|
||||||
|
is not found.
|
||||||
|
|
||||||
|
- `Namespace` has been removed and replaced by extensions. You can create an
|
||||||
|
extension including any custom template tags and filters. A collection of
|
||||||
|
extensions can be passed to an `Environment`.
|
||||||
|
|
||||||
|
### Enhancements
|
||||||
|
|
||||||
|
- `Environment` is a new way to load templates. You can configure an
|
||||||
|
environment with custom template filters, tags and loaders and then create a
|
||||||
|
template from an environment.
|
||||||
|
|
||||||
|
Environment also provides a convenience method to render a template directly.
|
||||||
|
|
||||||
|
- `FileSystemLoader` will now ensure that template paths are within the base
|
||||||
|
path. Any template names that try to escape the base path will raise a
|
||||||
|
`SuspiciousFileOperation` error.
|
||||||
|
|
||||||
|
- New `{% filter %}` tag allowing you to perform a filter across the contents
|
||||||
|
of a block.
|
||||||
|
|
||||||
|
```html+django
|
||||||
|
{% filter lowercase %}
|
||||||
|
This Text Will Be Lowercased.
|
||||||
|
{% endfilter %}
|
||||||
|
```
|
||||||
|
|
||||||
|
- You can now use `{{ block.super }}` to render a super block from another `{%
|
||||||
|
block %}`.
|
||||||
|
|
||||||
|
- `Environment` allows you to provide a custom `Template` subclass, allowing
|
||||||
|
new template to use a specific subclass.
|
||||||
|
|
||||||
|
- If expressions may now contain filters on variables. For example
|
||||||
|
`{% if name|uppercase == "TEST" %}` is now supported.
|
||||||
|
|
||||||
|
### Deprecations
|
||||||
|
|
||||||
|
- `Template` initialisers have been deprecated in favour of using a template
|
||||||
|
loader such as `FileSystemLoader` inside an `Environment`.
|
||||||
|
|
||||||
|
- The use of whitespace inside variable filter expression is now deprecated.
|
||||||
|
|
||||||
|
```diff
|
||||||
|
- {{ name | uppercase }}
|
||||||
|
+ {{ name|uppercase }}
|
||||||
|
```
|
||||||
|
|
||||||
|
### Bug Fixes
|
||||||
|
|
||||||
|
- Restores compatibility with ARM based platforms such as iOS. Stencil 0.7
|
||||||
|
introduced compilation errors due to using the `Float80` type which is not
|
||||||
|
available.
|
||||||
|
|
||||||
|
|
||||||
|
## 0.7.1
|
||||||
|
|
||||||
|
### Bug Fixes
|
||||||
|
|
||||||
|
- Fixes an issue where using `{% if %}` statements which use operators would
|
||||||
|
throw a syntax error.
|
||||||
|
|
||||||
|
|
||||||
|
## 0.7.0
|
||||||
|
|
||||||
|
### Breaking
|
||||||
|
|
||||||
|
- `TemplateLoader` has been renamed to `FileSystemLoader`. The
|
||||||
|
`loadTemplate(s)` methods are now throwing and now take labels for the `name`
|
||||||
|
and `names` arguments.
|
||||||
|
|
||||||
|
- Many internal classes are no longer public. Some APIs were previously
|
||||||
|
accessible due to earlier versions of Swift requiring the types to be public
|
||||||
|
to be able to test. Now we have access to `@testable` these can correctly be
|
||||||
|
private.
|
||||||
|
|
||||||
|
- `{% ifnot %}` tag is now deprecated, please use `{% if not %}` instead.
|
||||||
|
|
||||||
|
### Enhancements
|
||||||
|
|
||||||
|
- Variable lookup now supports introspection of Swift types. You can now lookup
|
||||||
|
values of Swift structures and classes inside a Context.
|
||||||
|
|
||||||
|
- If tags can now use prefix and infix operators such as `not`, `and`, `or`,
|
||||||
|
`==`, `!=`, `>`, `>=`, `<` and `<=`.
|
||||||
|
|
||||||
|
```html+django
|
||||||
|
{% if one or two and not three %}
|
||||||
|
```
|
||||||
|
|
||||||
|
- You may now register custom template filters which make use of arguments.
|
||||||
|
- There is now a `default` filter.
|
||||||
|
|
||||||
|
```html+django
|
||||||
|
Hello {{ name|default:"World" }}
|
||||||
|
```
|
||||||
|
|
||||||
|
- There is now a `join` filter.
|
||||||
|
|
||||||
|
```html+django
|
||||||
|
{{ value|join:", " }}
|
||||||
|
```
|
||||||
|
|
||||||
|
- `{% for %}` tag now supports filters.
|
||||||
|
|
||||||
|
```html+django
|
||||||
|
{% for user in non_admins|default:admins %}
|
||||||
|
{{ user }}
|
||||||
|
{% endfor %}
|
||||||
|
```
|
||||||
|
|
||||||
|
### Bug Fixes
|
||||||
|
|
||||||
|
- Variables (`{{ variable.5 }}`) that reference an array index at an unknown
|
||||||
|
index will now resolve to `nil` instead of causing a crash.
|
||||||
|
[#72](https://github.com/kylef/Stencil/issues/72)
|
||||||
|
|
||||||
|
- Templates can now extend templates that extend other templates.
|
||||||
|
[#60](https://github.com/kylef/Stencil/issues/60)
|
||||||
|
|
||||||
|
- If comparisons will now treat 0 and below numbers as negative.
|
||||||
|
|
||||||
|
|
||||||
|
## 0.6.0
|
||||||
|
|
||||||
|
### Enhancements
|
||||||
|
|
||||||
|
- Adds support for Swift 3.0.
|
||||||
7
Makefile
7
Makefile
@@ -1,7 +0,0 @@
|
|||||||
stencil:
|
|
||||||
@echo "Building Stencil"
|
|
||||||
@swift build
|
|
||||||
|
|
||||||
test: stencil
|
|
||||||
@echo "Running Tests"
|
|
||||||
@.build/debug/spectre-build
|
|
||||||
@@ -3,9 +3,9 @@ import PackageDescription
|
|||||||
let package = Package(
|
let package = Package(
|
||||||
name: "Stencil",
|
name: "Stencil",
|
||||||
dependencies: [
|
dependencies: [
|
||||||
.Package(url: "https://github.com/kylef/PathKit.git", majorVersion: 0, minor: 6),
|
.Package(url: "https://github.com/kylef/PathKit.git", majorVersion: 0, minor: 8),
|
||||||
],
|
|
||||||
testDependencies: [
|
// https://github.com/apple/swift-package-manager/pull/597
|
||||||
.Package(url: "https://github.com/kylef/spectre-build", majorVersion: 0),
|
.Package(url: "https://github.com/kylef/Spectre.git", majorVersion: 0, minor: 7),
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
|
|||||||
228
README.md
228
README.md
@@ -11,34 +11,32 @@ feel right at home with Stencil.
|
|||||||
```html+django
|
```html+django
|
||||||
There are {{ articles.count }} articles.
|
There are {{ articles.count }} articles.
|
||||||
|
|
||||||
{% for article in articles %}
|
<ul>
|
||||||
- {{ article.title }} by {{ article.author }}.
|
{% for article in articles %}
|
||||||
{% endfor %}
|
<li>{{ article.title }} by {{ article.author }}</li>
|
||||||
|
{% endfor %}
|
||||||
|
</ul>
|
||||||
```
|
```
|
||||||
|
|
||||||
```swift
|
```swift
|
||||||
let context = Context(dictionary: [
|
import Stencil
|
||||||
"articles": [
|
|
||||||
[ "title": "Migrating from OCUnit to XCTest", "author": "Kyle Fuller" ],
|
|
||||||
[ "title": "Memory Management with ARC", "author": "Kyle Fuller" ],
|
|
||||||
]
|
|
||||||
])
|
|
||||||
|
|
||||||
do {
|
struct Article {
|
||||||
let template = try Template(named: "template.stencil")
|
let title: String
|
||||||
let rendered = try template.render(context)
|
let author: String
|
||||||
print(rendered)
|
|
||||||
} catch {
|
|
||||||
print("Failed to render template \(error)")
|
|
||||||
}
|
}
|
||||||
```
|
|
||||||
|
|
||||||
## Installation
|
let context = [
|
||||||
|
"articles": [
|
||||||
|
Article(title: "Migrating from OCUnit to XCTest", author: "Kyle Fuller"),
|
||||||
|
Article(title: "Memory Management with ARC", author: "Kyle Fuller"),
|
||||||
|
]
|
||||||
|
]
|
||||||
|
|
||||||
Installation with CocoaPods is recommended.
|
let environment = Environment(loader: FileSystemLoader(paths: ["templates/"]))
|
||||||
|
let rendered = try environment.renderTemplate(name: context)
|
||||||
|
|
||||||
```ruby
|
print(rendered)
|
||||||
pod 'Stencil'
|
|
||||||
```
|
```
|
||||||
|
|
||||||
## Philosophy
|
## Philosophy
|
||||||
@@ -51,191 +49,19 @@ Stencil follows the same philosophy of Django:
|
|||||||
> design: the template system is meant to express presentation, not program
|
> design: the template system is meant to express presentation, not program
|
||||||
> logic.
|
> logic.
|
||||||
|
|
||||||
## Templates
|
## The User Guide
|
||||||
|
|
||||||
### Variables
|
Resources for Stencil template authors to write Stencil templates:
|
||||||
|
|
||||||
A variable can be defined in your template using the following:
|
- [Language overview](http://stencil.fuller.li/en/latest/templates.html)
|
||||||
|
- [Built-in template tags and filters](http://stencil.fuller.li/en/latest/builtins.html)
|
||||||
|
|
||||||
```html+django
|
Resources to help you integrate Stencil into a Swift project:
|
||||||
{{ variable }}
|
|
||||||
```
|
|
||||||
|
|
||||||
Stencil will look up the variable inside the current variable context and
|
- [Installation](http://stencil.fuller.li/en/latest/installation.html)
|
||||||
evaluate it. When a variable contains a dot, it will try doing the
|
- [Getting Started](http://stencil.fuller.li/en/latest/getting-started.html)
|
||||||
following lookup:
|
- [API Reference](http://stencil.fuller.li/en/latest/api.html)
|
||||||
|
- [Custom Template Tags and Filters](http://stencil.fuller.li/en/latest/custom-template-tags-and-filters.html)
|
||||||
- Context lookup
|
|
||||||
- Dictionary lookup
|
|
||||||
- Array lookup (first, last, count, index)
|
|
||||||
- Key value coding lookup
|
|
||||||
|
|
||||||
For example, if `people` was an array:
|
|
||||||
|
|
||||||
```html+django
|
|
||||||
There are {{ people.count }} people. {{ people.first }} is the first person,
|
|
||||||
followed by {{ people.1 }}.
|
|
||||||
```
|
|
||||||
|
|
||||||
#### Filters
|
|
||||||
|
|
||||||
Filters allow you to transform the values of variables. For example, they look like:
|
|
||||||
|
|
||||||
```html+django
|
|
||||||
{{ variable|uppercase }}
|
|
||||||
```
|
|
||||||
|
|
||||||
##### Capitalize
|
|
||||||
|
|
||||||
The capitalize filter allows you to capitalize a string.
|
|
||||||
For example, `stencil` to `Stencil`.
|
|
||||||
|
|
||||||
```html+django
|
|
||||||
{{ "stencil"|capitalize }}
|
|
||||||
```
|
|
||||||
|
|
||||||
##### Uppercase
|
|
||||||
|
|
||||||
The uppercase filter allows you to transform a string to uppercase.
|
|
||||||
For example, `Stencil` to `STENCIL`.
|
|
||||||
|
|
||||||
```html+django
|
|
||||||
{{ "Stencil"|uppercase }}
|
|
||||||
```
|
|
||||||
|
|
||||||
##### Lowercase
|
|
||||||
|
|
||||||
The uppercase filter allows you to transform a string to lowercase.
|
|
||||||
For example, `Stencil` to `stencil`.
|
|
||||||
|
|
||||||
```html+django
|
|
||||||
{{ "Stencil"|lowercase }}
|
|
||||||
```
|
|
||||||
|
|
||||||
### Tags
|
|
||||||
|
|
||||||
Tags are a mechanism to execute a piece of code, allowing you to have
|
|
||||||
control flow within your template.
|
|
||||||
|
|
||||||
```html+django
|
|
||||||
{% if variable %}
|
|
||||||
{{ variable }} was found.
|
|
||||||
{% endif %}
|
|
||||||
```
|
|
||||||
|
|
||||||
A tag can also affect the context and define variables as follows:
|
|
||||||
|
|
||||||
```html+django
|
|
||||||
{% for item in items %}
|
|
||||||
{{ item }}
|
|
||||||
{% endfor %}
|
|
||||||
```
|
|
||||||
|
|
||||||
Stencil has a couple of built-in tags which are listed below. You can also
|
|
||||||
extend Stencil by providing your own tags.
|
|
||||||
|
|
||||||
#### for
|
|
||||||
|
|
||||||
A for loop allows you to iterate over an array found by variable lookup.
|
|
||||||
|
|
||||||
```html+django
|
|
||||||
{% for item in items %}
|
|
||||||
{{ item }}
|
|
||||||
{% empty %}
|
|
||||||
There were no items.
|
|
||||||
{% endfor %}
|
|
||||||
```
|
|
||||||
|
|
||||||
#### if
|
|
||||||
|
|
||||||
```html+django
|
|
||||||
{% if variable %}
|
|
||||||
The variable was found in the current context.
|
|
||||||
{% else %}
|
|
||||||
The variable was not found.
|
|
||||||
{% endif %}
|
|
||||||
```
|
|
||||||
|
|
||||||
#### ifnot
|
|
||||||
|
|
||||||
```html+django
|
|
||||||
{% ifnot variable %}
|
|
||||||
The variable was NOT found in the current context.
|
|
||||||
{% else %}
|
|
||||||
The variable was found.
|
|
||||||
{% endif %}
|
|
||||||
```
|
|
||||||
|
|
||||||
#### include
|
|
||||||
|
|
||||||
You can include another template using the `include` tag.
|
|
||||||
|
|
||||||
```html+django
|
|
||||||
{% include "comment.html" %}
|
|
||||||
```
|
|
||||||
|
|
||||||
The `include` tag requires a TemplateLoader to be found inside your context with the paths, or bundles used to lookup the template.
|
|
||||||
|
|
||||||
```swift
|
|
||||||
let context = Context(dictionary: [
|
|
||||||
"loader": TemplateLoader(bundle:[NSBundle.mainBundle()])
|
|
||||||
])
|
|
||||||
```
|
|
||||||
|
|
||||||
### Customisation
|
|
||||||
|
|
||||||
You can build your own custom filters and tags and pass them down while
|
|
||||||
rendering your template. Any custom filters or tags must be registered
|
|
||||||
with a namespace which contains all filters and tags available to the template.
|
|
||||||
|
|
||||||
```swift
|
|
||||||
let namespace = Namespace()
|
|
||||||
// Register your filters and tags with the namespace
|
|
||||||
let rendered = try template.render(context, namespace: namespace)
|
|
||||||
```
|
|
||||||
|
|
||||||
#### Registering custom filters
|
|
||||||
|
|
||||||
```swift
|
|
||||||
namespace.registerFilter("double") { value in
|
|
||||||
if let value = value as? Int {
|
|
||||||
return value * 2
|
|
||||||
}
|
|
||||||
|
|
||||||
return value
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
#### Building custom tags
|
|
||||||
|
|
||||||
You can build a custom template tag. There are a couple of APIs to allow
|
|
||||||
you to write your own custom tags. The following is the simplest form:
|
|
||||||
|
|
||||||
```swift
|
|
||||||
namespace.registerSimpleTag("custom") { context in
|
|
||||||
return "Hello World"
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
When your tag is used via `{% custom %}` it will execute the registered block
|
|
||||||
of code allowing you to modify or retrieve a value from the context. Then
|
|
||||||
return either a string rendered in your template, or throw an error.
|
|
||||||
|
|
||||||
If you want to accept arguments or to capture different tokens between two sets
|
|
||||||
of template tags. You will need to call the `registerTag` API which accepts a
|
|
||||||
closure to handle the parsing. You can find examples of the `now`, `if` and
|
|
||||||
`for` tags found inside `Node.swift`.
|
|
||||||
|
|
||||||
The architecture of Stencil along with how to build advanced plugins can be
|
|
||||||
found in the [architecture](ARCHITECTURE.md) document.
|
|
||||||
|
|
||||||
### Comments
|
|
||||||
|
|
||||||
To comment out part of your template, you can use the following syntax:
|
|
||||||
|
|
||||||
```html+django
|
|
||||||
{# My comment is completely hidden #}
|
|
||||||
```
|
|
||||||
|
|
||||||
## License
|
## License
|
||||||
|
|
||||||
|
|||||||
@@ -1,23 +1,23 @@
|
|||||||
/// A container for template variables.
|
/// A container for template variables.
|
||||||
public class Context {
|
public class Context {
|
||||||
var dictionaries: [[String: Any]]
|
var dictionaries: [[String: Any?]]
|
||||||
let namespace: Namespace
|
|
||||||
|
|
||||||
/// Initialise a Context with an optional dictionary and optional namespace
|
public let environment: Environment
|
||||||
public init(dictionary: [String: Any]? = nil, namespace: Namespace = Namespace()) {
|
|
||||||
|
init(dictionary: [String: Any]? = nil, environment: Environment? = nil) {
|
||||||
if let dictionary = dictionary {
|
if let dictionary = dictionary {
|
||||||
dictionaries = [dictionary]
|
dictionaries = [dictionary]
|
||||||
} else {
|
} else {
|
||||||
dictionaries = []
|
dictionaries = []
|
||||||
}
|
}
|
||||||
|
|
||||||
self.namespace = namespace
|
self.environment = environment ?? Environment()
|
||||||
}
|
}
|
||||||
|
|
||||||
public subscript(key: String) -> Any? {
|
public subscript(key: String) -> Any? {
|
||||||
/// Retrieves a variable's value, starting at the current context and going upwards
|
/// Retrieves a variable's value, starting at the current context and going upwards
|
||||||
get {
|
get {
|
||||||
for dictionary in Array(dictionaries.reverse()) {
|
for dictionary in Array(dictionaries.reversed()) {
|
||||||
if let value = dictionary[key] {
|
if let value = dictionary[key] {
|
||||||
return value
|
return value
|
||||||
}
|
}
|
||||||
@@ -37,19 +37,33 @@ public class Context {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Push a new level into the Context
|
/// Push a new level into the Context
|
||||||
private func push(dictionary: [String: Any]? = nil) {
|
fileprivate func push(_ dictionary: [String: Any]? = nil) {
|
||||||
dictionaries.append(dictionary ?? [:])
|
dictionaries.append(dictionary ?? [:])
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Pop the last level off of the Context
|
/// Pop the last level off of the Context
|
||||||
private func pop() -> [String: Any]? {
|
fileprivate func pop() -> [String: Any?]? {
|
||||||
return dictionaries.popLast()
|
return dictionaries.popLast()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Push a new level onto the context for the duration of the execution of the given closure
|
/// Push a new level onto the context for the duration of the execution of the given closure
|
||||||
public func push<Result>(dictionary: [String: Any]? = nil, @noescape closure: (() throws -> Result)) rethrows -> Result {
|
public func push<Result>(dictionary: [String: Any]? = nil, closure: (() throws -> Result)) rethrows -> Result {
|
||||||
push(dictionary)
|
push(dictionary)
|
||||||
defer { pop() }
|
defer { _ = pop() }
|
||||||
return try closure()
|
return try closure()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public func flatten() -> [String: Any] {
|
||||||
|
var accumulator: [String: Any] = [:]
|
||||||
|
|
||||||
|
for dictionary in dictionaries {
|
||||||
|
for (key, value) in dictionary {
|
||||||
|
if let value = value {
|
||||||
|
accumulator.updateValue(value, forKey: key)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return accumulator
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
38
Sources/Environment.swift
Normal file
38
Sources/Environment.swift
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
public struct Environment {
|
||||||
|
public let templateClass: Template.Type
|
||||||
|
public let extensions: [Extension]
|
||||||
|
|
||||||
|
public var loader: Loader?
|
||||||
|
|
||||||
|
public init(loader: Loader? = nil, extensions: [Extension]? = nil, templateClass: Template.Type = Template.self) {
|
||||||
|
self.templateClass = templateClass
|
||||||
|
self.loader = loader
|
||||||
|
self.extensions = (extensions ?? []) + [DefaultExtension()]
|
||||||
|
}
|
||||||
|
|
||||||
|
public func loadTemplate(name: String) throws -> Template {
|
||||||
|
if let loader = loader {
|
||||||
|
return try loader.loadTemplate(name: name, environment: self)
|
||||||
|
} else {
|
||||||
|
throw TemplateDoesNotExist(templateNames: [name], loader: nil)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public func loadTemplate(names: [String]) throws -> Template {
|
||||||
|
if let loader = loader {
|
||||||
|
return try loader.loadTemplate(names: names, environment: self)
|
||||||
|
} else {
|
||||||
|
throw TemplateDoesNotExist(templateNames: names, loader: nil)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public func renderTemplate(name: String, context: [String: Any]? = nil) throws -> String {
|
||||||
|
let template = try loadTemplate(name: name)
|
||||||
|
return try template.render(context)
|
||||||
|
}
|
||||||
|
|
||||||
|
public func renderTemplate(string: String, context: [String: Any]? = nil) throws -> String {
|
||||||
|
let template = templateClass.init(templateString: string, environment: self)
|
||||||
|
return try template.render(context)
|
||||||
|
}
|
||||||
|
}
|
||||||
19
Sources/Errors.swift
Normal file
19
Sources/Errors.swift
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
public class TemplateDoesNotExist: Error, CustomStringConvertible {
|
||||||
|
let templateNames: [String]
|
||||||
|
let loader: Loader?
|
||||||
|
|
||||||
|
public init(templateNames: [String], loader: Loader? = nil) {
|
||||||
|
self.templateNames = templateNames
|
||||||
|
self.loader = loader
|
||||||
|
}
|
||||||
|
|
||||||
|
public var description: String {
|
||||||
|
let templates = templateNames.joined(separator: ", ")
|
||||||
|
|
||||||
|
if let loader = loader {
|
||||||
|
return "Template named `\(templates)` does not exist in loader \(loader)"
|
||||||
|
}
|
||||||
|
|
||||||
|
return "Template named `\(templates)` does not exist. No loaders found"
|
||||||
|
}
|
||||||
|
}
|
||||||
300
Sources/Expression.swift
Normal file
300
Sources/Expression.swift
Normal file
@@ -0,0 +1,300 @@
|
|||||||
|
protocol Expression: CustomStringConvertible {
|
||||||
|
func evaluate(context: Context) throws -> Bool
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
protocol InfixOperator: Expression {
|
||||||
|
init(lhs: Expression, rhs: Expression)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
protocol PrefixOperator: Expression {
|
||||||
|
init(expression: Expression)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
final class StaticExpression: Expression, CustomStringConvertible {
|
||||||
|
let value: Bool
|
||||||
|
|
||||||
|
init(value: Bool) {
|
||||||
|
self.value = value
|
||||||
|
}
|
||||||
|
|
||||||
|
func evaluate(context: Context) throws -> Bool {
|
||||||
|
return value
|
||||||
|
}
|
||||||
|
|
||||||
|
var description: String {
|
||||||
|
return "\(value)"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
final class VariableExpression: Expression, CustomStringConvertible {
|
||||||
|
let variable: Resolvable
|
||||||
|
|
||||||
|
init(variable: Resolvable) {
|
||||||
|
self.variable = variable
|
||||||
|
}
|
||||||
|
|
||||||
|
var description: String {
|
||||||
|
return "(variable: \(variable))"
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Resolves a variable in the given context as boolean
|
||||||
|
func resolve(context: Context, variable: Resolvable) throws -> Bool {
|
||||||
|
let result = try variable.resolve(context)
|
||||||
|
var truthy = false
|
||||||
|
|
||||||
|
if let result = result as? [Any] {
|
||||||
|
truthy = !result.isEmpty
|
||||||
|
} else if let result = result as? [String:Any] {
|
||||||
|
truthy = !result.isEmpty
|
||||||
|
} else if let result = result as? Bool {
|
||||||
|
truthy = result
|
||||||
|
} else if let result = result as? String {
|
||||||
|
truthy = !result.isEmpty
|
||||||
|
} else if let value = result, let result = toNumber(value: value) {
|
||||||
|
truthy = result > 0
|
||||||
|
} else if result != nil {
|
||||||
|
truthy = true
|
||||||
|
}
|
||||||
|
|
||||||
|
return truthy
|
||||||
|
}
|
||||||
|
|
||||||
|
func evaluate(context: Context) throws -> Bool {
|
||||||
|
return try resolve(context: context, variable: variable)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
final class NotExpression: Expression, PrefixOperator, CustomStringConvertible {
|
||||||
|
let expression: Expression
|
||||||
|
|
||||||
|
init(expression: Expression) {
|
||||||
|
self.expression = expression
|
||||||
|
}
|
||||||
|
|
||||||
|
var description: String {
|
||||||
|
return "not \(expression)"
|
||||||
|
}
|
||||||
|
|
||||||
|
func evaluate(context: Context) throws -> Bool {
|
||||||
|
return try !expression.evaluate(context: context)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
final class OrExpression: Expression, InfixOperator, CustomStringConvertible {
|
||||||
|
let lhs: Expression
|
||||||
|
let rhs: Expression
|
||||||
|
|
||||||
|
init(lhs: Expression, rhs: Expression) {
|
||||||
|
self.lhs = lhs
|
||||||
|
self.rhs = rhs
|
||||||
|
}
|
||||||
|
|
||||||
|
var description: String {
|
||||||
|
return "(\(lhs) or \(rhs))"
|
||||||
|
}
|
||||||
|
|
||||||
|
func evaluate(context: Context) throws -> Bool {
|
||||||
|
let lhs = try self.lhs.evaluate(context: context)
|
||||||
|
if lhs {
|
||||||
|
return lhs
|
||||||
|
}
|
||||||
|
|
||||||
|
return try rhs.evaluate(context: context)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
final class AndExpression: Expression, InfixOperator, CustomStringConvertible {
|
||||||
|
let lhs: Expression
|
||||||
|
let rhs: Expression
|
||||||
|
|
||||||
|
init(lhs: Expression, rhs: Expression) {
|
||||||
|
self.lhs = lhs
|
||||||
|
self.rhs = rhs
|
||||||
|
}
|
||||||
|
|
||||||
|
var description: String {
|
||||||
|
return "(\(lhs) and \(rhs))"
|
||||||
|
}
|
||||||
|
|
||||||
|
func evaluate(context: Context) throws -> Bool {
|
||||||
|
let lhs = try self.lhs.evaluate(context: context)
|
||||||
|
if !lhs {
|
||||||
|
return lhs
|
||||||
|
}
|
||||||
|
|
||||||
|
return try rhs.evaluate(context: context)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
class EqualityExpression: Expression, InfixOperator, CustomStringConvertible {
|
||||||
|
let lhs: Expression
|
||||||
|
let rhs: Expression
|
||||||
|
|
||||||
|
required init(lhs: Expression, rhs: Expression) {
|
||||||
|
self.lhs = lhs
|
||||||
|
self.rhs = rhs
|
||||||
|
}
|
||||||
|
|
||||||
|
var description: String {
|
||||||
|
return "(\(lhs) == \(rhs))"
|
||||||
|
}
|
||||||
|
|
||||||
|
func evaluate(context: Context) throws -> Bool {
|
||||||
|
if let lhs = lhs as? VariableExpression, let rhs = rhs as? VariableExpression {
|
||||||
|
let lhsValue = try lhs.variable.resolve(context)
|
||||||
|
let rhsValue = try rhs.variable.resolve(context)
|
||||||
|
|
||||||
|
if let lhs = lhsValue, let rhs = rhsValue {
|
||||||
|
if let lhs = toNumber(value: lhs), let rhs = toNumber(value: rhs) {
|
||||||
|
return lhs == rhs
|
||||||
|
} else if let lhs = lhsValue as? String, let rhs = rhsValue as? String {
|
||||||
|
return lhs == rhs
|
||||||
|
} else if let lhs = lhsValue as? Bool, let rhs = rhsValue as? Bool {
|
||||||
|
return lhs == rhs
|
||||||
|
}
|
||||||
|
} else if lhsValue == nil && rhsValue == nil {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
class NumericExpression: Expression, InfixOperator, CustomStringConvertible {
|
||||||
|
let lhs: Expression
|
||||||
|
let rhs: Expression
|
||||||
|
|
||||||
|
required init(lhs: Expression, rhs: Expression) {
|
||||||
|
self.lhs = lhs
|
||||||
|
self.rhs = rhs
|
||||||
|
}
|
||||||
|
|
||||||
|
var description: String {
|
||||||
|
return "(\(lhs) \(op) \(rhs))"
|
||||||
|
}
|
||||||
|
|
||||||
|
func evaluate(context: Context) throws -> Bool {
|
||||||
|
if let lhs = lhs as? VariableExpression, let rhs = rhs as? VariableExpression {
|
||||||
|
let lhsValue = try lhs.variable.resolve(context)
|
||||||
|
let rhsValue = try rhs.variable.resolve(context)
|
||||||
|
|
||||||
|
if let lhs = lhsValue, let rhs = rhsValue {
|
||||||
|
if let lhs = toNumber(value: lhs), let rhs = toNumber(value: rhs) {
|
||||||
|
return compare(lhs: lhs, rhs: rhs)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
var op: String {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
func compare(lhs: Number, rhs: Number) -> Bool {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
class MoreThanExpression: NumericExpression {
|
||||||
|
override var op: String {
|
||||||
|
return ">"
|
||||||
|
}
|
||||||
|
|
||||||
|
override func compare(lhs: Number, rhs: Number) -> Bool {
|
||||||
|
return lhs > rhs
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
class MoreThanEqualExpression: NumericExpression {
|
||||||
|
override var op: String {
|
||||||
|
return ">="
|
||||||
|
}
|
||||||
|
|
||||||
|
override func compare(lhs: Number, rhs: Number) -> Bool {
|
||||||
|
return lhs >= rhs
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
class LessThanExpression: NumericExpression {
|
||||||
|
override var op: String {
|
||||||
|
return "<"
|
||||||
|
}
|
||||||
|
|
||||||
|
override func compare(lhs: Number, rhs: Number) -> Bool {
|
||||||
|
return lhs < rhs
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
class LessThanEqualExpression: NumericExpression {
|
||||||
|
override var op: String {
|
||||||
|
return "<="
|
||||||
|
}
|
||||||
|
|
||||||
|
override func compare(lhs: Number, rhs: Number) -> Bool {
|
||||||
|
return lhs <= rhs
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
class InequalityExpression: EqualityExpression {
|
||||||
|
override var description: String {
|
||||||
|
return "(\(lhs) != \(rhs))"
|
||||||
|
}
|
||||||
|
|
||||||
|
override func evaluate(context: Context) throws -> Bool {
|
||||||
|
return try !super.evaluate(context: context)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
func toNumber(value: Any) -> Number? {
|
||||||
|
if let value = value as? Float {
|
||||||
|
return Number(value)
|
||||||
|
} else if let value = value as? Double {
|
||||||
|
return Number(value)
|
||||||
|
} else if let value = value as? UInt {
|
||||||
|
return Number(value)
|
||||||
|
} else if let value = value as? Int {
|
||||||
|
return Number(value)
|
||||||
|
} else if let value = value as? Int8 {
|
||||||
|
return Number(value)
|
||||||
|
} else if let value = value as? Int16 {
|
||||||
|
return Number(value)
|
||||||
|
} else if let value = value as? Int32 {
|
||||||
|
return Number(value)
|
||||||
|
} else if let value = value as? Int64 {
|
||||||
|
return Number(value)
|
||||||
|
} else if let value = value as? UInt8 {
|
||||||
|
return Number(value)
|
||||||
|
} else if let value = value as? UInt16 {
|
||||||
|
return Number(value)
|
||||||
|
} else if let value = value as? UInt32 {
|
||||||
|
return Number(value)
|
||||||
|
} else if let value = value as? UInt64 {
|
||||||
|
return Number(value)
|
||||||
|
} else if let value = value as? Number {
|
||||||
|
return value
|
||||||
|
} else if let value = value as? Float64 {
|
||||||
|
return Number(value)
|
||||||
|
} else if let value = value as? Float32 {
|
||||||
|
return Number(value)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
84
Sources/Extension.swift
Normal file
84
Sources/Extension.swift
Normal file
@@ -0,0 +1,84 @@
|
|||||||
|
open class Extension {
|
||||||
|
typealias TagParser = (TokenParser, Token) throws -> NodeType
|
||||||
|
var tags = [String: TagParser]()
|
||||||
|
|
||||||
|
var filters = [String: Filter]()
|
||||||
|
|
||||||
|
public init() {
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Registers a new template tag
|
||||||
|
public func registerTag(_ name: String, parser: @escaping (TokenParser, Token) throws -> NodeType) {
|
||||||
|
tags[name] = parser
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Registers a simple template tag with a name and a handler
|
||||||
|
public func registerSimpleTag(_ name: String, handler: @escaping (Context) throws -> String) {
|
||||||
|
registerTag(name, parser: { parser, token in
|
||||||
|
return SimpleNode(handler: handler)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Registers a template filter with the given name
|
||||||
|
public func registerFilter(_ name: String, filter: @escaping (Any?) throws -> Any?) {
|
||||||
|
filters[name] = .simple(filter)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Registers a template filter with the given name
|
||||||
|
public func registerFilter(_ name: String, filter: @escaping (Any?, [Any?]) throws -> Any?) {
|
||||||
|
filters[name] = .arguments(filter)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
class DefaultExtension: Extension {
|
||||||
|
override init() {
|
||||||
|
super.init()
|
||||||
|
registerDefaultTags()
|
||||||
|
registerDefaultFilters()
|
||||||
|
}
|
||||||
|
|
||||||
|
fileprivate func registerDefaultTags() {
|
||||||
|
registerTag("for", parser: ForNode.parse)
|
||||||
|
registerTag("if", parser: IfNode.parse)
|
||||||
|
registerTag("ifnot", parser: IfNode.parse_ifnot)
|
||||||
|
#if !os(Linux)
|
||||||
|
registerTag("now", parser: NowNode.parse)
|
||||||
|
#endif
|
||||||
|
registerTag("include", parser: IncludeNode.parse)
|
||||||
|
registerTag("extends", parser: ExtendsNode.parse)
|
||||||
|
registerTag("block", parser: BlockNode.parse)
|
||||||
|
registerTag("filter", parser: FilterNode.parse)
|
||||||
|
}
|
||||||
|
|
||||||
|
fileprivate func registerDefaultFilters() {
|
||||||
|
registerFilter("default", filter: defaultFilter)
|
||||||
|
registerFilter("capitalize", filter: capitalise)
|
||||||
|
registerFilter("uppercase", filter: uppercase)
|
||||||
|
registerFilter("lowercase", filter: lowercase)
|
||||||
|
registerFilter("join", filter: joinFilter)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
protocol FilterType {
|
||||||
|
func invoke(value: Any?, arguments: [Any?]) throws -> Any?
|
||||||
|
}
|
||||||
|
|
||||||
|
enum Filter: FilterType {
|
||||||
|
case simple(((Any?) throws -> Any?))
|
||||||
|
case arguments(((Any?, [Any?]) throws -> Any?))
|
||||||
|
|
||||||
|
func invoke(value: Any?, arguments: [Any?]) throws -> Any? {
|
||||||
|
switch self {
|
||||||
|
case let .simple(filter):
|
||||||
|
if !arguments.isEmpty {
|
||||||
|
throw TemplateSyntaxError("cannot invoke filter with an argument")
|
||||||
|
}
|
||||||
|
|
||||||
|
return try filter(value)
|
||||||
|
case let .arguments(filter):
|
||||||
|
return try filter(value, arguments)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
35
Sources/FilterTag.swift
Normal file
35
Sources/FilterTag.swift
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
class FilterNode : NodeType {
|
||||||
|
let resolvable: Resolvable
|
||||||
|
let nodes: [NodeType]
|
||||||
|
|
||||||
|
class func parse(_ parser: TokenParser, token: Token) throws -> NodeType {
|
||||||
|
let bits = token.components()
|
||||||
|
|
||||||
|
guard bits.count == 2 else {
|
||||||
|
throw TemplateSyntaxError("'filter' tag takes one argument, the filter expression")
|
||||||
|
}
|
||||||
|
|
||||||
|
let blocks = try parser.parse(until(["endfilter"]))
|
||||||
|
|
||||||
|
guard parser.nextToken() != nil else {
|
||||||
|
throw TemplateSyntaxError("`endfilter` was not found.")
|
||||||
|
}
|
||||||
|
|
||||||
|
let resolvable = try parser.compileFilter("filter_value|\(bits[1])")
|
||||||
|
return FilterNode(nodes: blocks, resolvable: resolvable)
|
||||||
|
}
|
||||||
|
|
||||||
|
init(nodes: [NodeType], resolvable: Resolvable) {
|
||||||
|
self.nodes = nodes
|
||||||
|
self.resolvable = resolvable
|
||||||
|
}
|
||||||
|
|
||||||
|
func render(_ context: Context) throws -> String {
|
||||||
|
let value = try renderNodes(nodes, context)
|
||||||
|
|
||||||
|
return try context.push(dictionary: ["filter_value": value]) {
|
||||||
|
return try VariableNode(variable: resolvable).render(context)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@@ -1,32 +1,40 @@
|
|||||||
func toString(value: Any?) -> String? {
|
func capitalise(_ value: Any?) -> Any? {
|
||||||
if let value = value as? String {
|
return stringify(value).capitalized
|
||||||
|
}
|
||||||
|
|
||||||
|
func uppercase(_ value: Any?) -> Any? {
|
||||||
|
return stringify(value).uppercased()
|
||||||
|
}
|
||||||
|
|
||||||
|
func lowercase(_ value: Any?) -> Any? {
|
||||||
|
return stringify(value).lowercased()
|
||||||
|
}
|
||||||
|
|
||||||
|
func defaultFilter(value: Any?, arguments: [Any?]) -> Any? {
|
||||||
|
if let value = value {
|
||||||
return value
|
return value
|
||||||
} else if let value = value as? CustomStringConvertible {
|
}
|
||||||
return value.description
|
|
||||||
|
for argument in arguments {
|
||||||
|
if let argument = argument {
|
||||||
|
return argument
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func capitalise(value: Any?) -> Any? {
|
func joinFilter(value: Any?, arguments: [Any?]) throws -> Any? {
|
||||||
if let value = toString(value) {
|
guard arguments.count < 2 else {
|
||||||
return value.capitalizedString
|
throw TemplateSyntaxError("'join' filter takes a single argument")
|
||||||
}
|
}
|
||||||
|
|
||||||
return value
|
let separator = stringify(arguments.first ?? "")
|
||||||
}
|
|
||||||
|
if let value = value as? [Any] {
|
||||||
func uppercase(value: Any?) -> Any? {
|
return value
|
||||||
if let value = toString(value) {
|
.map(stringify)
|
||||||
return value.uppercaseString
|
.joined(separator: separator)
|
||||||
}
|
|
||||||
|
|
||||||
return value
|
|
||||||
}
|
|
||||||
|
|
||||||
func lowercase(value: Any?) -> Any? {
|
|
||||||
if let value = toString(value) {
|
|
||||||
return value.lowercaseString
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return value
|
return value
|
||||||
|
|||||||
@@ -1,17 +1,25 @@
|
|||||||
public class ForNode : NodeType {
|
import Foundation
|
||||||
let variable:Variable
|
|
||||||
let loopVariable:String
|
class ForNode : NodeType {
|
||||||
|
let resolvable: Resolvable
|
||||||
|
let loopVariables: [String]
|
||||||
let nodes:[NodeType]
|
let nodes:[NodeType]
|
||||||
let emptyNodes: [NodeType]
|
let emptyNodes: [NodeType]
|
||||||
|
let `where`: Expression?
|
||||||
|
|
||||||
public class func parse(parser:TokenParser, token:Token) throws -> NodeType {
|
class func parse(_ parser:TokenParser, token:Token) throws -> NodeType {
|
||||||
let components = token.components()
|
let components = token.components()
|
||||||
|
|
||||||
guard components.count == 4 && components[2] == "in" else {
|
guard components.count >= 2 && components[2] == "in" &&
|
||||||
throw TemplateSyntaxError("'for' statements should use the following 'for x in y' `\(token.contents)`.")
|
(components.count == 4 || (components.count >= 6 && components[4] == "where")) else {
|
||||||
|
throw TemplateSyntaxError("'for' statements should use the following 'for x in y where condition' `\(token.contents)`.")
|
||||||
}
|
}
|
||||||
|
|
||||||
let loopVariable = components[1]
|
let loopVariables = components[1].characters
|
||||||
|
.split(separator: ",")
|
||||||
|
.map(String.init)
|
||||||
|
.map { $0.trimmingCharacters(in: CharacterSet.whitespaces) }
|
||||||
|
|
||||||
let variable = components[3]
|
let variable = components[3]
|
||||||
|
|
||||||
var emptyNodes = [NodeType]()
|
var emptyNodes = [NodeType]()
|
||||||
@@ -24,35 +32,92 @@ public class ForNode : NodeType {
|
|||||||
|
|
||||||
if token.contents == "empty" {
|
if token.contents == "empty" {
|
||||||
emptyNodes = try parser.parse(until(["endfor"]))
|
emptyNodes = try parser.parse(until(["endfor"]))
|
||||||
parser.nextToken()
|
_ = parser.nextToken()
|
||||||
}
|
}
|
||||||
|
|
||||||
return ForNode(variable: variable, loopVariable: loopVariable, nodes: forNodes, emptyNodes:emptyNodes)
|
let filter = try parser.compileFilter(variable)
|
||||||
|
let `where`: Expression?
|
||||||
|
if components.count >= 6 {
|
||||||
|
`where` = try parseExpression(components: Array(components.suffix(from: 5)), tokenParser: parser)
|
||||||
|
} else {
|
||||||
|
`where` = nil
|
||||||
|
}
|
||||||
|
return ForNode(resolvable: filter, loopVariables: loopVariables, nodes: forNodes, emptyNodes:emptyNodes, where: `where`)
|
||||||
}
|
}
|
||||||
|
|
||||||
public init(variable:String, loopVariable:String, nodes:[NodeType], emptyNodes:[NodeType]) {
|
init(resolvable: Resolvable, loopVariables: [String], nodes:[NodeType], emptyNodes:[NodeType], where: Expression? = nil) {
|
||||||
self.variable = Variable(variable)
|
self.resolvable = resolvable
|
||||||
self.loopVariable = loopVariable
|
self.loopVariables = loopVariables
|
||||||
self.nodes = nodes
|
self.nodes = nodes
|
||||||
self.emptyNodes = emptyNodes
|
self.emptyNodes = emptyNodes
|
||||||
|
self.where = `where`
|
||||||
}
|
}
|
||||||
|
|
||||||
public func render(context: Context) throws -> String {
|
func push<Result>(value: Any, context: Context, closure: () throws -> (Result)) rethrows -> Result {
|
||||||
let values = try variable.resolve(context)
|
if loopVariables.isEmpty {
|
||||||
|
return try context.push() {
|
||||||
|
return try closure()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if let values = values as? [Any] where values.count > 0 {
|
if let value = value as? (Any, Any) {
|
||||||
|
let first = loopVariables[0]
|
||||||
|
|
||||||
|
if loopVariables.count == 2 {
|
||||||
|
let second = loopVariables[1]
|
||||||
|
|
||||||
|
return try context.push(dictionary: [first: value.0, second: value.1]) {
|
||||||
|
return try closure()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return try context.push(dictionary: [first: value.0]) {
|
||||||
|
return try closure()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return try context.push(dictionary: [loopVariables.first!: value]) {
|
||||||
|
return try closure()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func render(_ context: Context) throws -> String {
|
||||||
|
let resolved = try resolvable.resolve(context)
|
||||||
|
|
||||||
|
var values: [Any]
|
||||||
|
|
||||||
|
if let dictionary = resolved as? [String: Any], !dictionary.isEmpty {
|
||||||
|
values = dictionary.map { ($0.key, $0.value) }
|
||||||
|
} else if let array = resolved as? [Any] {
|
||||||
|
values = array
|
||||||
|
} else {
|
||||||
|
values = []
|
||||||
|
}
|
||||||
|
|
||||||
|
if let `where` = self.where {
|
||||||
|
values = try values.filter({ item -> Bool in
|
||||||
|
return try push(value: item, context: context) {
|
||||||
|
try `where`.evaluate(context: context)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
if !values.isEmpty {
|
||||||
let count = values.count
|
let count = values.count
|
||||||
return try values.enumerate().map { index, item in
|
|
||||||
|
return try values.enumerated().map { index, item in
|
||||||
let forContext: [String: Any] = [
|
let forContext: [String: Any] = [
|
||||||
"first": index == 0,
|
"first": index == 0,
|
||||||
"last": index == (count - 1),
|
"last": index == (count - 1),
|
||||||
"counter": index + 1,
|
"counter": index + 1,
|
||||||
]
|
]
|
||||||
|
|
||||||
return try context.push([loopVariable: item, "forloop": forContext]) {
|
return try context.push(dictionary: ["forloop": forContext]) {
|
||||||
try renderNodes(nodes, context)
|
return try push(value: item, context: context) {
|
||||||
|
try renderNodes(nodes, context)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}.joinWithSeparator("")
|
}.joined(separator: "")
|
||||||
}
|
}
|
||||||
|
|
||||||
return try context.push {
|
return try context.push {
|
||||||
|
|||||||
@@ -1,37 +1,226 @@
|
|||||||
public class IfNode : NodeType {
|
enum Operator {
|
||||||
public let variable:Variable
|
case infix(String, Int, InfixOperator.Type)
|
||||||
public let trueNodes:[NodeType]
|
case prefix(String, Int, PrefixOperator.Type)
|
||||||
public let falseNodes:[NodeType]
|
|
||||||
|
|
||||||
public class func parse(parser:TokenParser, token:Token) throws -> NodeType {
|
var name: String {
|
||||||
let components = token.components()
|
switch self {
|
||||||
guard components.count == 2 else {
|
case .infix(let name, _, _):
|
||||||
throw TemplateSyntaxError("'if' statements should use the following 'if condition' `\(token.contents)`.")
|
return name
|
||||||
|
case .prefix(let name, _, _):
|
||||||
|
return name
|
||||||
}
|
}
|
||||||
let variable = components[1]
|
}
|
||||||
var trueNodes = [NodeType]()
|
}
|
||||||
var falseNodes = [NodeType]()
|
|
||||||
|
|
||||||
trueNodes = try parser.parse(until(["endif", "else"]))
|
|
||||||
|
|
||||||
guard let token = parser.nextToken() else {
|
let operators: [Operator] = [
|
||||||
|
.infix("or", 6, OrExpression.self),
|
||||||
|
.infix("and", 7, AndExpression.self),
|
||||||
|
.prefix("not", 8, NotExpression.self),
|
||||||
|
.infix("==", 10, EqualityExpression.self),
|
||||||
|
.infix("!=", 10, InequalityExpression.self),
|
||||||
|
.infix(">", 10, MoreThanExpression.self),
|
||||||
|
.infix(">=", 10, MoreThanEqualExpression.self),
|
||||||
|
.infix("<", 10, LessThanExpression.self),
|
||||||
|
.infix("<=", 10, LessThanEqualExpression.self),
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
func findOperator(name: String) -> Operator? {
|
||||||
|
for op in operators {
|
||||||
|
if op.name == name {
|
||||||
|
return op
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
enum IfToken {
|
||||||
|
case infix(name: String, bindingPower: Int, op: InfixOperator.Type)
|
||||||
|
case prefix(name: String, bindingPower: Int, op: PrefixOperator.Type)
|
||||||
|
case variable(Resolvable)
|
||||||
|
case end
|
||||||
|
|
||||||
|
var bindingPower: Int {
|
||||||
|
switch self {
|
||||||
|
case .infix(_, let bindingPower, _):
|
||||||
|
return bindingPower
|
||||||
|
case .prefix(_, let bindingPower, _):
|
||||||
|
return bindingPower
|
||||||
|
case .variable(_):
|
||||||
|
return 0
|
||||||
|
case .end:
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func nullDenotation(parser: IfExpressionParser) throws -> Expression {
|
||||||
|
switch self {
|
||||||
|
case .infix(let name, _, _):
|
||||||
|
throw TemplateSyntaxError("'if' expression error: infix operator '\(name)' doesn't have a left hand side")
|
||||||
|
case .prefix(_, let bindingPower, let op):
|
||||||
|
let expression = try parser.expression(bindingPower: bindingPower)
|
||||||
|
return op.init(expression: expression)
|
||||||
|
case .variable(let variable):
|
||||||
|
return VariableExpression(variable: variable)
|
||||||
|
case .end:
|
||||||
|
throw TemplateSyntaxError("'if' expression error: end")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func leftDenotation(left: Expression, parser: IfExpressionParser) throws -> Expression {
|
||||||
|
switch self {
|
||||||
|
case .infix(_, let bindingPower, let op):
|
||||||
|
let right = try parser.expression(bindingPower: bindingPower)
|
||||||
|
return op.init(lhs: left, rhs: right)
|
||||||
|
case .prefix(let name, _, _):
|
||||||
|
throw TemplateSyntaxError("'if' expression error: prefix operator '\(name)' was called with a left hand side")
|
||||||
|
case .variable(let variable):
|
||||||
|
throw TemplateSyntaxError("'if' expression error: variable '\(variable)' was called with a left hand side")
|
||||||
|
case .end:
|
||||||
|
throw TemplateSyntaxError("'if' expression error: end")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var isEnd: Bool {
|
||||||
|
switch self {
|
||||||
|
case .end:
|
||||||
|
return true
|
||||||
|
default:
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
final class IfExpressionParser {
|
||||||
|
let tokens: [IfToken]
|
||||||
|
var position: Int = 0
|
||||||
|
|
||||||
|
init(components: [String], tokenParser: TokenParser) throws {
|
||||||
|
self.tokens = try components.map { component in
|
||||||
|
if let op = findOperator(name: component) {
|
||||||
|
switch op {
|
||||||
|
case .infix(let name, let bindingPower, let cls):
|
||||||
|
return .infix(name: name, bindingPower: bindingPower, op: cls)
|
||||||
|
case .prefix(let name, let bindingPower, let cls):
|
||||||
|
return .prefix(name: name, bindingPower: bindingPower, op: cls)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return .variable(try tokenParser.compileFilter(component))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var currentToken: IfToken {
|
||||||
|
if tokens.count > position {
|
||||||
|
return tokens[position]
|
||||||
|
}
|
||||||
|
|
||||||
|
return .end
|
||||||
|
}
|
||||||
|
|
||||||
|
var nextToken: IfToken {
|
||||||
|
position += 1
|
||||||
|
return currentToken
|
||||||
|
}
|
||||||
|
|
||||||
|
func parse() throws -> Expression {
|
||||||
|
let expression = try self.expression()
|
||||||
|
|
||||||
|
if !currentToken.isEnd {
|
||||||
|
throw TemplateSyntaxError("'if' expression error: dangling token")
|
||||||
|
}
|
||||||
|
|
||||||
|
return expression
|
||||||
|
}
|
||||||
|
|
||||||
|
func expression(bindingPower: Int = 0) throws -> Expression {
|
||||||
|
var token = currentToken
|
||||||
|
position += 1
|
||||||
|
|
||||||
|
var left = try token.nullDenotation(parser: self)
|
||||||
|
|
||||||
|
while bindingPower < currentToken.bindingPower {
|
||||||
|
token = currentToken
|
||||||
|
position += 1
|
||||||
|
left = try token.leftDenotation(left: left, parser: self)
|
||||||
|
}
|
||||||
|
|
||||||
|
return left
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
func parseExpression(components: [String], tokenParser: TokenParser) throws -> Expression {
|
||||||
|
let parser = try IfExpressionParser(components: components, tokenParser: tokenParser)
|
||||||
|
return try parser.parse()
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// Represents an if condition and the associated nodes when the condition
|
||||||
|
/// evaluates
|
||||||
|
final class IfCondition {
|
||||||
|
let expression: Expression?
|
||||||
|
let nodes: [NodeType]
|
||||||
|
|
||||||
|
init(expression: Expression?, nodes: [NodeType]) {
|
||||||
|
self.expression = expression
|
||||||
|
self.nodes = nodes
|
||||||
|
}
|
||||||
|
|
||||||
|
func render(_ context: Context) throws -> String {
|
||||||
|
return try context.push {
|
||||||
|
return try renderNodes(nodes, context)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
class IfNode : NodeType {
|
||||||
|
let conditions: [IfCondition]
|
||||||
|
|
||||||
|
class func parse(_ parser: TokenParser, token: Token) throws -> NodeType {
|
||||||
|
var components = token.components()
|
||||||
|
components.removeFirst()
|
||||||
|
|
||||||
|
let expression = try parseExpression(components: components, tokenParser: parser)
|
||||||
|
let nodes = try parser.parse(until(["endif", "elif", "else"]))
|
||||||
|
var conditions: [IfCondition] = [
|
||||||
|
IfCondition(expression: expression, nodes: nodes)
|
||||||
|
]
|
||||||
|
|
||||||
|
var token = parser.nextToken()
|
||||||
|
while let current = token, current.contents.hasPrefix("elif") {
|
||||||
|
var components = current.components()
|
||||||
|
components.removeFirst()
|
||||||
|
let expression = try parseExpression(components: components, tokenParser: parser)
|
||||||
|
|
||||||
|
let nodes = try parser.parse(until(["endif", "elif", "else"]))
|
||||||
|
token = parser.nextToken()
|
||||||
|
conditions.append(IfCondition(expression: expression, nodes: nodes))
|
||||||
|
}
|
||||||
|
|
||||||
|
if let current = token, current.contents == "else" {
|
||||||
|
conditions.append(IfCondition(expression: nil, nodes: try parser.parse(until(["endif"]))))
|
||||||
|
token = parser.nextToken()
|
||||||
|
}
|
||||||
|
|
||||||
|
guard let current = token, current.contents == "endif" else {
|
||||||
throw TemplateSyntaxError("`endif` was not found.")
|
throw TemplateSyntaxError("`endif` was not found.")
|
||||||
}
|
}
|
||||||
|
|
||||||
if token.contents == "else" {
|
return IfNode(conditions: conditions)
|
||||||
falseNodes = try parser.parse(until(["endif"]))
|
|
||||||
parser.nextToken()
|
|
||||||
}
|
|
||||||
|
|
||||||
return IfNode(variable: variable, trueNodes: trueNodes, falseNodes: falseNodes)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public class func parse_ifnot(parser:TokenParser, token:Token) throws -> NodeType {
|
class func parse_ifnot(_ parser: TokenParser, token: Token) throws -> NodeType {
|
||||||
let components = token.components()
|
var components = token.components()
|
||||||
guard components.count == 2 else {
|
guard components.count == 2 else {
|
||||||
throw TemplateSyntaxError("'ifnot' statements should use the following 'if condition' `\(token.contents)`.")
|
throw TemplateSyntaxError("'ifnot' statements should use the following 'ifnot condition' `\(token.contents)`.")
|
||||||
}
|
}
|
||||||
let variable = components[1]
|
components.removeFirst()
|
||||||
var trueNodes = [NodeType]()
|
var trueNodes = [NodeType]()
|
||||||
var falseNodes = [NodeType]()
|
var falseNodes = [NodeType]()
|
||||||
|
|
||||||
@@ -43,36 +232,33 @@ public class IfNode : NodeType {
|
|||||||
|
|
||||||
if token.contents == "else" {
|
if token.contents == "else" {
|
||||||
trueNodes = try parser.parse(until(["endif"]))
|
trueNodes = try parser.parse(until(["endif"]))
|
||||||
parser.nextToken()
|
_ = parser.nextToken()
|
||||||
}
|
}
|
||||||
|
|
||||||
return IfNode(variable: variable, trueNodes: trueNodes, falseNodes: falseNodes)
|
let expression = try parseExpression(components: components, tokenParser: parser)
|
||||||
|
return IfNode(conditions: [
|
||||||
|
IfCondition(expression: expression, nodes: trueNodes),
|
||||||
|
IfCondition(expression: nil, nodes: falseNodes),
|
||||||
|
])
|
||||||
}
|
}
|
||||||
|
|
||||||
public init(variable:String, trueNodes:[NodeType], falseNodes:[NodeType]) {
|
init(conditions: [IfCondition]) {
|
||||||
self.variable = Variable(variable)
|
self.conditions = conditions
|
||||||
self.trueNodes = trueNodes
|
|
||||||
self.falseNodes = falseNodes
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public func render(context: Context) throws -> String {
|
func render(_ context: Context) throws -> String {
|
||||||
let result = try variable.resolve(context)
|
for condition in conditions {
|
||||||
var truthy = false
|
if let expression = condition.expression {
|
||||||
|
let truthy = try expression.evaluate(context: context)
|
||||||
|
|
||||||
if let result = result as? [Any] {
|
if truthy {
|
||||||
truthy = !result.isEmpty
|
return try condition.render(context)
|
||||||
} else if let result = result as? [String:Any] {
|
}
|
||||||
truthy = !result.isEmpty
|
|
||||||
} else if result != nil {
|
|
||||||
truthy = true
|
|
||||||
}
|
|
||||||
|
|
||||||
return try context.push {
|
|
||||||
if truthy {
|
|
||||||
return try renderNodes(trueNodes, context)
|
|
||||||
} else {
|
} else {
|
||||||
return try renderNodes(falseNodes, context)
|
return try condition.render(context)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return ""
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,10 +1,10 @@
|
|||||||
import PathKit
|
import PathKit
|
||||||
|
|
||||||
|
|
||||||
public class IncludeNode : NodeType {
|
class IncludeNode : NodeType {
|
||||||
public let templateName: Variable
|
let templateName: Variable
|
||||||
|
|
||||||
public class func parse(parser: TokenParser, token: Token) throws -> NodeType {
|
class func parse(_ parser: TokenParser, token: Token) throws -> NodeType {
|
||||||
let bits = token.components()
|
let bits = token.components()
|
||||||
|
|
||||||
guard bits.count == 2 else {
|
guard bits.count == 2 else {
|
||||||
@@ -14,25 +14,20 @@ public class IncludeNode : NodeType {
|
|||||||
return IncludeNode(templateName: Variable(bits[1]))
|
return IncludeNode(templateName: Variable(bits[1]))
|
||||||
}
|
}
|
||||||
|
|
||||||
public init(templateName: Variable) {
|
init(templateName: Variable) {
|
||||||
self.templateName = templateName
|
self.templateName = templateName
|
||||||
}
|
}
|
||||||
|
|
||||||
public func render(context: Context) throws -> String {
|
func render(_ context: Context) throws -> String {
|
||||||
guard let loader = context["loader"] as? TemplateLoader else {
|
|
||||||
throw TemplateSyntaxError("Template loader not in context")
|
|
||||||
}
|
|
||||||
|
|
||||||
guard let templateName = try self.templateName.resolve(context) as? String else {
|
guard let templateName = try self.templateName.resolve(context) as? String else {
|
||||||
throw TemplateSyntaxError("'\(self.templateName)' could not be resolved as a string")
|
throw TemplateSyntaxError("'\(self.templateName)' could not be resolved as a string")
|
||||||
}
|
}
|
||||||
|
|
||||||
guard let template = loader.loadTemplate(templateName) else {
|
let template = try context.environment.loadTemplate(name: templateName)
|
||||||
let paths = loader.paths.map { $0.description }.joinWithSeparator(", ")
|
|
||||||
throw TemplateSyntaxError("'\(templateName)' template not found in \(paths)")
|
|
||||||
}
|
|
||||||
|
|
||||||
return try template.render(context)
|
return try context.push {
|
||||||
|
return try template.render(context)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,20 +1,20 @@
|
|||||||
class BlockContext {
|
class BlockContext {
|
||||||
class var contextKey: String { return "block_context" }
|
class var contextKey: String { return "block_context" }
|
||||||
|
|
||||||
var blocks: [String:BlockNode]
|
var blocks: [String: BlockNode]
|
||||||
|
|
||||||
init(blocks: [String:BlockNode]) {
|
init(blocks: [String: BlockNode]) {
|
||||||
self.blocks = blocks
|
self.blocks = blocks
|
||||||
}
|
}
|
||||||
|
|
||||||
func pop(blockName: String) -> BlockNode? {
|
func pop(_ blockName: String) -> BlockNode? {
|
||||||
return blocks.removeValueForKey(blockName)
|
return blocks.removeValue(forKey: blockName)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
extension CollectionType {
|
extension Collection {
|
||||||
func any(closure: Generator.Element -> Bool) -> Generator.Element? {
|
func any(_ closure: (Iterator.Element) -> Bool) -> Iterator.Element? {
|
||||||
for element in self {
|
for element in self {
|
||||||
if closure(element) {
|
if closure(element) {
|
||||||
return element
|
return element
|
||||||
@@ -30,7 +30,7 @@ class ExtendsNode : NodeType {
|
|||||||
let templateName: Variable
|
let templateName: Variable
|
||||||
let blocks: [String:BlockNode]
|
let blocks: [String:BlockNode]
|
||||||
|
|
||||||
class func parse(parser: TokenParser, token: Token) throws -> NodeType {
|
class func parse(_ parser: TokenParser, token: Token) throws -> NodeType {
|
||||||
let bits = token.components()
|
let bits = token.components()
|
||||||
|
|
||||||
guard bits.count == 2 else {
|
guard bits.count == 2 else {
|
||||||
@@ -42,10 +42,9 @@ class ExtendsNode : NodeType {
|
|||||||
throw TemplateSyntaxError("'extends' cannot appear more than once in the same template")
|
throw TemplateSyntaxError("'extends' cannot appear more than once in the same template")
|
||||||
}
|
}
|
||||||
|
|
||||||
let blockNodes = parsedNodes.filter { node in node is BlockNode }
|
let blockNodes = parsedNodes.flatMap { $0 as? BlockNode }
|
||||||
|
|
||||||
let nodes = blockNodes.reduce([String:BlockNode]()) { (accumulator, node:NodeType) -> [String:BlockNode] in
|
let nodes = blockNodes.reduce([String: BlockNode]()) { (accumulator, node) -> [String: BlockNode] in
|
||||||
let node = (node as! BlockNode)
|
|
||||||
var dict = accumulator
|
var dict = accumulator
|
||||||
dict[node.name] = node
|
dict[node.name] = node
|
||||||
return dict
|
return dict
|
||||||
@@ -59,22 +58,27 @@ class ExtendsNode : NodeType {
|
|||||||
self.blocks = blocks
|
self.blocks = blocks
|
||||||
}
|
}
|
||||||
|
|
||||||
func render(context: Context) throws -> String {
|
func render(_ context: Context) throws -> String {
|
||||||
guard let loader = context["loader"] as? TemplateLoader else {
|
|
||||||
throw TemplateSyntaxError("Template loader not in context")
|
|
||||||
}
|
|
||||||
|
|
||||||
guard let templateName = try self.templateName.resolve(context) as? String else {
|
guard let templateName = try self.templateName.resolve(context) as? String else {
|
||||||
throw TemplateSyntaxError("'\(self.templateName)' could not be resolved as a string")
|
throw TemplateSyntaxError("'\(self.templateName)' could not be resolved as a string")
|
||||||
}
|
}
|
||||||
|
|
||||||
guard let template = loader.loadTemplate(templateName) else {
|
let template = try context.environment.loadTemplate(name: templateName)
|
||||||
let paths:String = loader.paths.map { $0.description }.joinWithSeparator(", ")
|
|
||||||
throw TemplateSyntaxError("'\(templateName)' template not found in \(paths)")
|
let blockContext: BlockContext
|
||||||
|
if let context = context[BlockContext.contextKey] as? BlockContext {
|
||||||
|
blockContext = context
|
||||||
|
|
||||||
|
for (key, value) in blocks {
|
||||||
|
if !blockContext.blocks.keys.contains(key) {
|
||||||
|
blockContext.blocks[key] = value
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
blockContext = BlockContext(blocks: blocks)
|
||||||
}
|
}
|
||||||
|
|
||||||
let blockContext = BlockContext(blocks: blocks)
|
return try context.push(dictionary: [BlockContext.contextKey: blockContext]) {
|
||||||
return try context.push([BlockContext.contextKey: blockContext]) {
|
|
||||||
return try template.render(context)
|
return try template.render(context)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -85,16 +89,16 @@ class BlockNode : NodeType {
|
|||||||
let name: String
|
let name: String
|
||||||
let nodes: [NodeType]
|
let nodes: [NodeType]
|
||||||
|
|
||||||
class func parse(parser: TokenParser, token: Token) throws -> NodeType {
|
class func parse(_ parser: TokenParser, token: Token) throws -> NodeType {
|
||||||
let bits = token.components()
|
let bits = token.components()
|
||||||
|
|
||||||
guard bits.count == 2 else {
|
guard bits.count == 2 else {
|
||||||
throw TemplateSyntaxError("'block' tag takes one argument, the template file to be included")
|
throw TemplateSyntaxError("'block' tag takes one argument, the block name")
|
||||||
}
|
}
|
||||||
|
|
||||||
let blockName = bits[1]
|
let blockName = bits[1]
|
||||||
let nodes = try parser.parse(until(["endblock"]))
|
let nodes = try parser.parse(until(["endblock"]))
|
||||||
parser.nextToken()
|
_ = parser.nextToken()
|
||||||
return BlockNode(name:blockName, nodes:nodes)
|
return BlockNode(name:blockName, nodes:nodes)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -103,9 +107,11 @@ class BlockNode : NodeType {
|
|||||||
self.nodes = nodes
|
self.nodes = nodes
|
||||||
}
|
}
|
||||||
|
|
||||||
func render(context: Context) throws -> String {
|
func render(_ context: Context) throws -> String {
|
||||||
if let blockContext = context[BlockContext.contextKey] as? BlockContext, node = blockContext.pop(name) {
|
if let blockContext = context[BlockContext.contextKey] as? BlockContext, let node = blockContext.pop(name) {
|
||||||
return try node.render(context)
|
return try context.push(dictionary: ["block": ["super": self]]) {
|
||||||
|
return try node.render(context)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return try renderNodes(nodes, context)
|
return try renderNodes(nodes, context)
|
||||||
|
|||||||
@@ -1,28 +1,30 @@
|
|||||||
public struct Lexer {
|
struct Lexer {
|
||||||
public let templateString: String
|
let templateString: String
|
||||||
|
|
||||||
public init(templateString: String) {
|
init(templateString: String) {
|
||||||
self.templateString = templateString
|
self.templateString = templateString
|
||||||
}
|
}
|
||||||
|
|
||||||
func createToken(string:String) -> Token {
|
func createToken(string:String) -> Token {
|
||||||
func strip() -> String {
|
func strip() -> String {
|
||||||
return string[string.startIndex.successor().successor()..<string.endIndex.predecessor().predecessor()].trim(" ")
|
let start = string.index(string.startIndex, offsetBy: 2)
|
||||||
|
let end = string.index(string.endIndex, offsetBy: -2)
|
||||||
|
return string[start..<end].trim(character: " ")
|
||||||
}
|
}
|
||||||
|
|
||||||
if string.hasPrefix("{{") {
|
if string.hasPrefix("{{") {
|
||||||
return Token.Variable(value: strip())
|
return .variable(value: strip())
|
||||||
} else if string.hasPrefix("{%") {
|
} else if string.hasPrefix("{%") {
|
||||||
return Token.Block(value: strip())
|
return .block(value: strip())
|
||||||
} else if string.hasPrefix("{#") {
|
} else if string.hasPrefix("{#") {
|
||||||
return Token.Comment(value: strip())
|
return .comment(value: strip())
|
||||||
}
|
}
|
||||||
|
|
||||||
return Token.Text(value: string)
|
return .text(value: string)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns an array of tokens from a given template string.
|
/// Returns an array of tokens from a given template string.
|
||||||
public func tokenize() -> [Token] {
|
func tokenize() -> [Token] {
|
||||||
var tokens: [Token] = []
|
var tokens: [Token] = []
|
||||||
|
|
||||||
let scanner = Scanner(templateString)
|
let scanner = Scanner(templateString)
|
||||||
@@ -36,14 +38,14 @@ public struct Lexer {
|
|||||||
while !scanner.isEmpty {
|
while !scanner.isEmpty {
|
||||||
if let text = scanner.scan(until: ["{{", "{%", "{#"]) {
|
if let text = scanner.scan(until: ["{{", "{%", "{#"]) {
|
||||||
if !text.1.isEmpty {
|
if !text.1.isEmpty {
|
||||||
tokens.append(createToken(text.1))
|
tokens.append(createToken(string: text.1))
|
||||||
}
|
}
|
||||||
|
|
||||||
let end = map[text.0]!
|
let end = map[text.0]!
|
||||||
let result = scanner.scan(until: end, returnUntil: true)
|
let result = scanner.scan(until: end, returnUntil: true)
|
||||||
tokens.append(createToken(result))
|
tokens.append(createToken(string: result))
|
||||||
} else {
|
} else {
|
||||||
tokens.append(createToken(scanner.content))
|
tokens.append(createToken(string: scanner.content))
|
||||||
scanner.content = ""
|
scanner.content = ""
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -64,49 +66,50 @@ class Scanner {
|
|||||||
return content.isEmpty
|
return content.isEmpty
|
||||||
}
|
}
|
||||||
|
|
||||||
func scan(until until: String, returnUntil: Bool = false) -> String {
|
func scan(until: String, returnUntil: Bool = false) -> String {
|
||||||
if until.isEmpty {
|
if until.isEmpty {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
var index = content.startIndex
|
var index = content.startIndex
|
||||||
while index != content.endIndex {
|
while index != content.endIndex {
|
||||||
let substring = content[index..<content.endIndex]
|
let substring = content.substring(from: index)
|
||||||
|
|
||||||
if substring.hasPrefix(until) {
|
if substring.hasPrefix(until) {
|
||||||
let result = content[content.startIndex..<index]
|
let result = content.substring(to: index)
|
||||||
content = substring
|
content = substring
|
||||||
|
|
||||||
if returnUntil {
|
if returnUntil {
|
||||||
content = content[until.endIndex..<content.endIndex]
|
content = content.substring(from: until.endIndex)
|
||||||
return result + until
|
return result + until
|
||||||
}
|
}
|
||||||
|
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
index = index.successor()
|
index = content.index(after: index)
|
||||||
}
|
}
|
||||||
|
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
func scan(until until: [String]) -> (String, String)? {
|
func scan(until: [String]) -> (String, String)? {
|
||||||
if until.isEmpty {
|
if until.isEmpty {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
var index = content.startIndex
|
var index = content.startIndex
|
||||||
while index != content.endIndex {
|
while index != content.endIndex {
|
||||||
let substring = content[index..<content.endIndex]
|
let substring = content.substring(from: index)
|
||||||
for string in until {
|
for string in until {
|
||||||
if substring.hasPrefix(string) {
|
if substring.hasPrefix(string) {
|
||||||
let result = content[content.startIndex..<index]
|
let result = content.substring(to: index)
|
||||||
content = substring
|
content = substring
|
||||||
return (string, result)
|
return (string, result)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
index = index.successor()
|
index = content.index(after: index)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
@@ -117,31 +120,33 @@ class Scanner {
|
|||||||
extension String {
|
extension String {
|
||||||
func findFirstNot(character: Character) -> String.Index? {
|
func findFirstNot(character: Character) -> String.Index? {
|
||||||
var index = startIndex
|
var index = startIndex
|
||||||
|
|
||||||
while index != endIndex {
|
while index != endIndex {
|
||||||
if character != self[index] {
|
if character != self[index] {
|
||||||
return index
|
return index
|
||||||
}
|
}
|
||||||
index = index.successor()
|
index = self.index(after: index)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func findLastNot(character: Character) -> String.Index? {
|
func findLastNot(character: Character) -> String.Index? {
|
||||||
var index = endIndex.predecessor()
|
var index = self.index(before: endIndex)
|
||||||
|
|
||||||
while index != startIndex {
|
while index != startIndex {
|
||||||
if character != self[index] {
|
if character != self[index] {
|
||||||
return index.successor()
|
return self.index(after: index)
|
||||||
}
|
}
|
||||||
index = index.predecessor()
|
index = self.index(before: index)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func trim(character: Character) -> String {
|
func trim(character: Character) -> String {
|
||||||
let first = findFirstNot(character) ?? startIndex
|
let first = findFirstNot(character: character) ?? startIndex
|
||||||
let last = findLastNot(character) ?? endIndex
|
let last = findLastNot(character: character) ?? endIndex
|
||||||
return self[first..<last]
|
return self[first..<last]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
103
Sources/Loader.swift
Normal file
103
Sources/Loader.swift
Normal file
@@ -0,0 +1,103 @@
|
|||||||
|
import Foundation
|
||||||
|
import PathKit
|
||||||
|
|
||||||
|
|
||||||
|
public protocol Loader {
|
||||||
|
func loadTemplate(name: String, environment: Environment) throws -> Template
|
||||||
|
func loadTemplate(names: [String], environment: Environment) throws -> Template
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
extension Loader {
|
||||||
|
public func loadTemplate(names: [String], environment: Environment) throws -> Template {
|
||||||
|
for name in names {
|
||||||
|
do {
|
||||||
|
return try loadTemplate(name: name, environment: environment)
|
||||||
|
} catch is TemplateDoesNotExist {
|
||||||
|
continue
|
||||||
|
} catch {
|
||||||
|
throw error
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
throw TemplateDoesNotExist(templateNames: names, loader: self)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// A class for loading a template from disk
|
||||||
|
public class FileSystemLoader: Loader, CustomStringConvertible {
|
||||||
|
public let paths: [Path]
|
||||||
|
|
||||||
|
public init(paths: [Path]) {
|
||||||
|
self.paths = paths
|
||||||
|
}
|
||||||
|
|
||||||
|
public init(bundle: [Bundle]) {
|
||||||
|
self.paths = bundle.map {
|
||||||
|
return Path($0.bundlePath)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public var description: String {
|
||||||
|
return "FileSystemLoader(\(paths))"
|
||||||
|
}
|
||||||
|
|
||||||
|
public func loadTemplate(name: String, environment: Environment) throws -> Template {
|
||||||
|
for path in paths {
|
||||||
|
let templatePath = try path.safeJoin(path: Path(name))
|
||||||
|
|
||||||
|
if !templatePath.exists {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
let content: String = try templatePath.read()
|
||||||
|
return environment.templateClass.init(templateString: content, environment: environment, name: name)
|
||||||
|
}
|
||||||
|
|
||||||
|
throw TemplateDoesNotExist(templateNames: [name], loader: self)
|
||||||
|
}
|
||||||
|
|
||||||
|
public func loadTemplate(names: [String], environment: Environment) throws -> Template {
|
||||||
|
for path in paths {
|
||||||
|
for templateName in names {
|
||||||
|
let templatePath = try path.safeJoin(path: Path(templateName))
|
||||||
|
|
||||||
|
if templatePath.exists {
|
||||||
|
let content: String = try templatePath.read()
|
||||||
|
return environment.templateClass.init(templateString: content, environment: environment, name: templateName)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
throw TemplateDoesNotExist(templateNames: names, loader: self)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
extension Path {
|
||||||
|
func safeJoin(path: Path) throws -> Path {
|
||||||
|
let newPath = self + path
|
||||||
|
|
||||||
|
if !newPath.absolute().description.hasPrefix(absolute().description) {
|
||||||
|
throw SuspiciousFileOperation(basePath: self, path: newPath)
|
||||||
|
}
|
||||||
|
|
||||||
|
return newPath
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
class SuspiciousFileOperation: Error {
|
||||||
|
let basePath: Path
|
||||||
|
let path: Path
|
||||||
|
|
||||||
|
init(basePath: Path, path: Path) {
|
||||||
|
self.basePath = basePath
|
||||||
|
self.path = path
|
||||||
|
}
|
||||||
|
|
||||||
|
var description: String {
|
||||||
|
return "Path `\(path)` is located outside of base path `\(basePath)`"
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,46 +0,0 @@
|
|||||||
public class Namespace {
|
|
||||||
public typealias TagParser = (TokenParser, Token) throws -> NodeType
|
|
||||||
|
|
||||||
var tags = [String: TagParser]()
|
|
||||||
var filters = [String: Filter]()
|
|
||||||
|
|
||||||
public init() {
|
|
||||||
registerDefaultTags()
|
|
||||||
registerDefaultFilters()
|
|
||||||
}
|
|
||||||
|
|
||||||
private func registerDefaultTags() {
|
|
||||||
registerTag("for", parser: ForNode.parse)
|
|
||||||
registerTag("if", parser: IfNode.parse)
|
|
||||||
registerTag("ifnot", parser: IfNode.parse_ifnot)
|
|
||||||
#if !os(Linux)
|
|
||||||
registerTag("now", parser: NowNode.parse)
|
|
||||||
#endif
|
|
||||||
registerTag("include", parser: IncludeNode.parse)
|
|
||||||
registerTag("extends", parser: ExtendsNode.parse)
|
|
||||||
registerTag("block", parser: BlockNode.parse)
|
|
||||||
}
|
|
||||||
|
|
||||||
private func registerDefaultFilters() {
|
|
||||||
registerFilter("capitalize", filter: capitalise)
|
|
||||||
registerFilter("uppercase", filter: uppercase)
|
|
||||||
registerFilter("lowercase", filter: lowercase)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Registers a new template tag
|
|
||||||
public func registerTag(name: String, parser: TagParser) {
|
|
||||||
tags[name] = parser
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Registers a simple template tag with a name and a handler
|
|
||||||
public func registerSimpleTag(name: String, handler: Context throws -> String) {
|
|
||||||
registerTag(name, parser: { parser, token in
|
|
||||||
return SimpleNode(handler: handler)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Registers a template filter with the given name
|
|
||||||
public func registerFilter(name: String, filter: Filter) {
|
|
||||||
filters[name] = filter
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
import Foundation
|
import Foundation
|
||||||
|
|
||||||
|
|
||||||
public struct TemplateSyntaxError : ErrorType, Equatable, CustomStringConvertible {
|
public struct TemplateSyntaxError : Error, Equatable, CustomStringConvertible {
|
||||||
public let description:String
|
public let description:String
|
||||||
|
|
||||||
public init(_ description:String) {
|
public init(_ description:String) {
|
||||||
@@ -17,23 +17,23 @@ public func ==(lhs:TemplateSyntaxError, rhs:TemplateSyntaxError) -> Bool {
|
|||||||
|
|
||||||
public protocol NodeType {
|
public protocol NodeType {
|
||||||
/// Render the node in the given context
|
/// Render the node in the given context
|
||||||
func render(context:Context) throws -> String
|
func render(_ context:Context) throws -> String
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// Render the collection of nodes in the given context
|
/// Render the collection of nodes in the given context
|
||||||
public func renderNodes(nodes:[NodeType], _ context:Context) throws -> String {
|
public func renderNodes(_ nodes:[NodeType], _ context:Context) throws -> String {
|
||||||
return try nodes.map { try $0.render(context) }.joinWithSeparator("")
|
return try nodes.map { try $0.render(context) }.joined(separator: "")
|
||||||
}
|
}
|
||||||
|
|
||||||
public class SimpleNode : NodeType {
|
public class SimpleNode : NodeType {
|
||||||
let handler:Context throws -> String
|
public let handler:(Context) throws -> String
|
||||||
|
|
||||||
public init(handler:Context throws -> String) {
|
public init(handler: @escaping (Context) throws -> String) {
|
||||||
self.handler = handler
|
self.handler = handler
|
||||||
}
|
}
|
||||||
|
|
||||||
public func render(context: Context) throws -> String {
|
public func render(_ context: Context) throws -> String {
|
||||||
return try handler(context)
|
return try handler(context)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -46,14 +46,14 @@ public class TextNode : NodeType {
|
|||||||
self.text = text
|
self.text = text
|
||||||
}
|
}
|
||||||
|
|
||||||
public func render(context:Context) throws -> String {
|
public func render(_ context:Context) throws -> String {
|
||||||
return self.text
|
return self.text
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public protocol Resolvable {
|
public protocol Resolvable {
|
||||||
func resolve(context: Context) throws -> Any?
|
func resolve(_ context: Context) throws -> Any?
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -68,17 +68,21 @@ public class VariableNode : NodeType {
|
|||||||
self.variable = Variable(variable)
|
self.variable = Variable(variable)
|
||||||
}
|
}
|
||||||
|
|
||||||
public func render(context: Context) throws -> String {
|
public func render(_ context: Context) throws -> String {
|
||||||
let result = try variable.resolve(context)
|
let result = try variable.resolve(context)
|
||||||
|
return stringify(result)
|
||||||
if let result = result as? String {
|
|
||||||
return result
|
|
||||||
} else if let result = result as? CustomStringConvertible {
|
|
||||||
return result.description
|
|
||||||
} else if let result = result as? NSObject {
|
|
||||||
return result.description
|
|
||||||
}
|
|
||||||
|
|
||||||
return ""
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
func stringify(_ result: Any?) -> String {
|
||||||
|
if let result = result as? String {
|
||||||
|
return result
|
||||||
|
} else if let result = result as? CustomStringConvertible {
|
||||||
|
return result.description
|
||||||
|
} else if let result = result as? NSObject {
|
||||||
|
return result.description
|
||||||
|
}
|
||||||
|
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|||||||
@@ -2,10 +2,10 @@
|
|||||||
import Foundation
|
import Foundation
|
||||||
|
|
||||||
|
|
||||||
public class NowNode : NodeType {
|
class NowNode : NodeType {
|
||||||
public let format:Variable
|
let format:Variable
|
||||||
|
|
||||||
public class func parse(parser:TokenParser, token:Token) throws -> NodeType {
|
class func parse(_ parser:TokenParser, token:Token) throws -> NodeType {
|
||||||
var format:Variable?
|
var format:Variable?
|
||||||
|
|
||||||
let components = token.components()
|
let components = token.components()
|
||||||
@@ -19,25 +19,25 @@ public class NowNode : NodeType {
|
|||||||
return NowNode(format:format)
|
return NowNode(format:format)
|
||||||
}
|
}
|
||||||
|
|
||||||
public init(format:Variable?) {
|
init(format:Variable?) {
|
||||||
self.format = format ?? Variable("\"yyyy-MM-dd 'at' HH:mm\"")
|
self.format = format ?? Variable("\"yyyy-MM-dd 'at' HH:mm\"")
|
||||||
}
|
}
|
||||||
|
|
||||||
public func render(context: Context) throws -> String {
|
func render(_ context: Context) throws -> String {
|
||||||
let date = NSDate()
|
let date = Date()
|
||||||
let format = try self.format.resolve(context)
|
let format = try self.format.resolve(context)
|
||||||
var formatter:NSDateFormatter?
|
var formatter:DateFormatter?
|
||||||
|
|
||||||
if let format = format as? NSDateFormatter {
|
if let format = format as? DateFormatter {
|
||||||
formatter = format
|
formatter = format
|
||||||
} else if let format = format as? String {
|
} else if let format = format as? String {
|
||||||
formatter = NSDateFormatter()
|
formatter = DateFormatter()
|
||||||
formatter!.dateFormat = format
|
formatter!.dateFormat = format
|
||||||
} else {
|
} else {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
return formatter!.stringFromDate(date)
|
return formatter!.string(from: date)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
public func until(tags: [String]) -> ((TokenParser, Token) -> Bool) {
|
public func until(_ tags: [String]) -> ((TokenParser, Token) -> Bool) {
|
||||||
return { parser, token in
|
return { parser, token in
|
||||||
if let name = token.components().first {
|
if let name = token.components().first {
|
||||||
for tag in tags {
|
for tag in tags {
|
||||||
@@ -12,18 +12,17 @@ public func until(tags: [String]) -> ((TokenParser, Token) -> Bool) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public typealias Filter = Any? throws -> Any?
|
|
||||||
|
|
||||||
/// A class for parsing an array of tokens and converts them into a collection of Node's
|
/// A class for parsing an array of tokens and converts them into a collection of Node's
|
||||||
public class TokenParser {
|
public class TokenParser {
|
||||||
public typealias TagParser = (TokenParser, Token) throws -> NodeType
|
public typealias TagParser = (TokenParser, Token) throws -> NodeType
|
||||||
|
|
||||||
private var tokens: [Token]
|
fileprivate var tokens: [Token]
|
||||||
private let namespace: Namespace
|
fileprivate let environment: Environment
|
||||||
|
|
||||||
public init(tokens: [Token], namespace: Namespace) {
|
public init(tokens: [Token], environment: Environment) {
|
||||||
self.tokens = tokens
|
self.tokens = tokens
|
||||||
self.namespace = namespace
|
self.environment = environment
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Parse the given tokens into nodes
|
/// Parse the given tokens into nodes
|
||||||
@@ -31,33 +30,28 @@ public class TokenParser {
|
|||||||
return try parse(nil)
|
return try parse(nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
public func parse(parse_until:((parser:TokenParser, token:Token) -> (Bool))?) throws -> [NodeType] {
|
public func parse(_ parse_until:((_ parser:TokenParser, _ token:Token) -> (Bool))?) throws -> [NodeType] {
|
||||||
var nodes = [NodeType]()
|
var nodes = [NodeType]()
|
||||||
|
|
||||||
while tokens.count > 0 {
|
while tokens.count > 0 {
|
||||||
let token = nextToken()!
|
let token = nextToken()!
|
||||||
|
|
||||||
switch token {
|
switch token {
|
||||||
case .Text(let text):
|
case .text(let text):
|
||||||
nodes.append(TextNode(text: text))
|
nodes.append(TextNode(text: text))
|
||||||
case .Variable:
|
case .variable:
|
||||||
nodes.append(VariableNode(variable: try compileFilter(token.contents)))
|
nodes.append(VariableNode(variable: try compileFilter(token.contents)))
|
||||||
case .Block:
|
case .block:
|
||||||
let tag = token.components().first
|
if let parse_until = parse_until , parse_until(self, token) {
|
||||||
|
|
||||||
if let parse_until = parse_until where parse_until(parser: self, token: token) {
|
|
||||||
prependToken(token)
|
prependToken(token)
|
||||||
return nodes
|
return nodes
|
||||||
}
|
}
|
||||||
|
|
||||||
if let tag = tag {
|
if let tag = token.components().first {
|
||||||
if let parser = namespace.tags[tag] {
|
let parser = try findTag(name: tag)
|
||||||
nodes.append(try parser(self, token))
|
nodes.append(try parser(self, token))
|
||||||
} else {
|
|
||||||
throw TemplateSyntaxError("Unknown template tag '\(tag)'")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
case .Comment:
|
case .comment:
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -67,25 +61,38 @@ public class TokenParser {
|
|||||||
|
|
||||||
public func nextToken() -> Token? {
|
public func nextToken() -> Token? {
|
||||||
if tokens.count > 0 {
|
if tokens.count > 0 {
|
||||||
return tokens.removeAtIndex(0)
|
return tokens.remove(at: 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
public func prependToken(token:Token) {
|
public func prependToken(_ token:Token) {
|
||||||
tokens.insert(token, atIndex: 0)
|
tokens.insert(token, at: 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
public func findFilter(name: String) throws -> Filter {
|
func findTag(name: String) throws -> Extension.TagParser {
|
||||||
if let filter = namespace.filters[name] {
|
for ext in environment.extensions {
|
||||||
return filter
|
if let filter = ext.tags[name] {
|
||||||
|
return filter
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
throw TemplateSyntaxError("Invalid filter '\(name)'")
|
throw TemplateSyntaxError("Unknown template tag '\(name)'")
|
||||||
}
|
}
|
||||||
|
|
||||||
func compileFilter(token: String) throws -> Resolvable {
|
func findFilter(_ name: String) throws -> FilterType {
|
||||||
|
for ext in environment.extensions {
|
||||||
|
if let filter = ext.filters[name] {
|
||||||
|
return filter
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
throw TemplateSyntaxError("Unknown filter '\(name)'")
|
||||||
|
}
|
||||||
|
|
||||||
|
public func compileFilter(_ token: String) throws -> Resolvable {
|
||||||
return try FilterExpression(token: token, parser: self)
|
return try FilterExpression(token: token, parser: self)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,13 +6,27 @@ let NSFileNoSuchFileError = 4
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
/// A class representing a template
|
/// A class representing a template
|
||||||
public class Template {
|
open class Template: ExpressibleByStringLiteral {
|
||||||
|
let environment: Environment
|
||||||
let tokens: [Token]
|
let tokens: [Token]
|
||||||
|
|
||||||
|
/// The name of the loaded Template if the Template was loaded from a Loader
|
||||||
|
public let name: String?
|
||||||
|
|
||||||
|
/// Create a template with a template string
|
||||||
|
public required init(templateString: String, environment: Environment? = nil, name: String? = nil) {
|
||||||
|
self.environment = environment ?? Environment()
|
||||||
|
self.name = name
|
||||||
|
|
||||||
|
let lexer = Lexer(templateString: templateString)
|
||||||
|
tokens = lexer.tokenize()
|
||||||
|
}
|
||||||
|
|
||||||
/// Create a template with the given name inside the given bundle
|
/// Create a template with the given name inside the given bundle
|
||||||
public convenience init(named:String, inBundle bundle:NSBundle? = nil) throws {
|
@available(*, deprecated, message: "Use Environment/FileSystemLoader instead")
|
||||||
let useBundle = bundle ?? NSBundle.mainBundle()
|
public convenience init(named:String, inBundle bundle:Bundle? = nil) throws {
|
||||||
guard let url = useBundle.URLForResource(named, withExtension: nil) else {
|
let useBundle = bundle ?? Bundle.main
|
||||||
|
guard let url = useBundle.url(forResource: named, withExtension: nil) else {
|
||||||
throw NSError(domain: NSCocoaErrorDomain, code: NSFileNoSuchFileError, userInfo: nil)
|
throw NSError(domain: NSCocoaErrorDomain, code: NSFileNoSuchFileError, userInfo: nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -20,26 +34,44 @@ public class Template {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Create a template with a file found at the given URL
|
/// Create a template with a file found at the given URL
|
||||||
public convenience init(URL:NSURL) throws {
|
@available(*, deprecated, message: "Use Environment/FileSystemLoader instead")
|
||||||
try self.init(path: Path(URL.path!))
|
public convenience init(URL:Foundation.URL) throws {
|
||||||
|
try self.init(path: Path(URL.path))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Create a template with a file found at the given path
|
/// Create a template with a file found at the given path
|
||||||
public convenience init(path:Path) throws {
|
@available(*, deprecated, message: "Use Environment/FileSystemLoader instead")
|
||||||
self.init(templateString: try path.read())
|
public convenience init(path: Path, environment: Environment? = nil, name: String? = nil) throws {
|
||||||
|
self.init(templateString: try path.read(), environment: environment, name: name)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Create a template with a template string
|
// MARK: ExpressibleByStringLiteral
|
||||||
public init(templateString:String) {
|
|
||||||
let lexer = Lexer(templateString: templateString)
|
// Create a templaVte with a template string literal
|
||||||
tokens = lexer.tokenize()
|
public convenience required init(stringLiteral value: String) {
|
||||||
|
self.init(templateString: value)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Render the given template
|
// Create a template with a template string literal
|
||||||
public func render(context: Context? = nil) throws -> String {
|
public convenience required init(extendedGraphemeClusterLiteral value: StringLiteralType) {
|
||||||
let context = context ?? Context()
|
self.init(stringLiteral: value)
|
||||||
let parser = TokenParser(tokens: tokens, namespace: context.namespace)
|
}
|
||||||
|
|
||||||
|
// Create a template with a template string literal
|
||||||
|
public convenience required init(unicodeScalarLiteral value: StringLiteralType) {
|
||||||
|
self.init(stringLiteral: value)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Render the given template with a context
|
||||||
|
func render(_ context: Context) throws -> String {
|
||||||
|
let context = context
|
||||||
|
let parser = TokenParser(tokens: tokens, environment: context.environment)
|
||||||
let nodes = try parser.parse()
|
let nodes = try parser.parse()
|
||||||
return try renderNodes(nodes, context)
|
return try renderNodes(nodes, context)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Render the given template
|
||||||
|
open func render(_ dictionary: [String: Any]? = nil) throws -> String {
|
||||||
|
return try render(Context(dictionary: dictionary, environment: environment))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,38 +0,0 @@
|
|||||||
import Foundation
|
|
||||||
import PathKit
|
|
||||||
|
|
||||||
|
|
||||||
// A class for loading a template from disk
|
|
||||||
public class TemplateLoader {
|
|
||||||
public let paths: [Path]
|
|
||||||
|
|
||||||
public init(paths: [Path]) {
|
|
||||||
self.paths = paths
|
|
||||||
}
|
|
||||||
|
|
||||||
public init(bundle: [NSBundle]) {
|
|
||||||
self.paths = bundle.map {
|
|
||||||
return Path($0.bundlePath)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public func loadTemplate(templateName: String) -> Template? {
|
|
||||||
return loadTemplate([templateName])
|
|
||||||
}
|
|
||||||
|
|
||||||
public func loadTemplate(templateNames: [String]) -> Template? {
|
|
||||||
for path in paths {
|
|
||||||
for templateName in templateNames {
|
|
||||||
let templatePath = path + Path(templateName)
|
|
||||||
|
|
||||||
if templatePath.exists {
|
|
||||||
if let template = try? Template(path: templatePath) {
|
|
||||||
return template
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,76 +1,82 @@
|
|||||||
import Foundation
|
import Foundation
|
||||||
|
|
||||||
|
|
||||||
/// Split a string by spaces leaving quoted phrases together
|
extension String {
|
||||||
func smartSplit(value: String) -> [String] {
|
/// Split a string by a separator leaving quoted phrases together
|
||||||
var word = ""
|
func smartSplit(separator: Character = " ") -> [String] {
|
||||||
var separator: Character = " "
|
var word = ""
|
||||||
var components: [String] = []
|
var components: [String] = []
|
||||||
|
var separate: Character = separator
|
||||||
|
var singleQuoteCount = 0
|
||||||
|
var doubleQuoteCount = 0
|
||||||
|
|
||||||
for character in value.characters {
|
for character in self.characters {
|
||||||
if character == separator {
|
if character == "'" { singleQuoteCount += 1 }
|
||||||
if separator != " " {
|
else if character == "\"" { doubleQuoteCount += 1 }
|
||||||
word.append(separator)
|
|
||||||
}
|
|
||||||
|
|
||||||
if !word.isEmpty {
|
if character == separate {
|
||||||
components.append(word)
|
|
||||||
word = ""
|
|
||||||
}
|
|
||||||
|
|
||||||
separator = " "
|
if separate != separator {
|
||||||
} else {
|
word.append(separate)
|
||||||
if separator == " " && (character == "'" || character == "\"") {
|
} else if singleQuoteCount % 2 == 0 && doubleQuoteCount % 2 == 0 && !word.isEmpty {
|
||||||
separator = character
|
components.append(word)
|
||||||
|
word = ""
|
||||||
|
}
|
||||||
|
|
||||||
|
separate = separator
|
||||||
|
} else {
|
||||||
|
if separate == separator && (character == "'" || character == "\"") {
|
||||||
|
separate = character
|
||||||
|
}
|
||||||
|
word.append(character)
|
||||||
}
|
}
|
||||||
word.append(character)
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if !word.isEmpty {
|
if !word.isEmpty {
|
||||||
components.append(word)
|
components.append(word)
|
||||||
}
|
}
|
||||||
|
|
||||||
return components
|
return components
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public enum Token : Equatable {
|
public enum Token : Equatable {
|
||||||
/// A token representing a piece of text.
|
/// A token representing a piece of text.
|
||||||
case Text(value: String)
|
case text(value: String)
|
||||||
|
|
||||||
/// A token representing a variable.
|
/// A token representing a variable.
|
||||||
case Variable(value: String)
|
case variable(value: String)
|
||||||
|
|
||||||
/// A token representing a comment.
|
/// A token representing a comment.
|
||||||
case Comment(value: String)
|
case comment(value: String)
|
||||||
|
|
||||||
/// A token representing a template block.
|
/// A token representing a template block.
|
||||||
case Block(value: String)
|
case block(value: String)
|
||||||
|
|
||||||
/// Returns the underlying value as an array seperated by spaces
|
/// Returns the underlying value as an array seperated by spaces
|
||||||
public func components() -> [String] {
|
public func components() -> [String] {
|
||||||
switch self {
|
switch self {
|
||||||
case .Block(let value):
|
case .block(let value):
|
||||||
return smartSplit(value)
|
return value.smartSplit()
|
||||||
case .Variable(let value):
|
case .variable(let value):
|
||||||
return smartSplit(value)
|
return value.smartSplit()
|
||||||
case .Text(let value):
|
case .text(let value):
|
||||||
return smartSplit(value)
|
return value.smartSplit()
|
||||||
case .Comment(let value):
|
case .comment(let value):
|
||||||
return smartSplit(value)
|
return value.smartSplit()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public var contents: String {
|
public var contents: String {
|
||||||
switch self {
|
switch self {
|
||||||
case .Block(let value):
|
case .block(let value):
|
||||||
return value
|
return value
|
||||||
case .Variable(let value):
|
case .variable(let value):
|
||||||
return value
|
return value
|
||||||
case .Text(let value):
|
case .text(let value):
|
||||||
return value
|
return value
|
||||||
case .Comment(let value):
|
case .comment(let value):
|
||||||
return value
|
return value
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -79,13 +85,13 @@ public enum Token : Equatable {
|
|||||||
|
|
||||||
public func == (lhs: Token, rhs: Token) -> Bool {
|
public func == (lhs: Token, rhs: Token) -> Bool {
|
||||||
switch (lhs, rhs) {
|
switch (lhs, rhs) {
|
||||||
case (.Text(let lhsValue), .Text(let rhsValue)):
|
case (.text(let lhsValue), .text(let rhsValue)):
|
||||||
return lhsValue == rhsValue
|
return lhsValue == rhsValue
|
||||||
case (.Variable(let lhsValue), .Variable(let rhsValue)):
|
case (.variable(let lhsValue), .variable(let rhsValue)):
|
||||||
return lhsValue == rhsValue
|
return lhsValue == rhsValue
|
||||||
case (.Block(let lhsValue), .Block(let rhsValue)):
|
case (.block(let lhsValue), .block(let rhsValue)):
|
||||||
return lhsValue == rhsValue
|
return lhsValue == rhsValue
|
||||||
case (.Comment(let lhsValue), .Comment(let rhsValue)):
|
case (.comment(let lhsValue), .comment(let rhsValue)):
|
||||||
return lhsValue == rhsValue
|
return lhsValue == rhsValue
|
||||||
default:
|
default:
|
||||||
return false
|
return false
|
||||||
|
|||||||
@@ -1,12 +1,15 @@
|
|||||||
import Foundation
|
import Foundation
|
||||||
|
|
||||||
|
|
||||||
|
typealias Number = Float
|
||||||
|
|
||||||
|
|
||||||
class FilterExpression : Resolvable {
|
class FilterExpression : Resolvable {
|
||||||
let filters: [Filter]
|
let filters: [(FilterType, [Variable])]
|
||||||
let variable: Variable
|
let variable: Variable
|
||||||
|
|
||||||
init(token: String, parser: TokenParser) throws {
|
init(token: String, parser: TokenParser) throws {
|
||||||
let bits = token.characters.split("|").map({ String($0).trim(" ") })
|
let bits = token.characters.split(separator: "|").map({ String($0).trim(character: " ") })
|
||||||
if bits.isEmpty {
|
if bits.isEmpty {
|
||||||
filters = []
|
filters = []
|
||||||
variable = Variable("")
|
variable = Variable("")
|
||||||
@@ -14,21 +17,26 @@ class FilterExpression : Resolvable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
variable = Variable(bits[0])
|
variable = Variable(bits[0])
|
||||||
let filterBits = bits[1 ..< bits.endIndex]
|
let filterBits = bits[bits.indices.suffix(from: 1)]
|
||||||
|
|
||||||
do {
|
do {
|
||||||
filters = try filterBits.map { try parser.findFilter($0) }
|
filters = try filterBits.map {
|
||||||
|
let (name, arguments) = parseFilterComponents(token: $0)
|
||||||
|
let filter = try parser.findFilter(name)
|
||||||
|
return (filter, arguments)
|
||||||
|
}
|
||||||
} catch {
|
} catch {
|
||||||
filters = []
|
filters = []
|
||||||
throw error
|
throw error
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func resolve(context: Context) throws -> Any? {
|
func resolve(_ context: Context) throws -> Any? {
|
||||||
let result = try variable.resolve(context)
|
let result = try variable.resolve(context)
|
||||||
|
|
||||||
return try filters.reduce(result) { x, y in
|
return try filters.reduce(result) { x, y in
|
||||||
return try y(x)
|
let arguments = try y.1.map { try $0.resolve(context) }
|
||||||
|
return try y.0.invoke(value: x, arguments: arguments)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -42,17 +50,22 @@ public struct Variable : Equatable, Resolvable {
|
|||||||
self.variable = variable
|
self.variable = variable
|
||||||
}
|
}
|
||||||
|
|
||||||
private func lookup() -> [String] {
|
fileprivate func lookup() -> [String] {
|
||||||
return variable.characters.split(".").map(String.init)
|
return variable.characters.split(separator: ".").map(String.init)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Resolve the variable in the given context
|
/// Resolve the variable in the given context
|
||||||
public func resolve(context: Context) throws -> Any? {
|
public func resolve(_ context: Context) throws -> Any? {
|
||||||
var current: Any? = context
|
var current: Any? = context
|
||||||
|
|
||||||
if (variable.hasPrefix("'") && variable.hasSuffix("'")) || (variable.hasPrefix("\"") && variable.hasSuffix("\"")) {
|
if (variable.hasPrefix("'") && variable.hasSuffix("'")) || (variable.hasPrefix("\"") && variable.hasSuffix("\"")) {
|
||||||
// String literal
|
// String literal
|
||||||
return variable[variable.startIndex.successor() ..< variable.endIndex.predecessor()]
|
return variable[variable.characters.index(after: variable.startIndex) ..< variable.characters.index(before: variable.endIndex)]
|
||||||
|
}
|
||||||
|
|
||||||
|
if let number = Number(variable) {
|
||||||
|
// Number literal
|
||||||
|
return number
|
||||||
}
|
}
|
||||||
|
|
||||||
for bit in lookup() {
|
for bit in lookup() {
|
||||||
@@ -64,7 +77,11 @@ public struct Variable : Equatable, Resolvable {
|
|||||||
current = dictionary[bit]
|
current = dictionary[bit]
|
||||||
} else if let array = current as? [Any] {
|
} else if let array = current as? [Any] {
|
||||||
if let index = Int(bit) {
|
if let index = Int(bit) {
|
||||||
current = array[index]
|
if index >= 0 && index < array.count {
|
||||||
|
current = array[index]
|
||||||
|
} else {
|
||||||
|
current = nil
|
||||||
|
}
|
||||||
} else if bit == "first" {
|
} else if bit == "first" {
|
||||||
current = array.first
|
current = array.first
|
||||||
} else if bit == "last" {
|
} else if bit == "last" {
|
||||||
@@ -76,13 +93,26 @@ public struct Variable : Equatable, Resolvable {
|
|||||||
#if os(Linux)
|
#if os(Linux)
|
||||||
return nil
|
return nil
|
||||||
#else
|
#else
|
||||||
current = object.valueForKey(bit)
|
current = object.value(forKey: bit)
|
||||||
#endif
|
#endif
|
||||||
|
} else if let value = current {
|
||||||
|
let mirror = Mirror(reflecting: value)
|
||||||
|
current = mirror.descendant(bit)
|
||||||
|
|
||||||
|
if current == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if let resolvable = current as? Resolvable {
|
||||||
|
current = try resolvable.resolve(context)
|
||||||
|
} else if let node = current as? NodeType {
|
||||||
|
current = try node.render(context)
|
||||||
|
}
|
||||||
|
|
||||||
return normalize(current)
|
return normalize(current)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -92,7 +122,7 @@ public func ==(lhs: Variable, rhs: Variable) -> Bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func normalize(current: Any?) -> Any? {
|
func normalize(_ current: Any?) -> Any? {
|
||||||
if let current = current as? Normalizable {
|
if let current = current as? Normalizable {
|
||||||
return current.normalize()
|
return current.normalize()
|
||||||
}
|
}
|
||||||
@@ -131,3 +161,13 @@ extension Dictionary : Normalizable {
|
|||||||
return dictionary
|
return dictionary
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func parseFilterComponents(token: String) -> (String, [Variable]) {
|
||||||
|
var components = token.smartSplit(separator: ":")
|
||||||
|
let name = components.removeFirst()
|
||||||
|
let variables = components
|
||||||
|
.joined(separator: ":")
|
||||||
|
.smartSplit(separator: ",")
|
||||||
|
.map { Variable($0) }
|
||||||
|
return (name, variables)
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
{
|
{
|
||||||
"name": "Stencil",
|
"name": "Stencil",
|
||||||
"version": "0.6.0-beta.1",
|
"version": "0.9.0",
|
||||||
"summary": "Stencil is a simple and powerful template language for Swift.",
|
"summary": "Stencil is a simple and powerful template language for Swift.",
|
||||||
"homepage": "https://github.com/kylef/Stencil",
|
"homepage": "https://stencil.fuller.li",
|
||||||
"license": {
|
"license": {
|
||||||
"type": "BSD",
|
"type": "BSD",
|
||||||
"file": "LICENSE"
|
"file": "LICENSE"
|
||||||
@@ -10,20 +10,21 @@
|
|||||||
"authors": {
|
"authors": {
|
||||||
"Kyle Fuller": "kyle@fuller.li"
|
"Kyle Fuller": "kyle@fuller.li"
|
||||||
},
|
},
|
||||||
"social_media_url": "http://twitter.com/kylefuller",
|
"social_media_url": "https://twitter.com/kylefuller",
|
||||||
"source": {
|
"source": {
|
||||||
"git": "https://github.com/kylef/Stencil.git",
|
"git": "https://github.com/kylef/Stencil.git",
|
||||||
"tag": "0.6.0-beta.1"
|
"tag": "0.9.0"
|
||||||
},
|
},
|
||||||
"source_files": [
|
"source_files": [
|
||||||
"Sources/*.swift"
|
"Sources/*.swift"
|
||||||
],
|
],
|
||||||
"platforms": {
|
"platforms": {
|
||||||
"ios": "8.0",
|
"ios": "8.0",
|
||||||
"osx": "10.9"
|
"osx": "10.9",
|
||||||
|
"tvos": "9.0"
|
||||||
},
|
},
|
||||||
"requires_arc": true,
|
"requires_arc": true,
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"PathKit": [ "~> 0.6.0" ]
|
"PathKit": [ "~> 0.8.0" ]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,70 +0,0 @@
|
|||||||
import Spectre
|
|
||||||
import Stencil
|
|
||||||
|
|
||||||
|
|
||||||
func testFilter() {
|
|
||||||
describe("template filters") {
|
|
||||||
let context: [String: Any] = ["name": "Kyle"]
|
|
||||||
|
|
||||||
$0.it("allows you to register a custom filter") {
|
|
||||||
let template = Template(templateString: "{{ name|repeat }}")
|
|
||||||
|
|
||||||
let namespace = Namespace()
|
|
||||||
namespace.registerFilter("repeat") { value in
|
|
||||||
if let value = value as? String {
|
|
||||||
return "\(value) \(value)"
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
let result = try template.render(Context(dictionary: context, namespace: namespace))
|
|
||||||
try expect(result) == "Kyle Kyle"
|
|
||||||
}
|
|
||||||
|
|
||||||
$0.it("allows you to register a custom which throws") {
|
|
||||||
let template = Template(templateString: "{{ name|repeat }}")
|
|
||||||
let namespace = Namespace()
|
|
||||||
namespace.registerFilter("repeat") { value in
|
|
||||||
throw TemplateSyntaxError("No Repeat")
|
|
||||||
}
|
|
||||||
|
|
||||||
try expect(try template.render(Context(dictionary: context, namespace: namespace))).toThrow(TemplateSyntaxError("No Repeat"))
|
|
||||||
}
|
|
||||||
|
|
||||||
$0.it("allows whitespace in expression") {
|
|
||||||
let template = Template(templateString: "{{ name | uppercase }}")
|
|
||||||
let result = try template.render(Context(dictionary: ["name": "kyle"]))
|
|
||||||
try expect(result) == "KYLE"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
describe("capitalize filter") {
|
|
||||||
let template = Template(templateString: "{{ name|capitalize }}")
|
|
||||||
|
|
||||||
$0.it("capitalizes a string") {
|
|
||||||
let result = try template.render(Context(dictionary: ["name": "kyle"]))
|
|
||||||
try expect(result) == "Kyle"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
describe("uppercase filter") {
|
|
||||||
let template = Template(templateString: "{{ name|uppercase }}")
|
|
||||||
|
|
||||||
$0.it("transforms a string to be uppercase") {
|
|
||||||
let result = try template.render(Context(dictionary: ["name": "kyle"]))
|
|
||||||
try expect(result) == "KYLE"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
describe("lowercase filter") {
|
|
||||||
let template = Template(templateString: "{{ name|lowercase }}")
|
|
||||||
|
|
||||||
$0.it("transforms a string to be lowercase") {
|
|
||||||
let result = try template.render(Context(dictionary: ["name": "Kyle"]))
|
|
||||||
try expect(result) == "kyle"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
3
Tests/LinuxMain.swift
Normal file
3
Tests/LinuxMain.swift
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
import StencilTests
|
||||||
|
|
||||||
|
stencilTests()
|
||||||
@@ -1,66 +0,0 @@
|
|||||||
import Spectre
|
|
||||||
import Stencil
|
|
||||||
import Foundation
|
|
||||||
|
|
||||||
|
|
||||||
func testForNode() {
|
|
||||||
describe("ForNode") {
|
|
||||||
let context = Context(dictionary: [
|
|
||||||
"items": [1, 2, 3],
|
|
||||||
"emptyItems": [Int](),
|
|
||||||
])
|
|
||||||
|
|
||||||
$0.it("renders the given nodes for each item") {
|
|
||||||
let nodes: [NodeType] = [VariableNode(variable: "item")]
|
|
||||||
let node = ForNode(variable: "items", loopVariable: "item", nodes: nodes, emptyNodes: [])
|
|
||||||
try expect(try node.render(context)) == "123"
|
|
||||||
}
|
|
||||||
|
|
||||||
$0.it("renders the given empty nodes when no items found item") {
|
|
||||||
let nodes: [NodeType] = [VariableNode(variable: "item")]
|
|
||||||
let emptyNodes: [NodeType] = [TextNode(text: "empty")]
|
|
||||||
let node = ForNode(variable: "emptyItems", loopVariable: "item", nodes: nodes, emptyNodes: emptyNodes)
|
|
||||||
try expect(try node.render(context)) == "empty"
|
|
||||||
}
|
|
||||||
|
|
||||||
$0.it("renders a context variable of type Array<Any>") {
|
|
||||||
let any_context = Context(dictionary: [
|
|
||||||
"items": ([1, 2, 3] as [Any])
|
|
||||||
])
|
|
||||||
|
|
||||||
let nodes: [NodeType] = [VariableNode(variable: "item")]
|
|
||||||
let node = ForNode(variable: "items", loopVariable: "item", nodes: nodes, emptyNodes: [])
|
|
||||||
try expect(try node.render(any_context)) == "123"
|
|
||||||
}
|
|
||||||
|
|
||||||
#if os(OSX)
|
|
||||||
$0.it("renders a context variable of type NSArray") {
|
|
||||||
let nsarray_context = Context(dictionary: [
|
|
||||||
"items": NSArray(array: [1, 2, 3])
|
|
||||||
])
|
|
||||||
|
|
||||||
let nodes: [NodeType] = [VariableNode(variable: "item")]
|
|
||||||
let node = ForNode(variable: "items", loopVariable: "item", nodes: nodes, emptyNodes: [])
|
|
||||||
try expect(try node.render(nsarray_context)) == "123"
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
$0.it("renders the given nodes while providing if the item is first in the context") {
|
|
||||||
let nodes: [NodeType] = [VariableNode(variable: "item"), VariableNode(variable: "forloop.first")]
|
|
||||||
let node = ForNode(variable: "items", loopVariable: "item", nodes: nodes, emptyNodes: [])
|
|
||||||
try expect(try node.render(context)) == "1true2false3false"
|
|
||||||
}
|
|
||||||
|
|
||||||
$0.it("renders the given nodes while providing if the item is last in the context") {
|
|
||||||
let nodes: [NodeType] = [VariableNode(variable: "item"), VariableNode(variable: "forloop.last")]
|
|
||||||
let node = ForNode(variable: "items", loopVariable: "item", nodes: nodes, emptyNodes: [])
|
|
||||||
try expect(try node.render(context)) == "1false2false3true"
|
|
||||||
}
|
|
||||||
|
|
||||||
$0.it("renders the given nodes while providing item counter") {
|
|
||||||
let nodes: [NodeType] = [VariableNode(variable: "item"), VariableNode(variable: "forloop.counter")]
|
|
||||||
let node = ForNode(variable: "items", loopVariable: "item", nodes: nodes, emptyNodes: [])
|
|
||||||
try expect(try node.render(context)) == "112233"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,116 +0,0 @@
|
|||||||
import Spectre
|
|
||||||
import Stencil
|
|
||||||
|
|
||||||
|
|
||||||
func testIfNode() {
|
|
||||||
describe("IfNode") {
|
|
||||||
$0.describe("parsing") {
|
|
||||||
$0.it("can parse an if block") {
|
|
||||||
let tokens = [
|
|
||||||
Token.Block(value: "if value"),
|
|
||||||
Token.Text(value: "true"),
|
|
||||||
Token.Block(value: "else"),
|
|
||||||
Token.Text(value: "false"),
|
|
||||||
Token.Block(value: "endif")
|
|
||||||
]
|
|
||||||
|
|
||||||
let parser = TokenParser(tokens: tokens, namespace: Namespace())
|
|
||||||
let nodes = try parser.parse()
|
|
||||||
let node = nodes.first as? IfNode
|
|
||||||
let trueNode = node?.trueNodes.first as? TextNode
|
|
||||||
let falseNode = node?.falseNodes.first as? TextNode
|
|
||||||
|
|
||||||
try expect(nodes.count) == 1
|
|
||||||
try expect(node?.variable.variable) == "value"
|
|
||||||
try expect(node?.trueNodes.count) == 1
|
|
||||||
try expect(trueNode?.text) == "true"
|
|
||||||
try expect(node?.falseNodes.count) == 1
|
|
||||||
try expect(falseNode?.text) == "false"
|
|
||||||
}
|
|
||||||
|
|
||||||
$0.it("can parse an ifnot block") {
|
|
||||||
let tokens = [
|
|
||||||
Token.Block(value: "ifnot value"),
|
|
||||||
Token.Text(value: "false"),
|
|
||||||
Token.Block(value: "else"),
|
|
||||||
Token.Text(value: "true"),
|
|
||||||
Token.Block(value: "endif")
|
|
||||||
]
|
|
||||||
|
|
||||||
let parser = TokenParser(tokens: tokens, namespace: Namespace())
|
|
||||||
let nodes = try parser.parse()
|
|
||||||
let node = nodes.first as? IfNode
|
|
||||||
let trueNode = node?.trueNodes.first as? TextNode
|
|
||||||
let falseNode = node?.falseNodes.first as? TextNode
|
|
||||||
|
|
||||||
try expect(nodes.count) == 1
|
|
||||||
try expect(node?.variable.variable) == "value"
|
|
||||||
try expect(node?.trueNodes.count) == 1
|
|
||||||
try expect(trueNode?.text) == "true"
|
|
||||||
try expect(node?.falseNodes.count) == 1
|
|
||||||
try expect(falseNode?.text) == "false"
|
|
||||||
}
|
|
||||||
|
|
||||||
$0.it("throws an error when parsing an if block without an endif") {
|
|
||||||
let tokens = [
|
|
||||||
Token.Block(value: "if value"),
|
|
||||||
]
|
|
||||||
|
|
||||||
let parser = TokenParser(tokens: tokens, namespace: Namespace())
|
|
||||||
let error = TemplateSyntaxError("`endif` was not found.")
|
|
||||||
try expect(try parser.parse()).toThrow(error)
|
|
||||||
}
|
|
||||||
|
|
||||||
$0.it("throws an error when parsing an ifnot without an endif") {
|
|
||||||
let tokens = [
|
|
||||||
Token.Block(value: "ifnot value"),
|
|
||||||
]
|
|
||||||
|
|
||||||
let parser = TokenParser(tokens: tokens, namespace: Namespace())
|
|
||||||
let error = TemplateSyntaxError("`endif` was not found.")
|
|
||||||
try expect(try parser.parse()).toThrow(error)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
$0.describe("rendering") {
|
|
||||||
let context = Context(dictionary: ["items": true])
|
|
||||||
|
|
||||||
$0.it("renders the truth when expression evaluates to true") {
|
|
||||||
let node = IfNode(variable: "items", trueNodes: [TextNode(text: "true")], falseNodes: [TextNode(text: "false")])
|
|
||||||
try expect(try node.render(context)) == "true"
|
|
||||||
}
|
|
||||||
|
|
||||||
$0.it("renders the false when expression evaluates to false") {
|
|
||||||
let node = IfNode(variable: "unknown", trueNodes: [TextNode(text: "true")], falseNodes: [TextNode(text: "false")])
|
|
||||||
try expect(try node.render(context)) == "false"
|
|
||||||
}
|
|
||||||
|
|
||||||
$0.it("renders the truth when array expression is not empty") {
|
|
||||||
let items: [[String: Any]] = [["key":"key1","value":42],["key":"key2","value":1337]]
|
|
||||||
let arrayContext = Context(dictionary: ["items": [items]])
|
|
||||||
let node = IfNode(variable: "items", trueNodes: [TextNode(text: "true")], falseNodes: [TextNode(text: "false")])
|
|
||||||
try expect(try node.render(arrayContext)) == "true"
|
|
||||||
}
|
|
||||||
|
|
||||||
$0.it("renders the false when array expression is empty") {
|
|
||||||
let emptyItems = [[String: Any]]()
|
|
||||||
let arrayContext = Context(dictionary: ["items": emptyItems])
|
|
||||||
let node = IfNode(variable: "items", trueNodes: [TextNode(text: "true")], falseNodes: [TextNode(text: "false")])
|
|
||||||
try expect(try node.render(arrayContext)) == "false"
|
|
||||||
}
|
|
||||||
|
|
||||||
$0.it("renders the false when dictionary expression is empty") {
|
|
||||||
let emptyItems = [String:Any]()
|
|
||||||
let arrayContext = Context(dictionary: ["items": emptyItems])
|
|
||||||
let node = IfNode(variable: "items", trueNodes: [TextNode(text: "true")], falseNodes: [TextNode(text: "false")])
|
|
||||||
try expect(try node.render(arrayContext)) == "false"
|
|
||||||
}
|
|
||||||
|
|
||||||
$0.it("renders the false when Array<Any> variable is empty") {
|
|
||||||
let arrayContext = Context(dictionary: ["items": ([] as [Any])])
|
|
||||||
let node = IfNode(variable: "items", trueNodes: [TextNode(text: "true")], falseNodes: [TextNode(text: "false")])
|
|
||||||
try expect(try node.render(arrayContext)) == "false"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
import Spectre
|
import Spectre
|
||||||
import Stencil
|
@testable import Stencil
|
||||||
|
|
||||||
|
|
||||||
func testContext() {
|
func testContext() {
|
||||||
@@ -47,10 +47,19 @@ func testContext() {
|
|||||||
try expect(context["name"] as? String) == "Kyle"
|
try expect(context["name"] as? String) == "Kyle"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$0.it("allows you to remove a parent's value in a level") {
|
||||||
|
try context.push {
|
||||||
|
context["name"] = nil
|
||||||
|
try expect(context["name"]).to.beNil()
|
||||||
|
}
|
||||||
|
|
||||||
|
try expect(context["name"] as? String) == "Kyle"
|
||||||
|
}
|
||||||
|
|
||||||
$0.it("allows you to push a dictionary and run a closure then restoring previous state") {
|
$0.it("allows you to push a dictionary and run a closure then restoring previous state") {
|
||||||
var didRun = false
|
var didRun = false
|
||||||
|
|
||||||
try context.push(["name": "Katie"]) {
|
try context.push(dictionary: ["name": "Katie"]) {
|
||||||
didRun = true
|
didRun = true
|
||||||
try expect(context["name"] as? String) == "Katie"
|
try expect(context["name"] as? String) == "Katie"
|
||||||
}
|
}
|
||||||
@@ -58,5 +67,15 @@ func testContext() {
|
|||||||
try expect(didRun).to.beTrue()
|
try expect(didRun).to.beTrue()
|
||||||
try expect(context["name"] as? String) == "Kyle"
|
try expect(context["name"] as? String) == "Kyle"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$0.it("allows you to flatten the context contents") {
|
||||||
|
try context.push(dictionary: ["test": "abc"]) {
|
||||||
|
let flattened = context.flatten()
|
||||||
|
|
||||||
|
try expect(flattened.count) == 2
|
||||||
|
try expect(flattened["name"] as? String) == "Kyle"
|
||||||
|
try expect(flattened["test"] as? String) == "abc"
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
54
Tests/StencilTests/EnvironmentSpec.swift
Normal file
54
Tests/StencilTests/EnvironmentSpec.swift
Normal file
@@ -0,0 +1,54 @@
|
|||||||
|
import Spectre
|
||||||
|
import Stencil
|
||||||
|
|
||||||
|
|
||||||
|
func testEnvironment() {
|
||||||
|
describe("Environment") {
|
||||||
|
let environment = Environment(loader: ExampleLoader())
|
||||||
|
|
||||||
|
$0.it("can load a template from a name") {
|
||||||
|
let template = try environment.loadTemplate(name: "example.html")
|
||||||
|
try expect(template.name) == "example.html"
|
||||||
|
}
|
||||||
|
|
||||||
|
$0.it("can load a template from a names") {
|
||||||
|
let template = try environment.loadTemplate(names: ["first.html", "example.html"])
|
||||||
|
try expect(template.name) == "example.html"
|
||||||
|
}
|
||||||
|
|
||||||
|
$0.it("can render a template from a string") {
|
||||||
|
let result = try environment.renderTemplate(string: "Hello World")
|
||||||
|
try expect(result) == "Hello World"
|
||||||
|
}
|
||||||
|
|
||||||
|
$0.it("can render a template from a file") {
|
||||||
|
let result = try environment.renderTemplate(name: "example.html")
|
||||||
|
try expect(result) == "Hello World!"
|
||||||
|
}
|
||||||
|
|
||||||
|
$0.it("allows you to provide a custom template class") {
|
||||||
|
let environment = Environment(loader: ExampleLoader(), templateClass: CustomTemplate.self)
|
||||||
|
let result = try environment.renderTemplate(string: "Hello World")
|
||||||
|
|
||||||
|
try expect(result) == "here"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
fileprivate class ExampleLoader: Loader {
|
||||||
|
func loadTemplate(name: String, environment: Environment) throws -> Template {
|
||||||
|
if name == "example.html" {
|
||||||
|
return Template(templateString: "Hello World!", environment: environment, name: name)
|
||||||
|
}
|
||||||
|
|
||||||
|
throw TemplateDoesNotExist(templateNames: [name], loader: self)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
class CustomTemplate: Template {
|
||||||
|
override func render(_ dictionary: [String: Any]? = nil) throws -> String {
|
||||||
|
return "here"
|
||||||
|
}
|
||||||
|
}
|
||||||
284
Tests/StencilTests/ExpressionSpec.swift
Normal file
284
Tests/StencilTests/ExpressionSpec.swift
Normal file
@@ -0,0 +1,284 @@
|
|||||||
|
import Spectre
|
||||||
|
@testable import Stencil
|
||||||
|
|
||||||
|
|
||||||
|
func testExpressions() {
|
||||||
|
describe("Expression") {
|
||||||
|
let parser = TokenParser(tokens: [], environment: Environment())
|
||||||
|
|
||||||
|
$0.describe("VariableExpression") {
|
||||||
|
let expression = VariableExpression(variable: Variable("value"))
|
||||||
|
|
||||||
|
$0.it("evaluates to true when value is not nil") {
|
||||||
|
let context = Context(dictionary: ["value": "known"])
|
||||||
|
try expect(try expression.evaluate(context: context)).to.beTrue()
|
||||||
|
}
|
||||||
|
|
||||||
|
$0.it("evaluates to false when value is unset") {
|
||||||
|
let context = Context()
|
||||||
|
try expect(try expression.evaluate(context: context)).to.beFalse()
|
||||||
|
}
|
||||||
|
|
||||||
|
$0.it("evaluates to true when array variable is not empty") {
|
||||||
|
let items: [[String: Any]] = [["key":"key1","value":42],["key":"key2","value":1337]]
|
||||||
|
let context = Context(dictionary: ["value": [items]])
|
||||||
|
try expect(try expression.evaluate(context: context)).to.beTrue()
|
||||||
|
}
|
||||||
|
|
||||||
|
$0.it("evaluates to false when array value is empty") {
|
||||||
|
let emptyItems = [[String: Any]]()
|
||||||
|
let context = Context(dictionary: ["value": emptyItems])
|
||||||
|
try expect(try expression.evaluate(context: context)).to.beFalse()
|
||||||
|
}
|
||||||
|
|
||||||
|
$0.it("evaluates to false when dictionary value is empty") {
|
||||||
|
let emptyItems = [String:Any]()
|
||||||
|
let context = Context(dictionary: ["value": emptyItems])
|
||||||
|
try expect(try expression.evaluate(context: context)).to.beFalse()
|
||||||
|
}
|
||||||
|
|
||||||
|
$0.it("evaluates to false when Array<Any> value is empty") {
|
||||||
|
let context = Context(dictionary: ["value": ([] as [Any])])
|
||||||
|
try expect(try expression.evaluate(context: context)).to.beFalse()
|
||||||
|
}
|
||||||
|
|
||||||
|
$0.it("evaluates to true when integer value is above 0") {
|
||||||
|
let context = Context(dictionary: ["value": 1])
|
||||||
|
try expect(try expression.evaluate(context: context)).to.beTrue()
|
||||||
|
}
|
||||||
|
|
||||||
|
$0.it("evaluates to true with string") {
|
||||||
|
let context = Context(dictionary: ["value": "test"])
|
||||||
|
try expect(try expression.evaluate(context: context)).to.beTrue()
|
||||||
|
}
|
||||||
|
|
||||||
|
$0.it("evaluates to false when empty string") {
|
||||||
|
let context = Context(dictionary: ["value": ""])
|
||||||
|
try expect(try expression.evaluate(context: context)).to.beFalse()
|
||||||
|
}
|
||||||
|
|
||||||
|
$0.it("evaluates to false when integer value is below 0 or below") {
|
||||||
|
let context = Context(dictionary: ["value": 0])
|
||||||
|
try expect(try expression.evaluate(context: context)).to.beFalse()
|
||||||
|
|
||||||
|
let negativeContext = Context(dictionary: ["value": 0])
|
||||||
|
try expect(try expression.evaluate(context: negativeContext)).to.beFalse()
|
||||||
|
}
|
||||||
|
|
||||||
|
$0.it("evaluates to true when float value is above 0") {
|
||||||
|
let context = Context(dictionary: ["value": Float(0.5)])
|
||||||
|
try expect(try expression.evaluate(context: context)).to.beTrue()
|
||||||
|
}
|
||||||
|
|
||||||
|
$0.it("evaluates to false when float is 0 or below") {
|
||||||
|
let context = Context(dictionary: ["value": Float(0)])
|
||||||
|
try expect(try expression.evaluate(context: context)).to.beFalse()
|
||||||
|
}
|
||||||
|
|
||||||
|
$0.it("evaluates to true when double value is above 0") {
|
||||||
|
let context = Context(dictionary: ["value": Double(0.5)])
|
||||||
|
try expect(try expression.evaluate(context: context)).to.beTrue()
|
||||||
|
}
|
||||||
|
|
||||||
|
$0.it("evaluates to false when double is 0 or below") {
|
||||||
|
let context = Context(dictionary: ["value": Double(0)])
|
||||||
|
try expect(try expression.evaluate(context: context)).to.beFalse()
|
||||||
|
}
|
||||||
|
|
||||||
|
$0.it("evaluates to false when uint is 0") {
|
||||||
|
let context = Context(dictionary: ["value": UInt(0)])
|
||||||
|
try expect(try expression.evaluate(context: context)).to.beFalse()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$0.describe("NotExpression") {
|
||||||
|
$0.it("returns truthy for positive expressions") {
|
||||||
|
let expression = NotExpression(expression: StaticExpression(value: true))
|
||||||
|
try expect(expression.evaluate(context: Context())).to.beFalse()
|
||||||
|
}
|
||||||
|
|
||||||
|
$0.it("returns falsy for negative expressions") {
|
||||||
|
let expression = NotExpression(expression: StaticExpression(value: false))
|
||||||
|
try expect(expression.evaluate(context: Context())).to.beTrue()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$0.describe("expression parsing") {
|
||||||
|
$0.it("can parse a variable expression") {
|
||||||
|
let expression = try parseExpression(components: ["value"], tokenParser: parser)
|
||||||
|
try expect(expression.evaluate(context: Context())).to.beFalse()
|
||||||
|
try expect(expression.evaluate(context: Context(dictionary: ["value": true]))).to.beTrue()
|
||||||
|
}
|
||||||
|
|
||||||
|
$0.it("can parse a not expression") {
|
||||||
|
let expression = try parseExpression(components: ["not", "value"], tokenParser: parser)
|
||||||
|
try expect(expression.evaluate(context: Context())).to.beTrue()
|
||||||
|
try expect(expression.evaluate(context: Context(dictionary: ["value": true]))).to.beFalse()
|
||||||
|
}
|
||||||
|
|
||||||
|
$0.describe("and expression") {
|
||||||
|
let expression = try! parseExpression(components: ["lhs", "and", "rhs"], tokenParser: parser)
|
||||||
|
|
||||||
|
$0.it("evaluates to false with lhs false") {
|
||||||
|
try expect(expression.evaluate(context: Context(dictionary: ["lhs": false, "rhs": true]))).to.beFalse()
|
||||||
|
}
|
||||||
|
|
||||||
|
$0.it("evaluates to false with rhs false") {
|
||||||
|
try expect(expression.evaluate(context: Context(dictionary: ["lhs": true, "rhs": false]))).to.beFalse()
|
||||||
|
}
|
||||||
|
|
||||||
|
$0.it("evaluates to false with lhs and rhs false") {
|
||||||
|
try expect(expression.evaluate(context: Context(dictionary: ["lhs": false, "rhs": false]))).to.beFalse()
|
||||||
|
}
|
||||||
|
|
||||||
|
$0.it("evaluates to true with lhs and rhs true") {
|
||||||
|
try expect(expression.evaluate(context: Context(dictionary: ["lhs": true, "rhs": true]))).to.beTrue()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$0.describe("or expression") {
|
||||||
|
let expression = try! parseExpression(components: ["lhs", "or", "rhs"], tokenParser: parser)
|
||||||
|
|
||||||
|
$0.it("evaluates to true with lhs true") {
|
||||||
|
try expect(expression.evaluate(context: Context(dictionary: ["lhs": true, "rhs": false]))).to.beTrue()
|
||||||
|
}
|
||||||
|
|
||||||
|
$0.it("evaluates to true with rhs true") {
|
||||||
|
try expect(expression.evaluate(context: Context(dictionary: ["lhs": false, "rhs": true]))).to.beTrue()
|
||||||
|
}
|
||||||
|
|
||||||
|
$0.it("evaluates to true with lhs and rhs true") {
|
||||||
|
try expect(expression.evaluate(context: Context(dictionary: ["lhs": true, "rhs": true]))).to.beTrue()
|
||||||
|
}
|
||||||
|
|
||||||
|
$0.it("evaluates to false with lhs and rhs false") {
|
||||||
|
try expect(expression.evaluate(context: Context(dictionary: ["lhs": false, "rhs": false]))).to.beFalse()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$0.describe("equality expression") {
|
||||||
|
let expression = try! parseExpression(components: ["lhs", "==", "rhs"], tokenParser: parser)
|
||||||
|
|
||||||
|
$0.it("evaluates to true with equal lhs/rhs") {
|
||||||
|
try expect(expression.evaluate(context: Context(dictionary: ["lhs": "a", "rhs": "a"]))).to.beTrue()
|
||||||
|
}
|
||||||
|
|
||||||
|
$0.it("evaluates to false with non equal lhs/rhs") {
|
||||||
|
try expect(expression.evaluate(context: Context(dictionary: ["lhs": "a", "rhs": "b"]))).to.beFalse()
|
||||||
|
}
|
||||||
|
|
||||||
|
$0.it("evaluates to true with nils") {
|
||||||
|
try expect(expression.evaluate(context: Context(dictionary: [:]))).to.beTrue()
|
||||||
|
}
|
||||||
|
|
||||||
|
$0.it("evaluates to true with numbers") {
|
||||||
|
try expect(expression.evaluate(context: Context(dictionary: ["lhs": 1, "rhs": 1.0]))).to.beTrue()
|
||||||
|
}
|
||||||
|
|
||||||
|
$0.it("evaluates to false with non equal numbers") {
|
||||||
|
try expect(expression.evaluate(context: Context(dictionary: ["lhs": 1, "rhs": 1.1]))).to.beFalse()
|
||||||
|
}
|
||||||
|
|
||||||
|
$0.it("evaluates to true with booleans") {
|
||||||
|
try expect(expression.evaluate(context: Context(dictionary: ["lhs": true, "rhs": true]))).to.beTrue()
|
||||||
|
}
|
||||||
|
|
||||||
|
$0.it("evaluates to false with falsy booleans") {
|
||||||
|
try expect(expression.evaluate(context: Context(dictionary: ["lhs": true, "rhs": false]))).to.beFalse()
|
||||||
|
}
|
||||||
|
|
||||||
|
$0.it("evaluates to false with different types") {
|
||||||
|
try expect(expression.evaluate(context: Context(dictionary: ["lhs": true, "rhs": 1]))).to.beFalse()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$0.describe("inequality expression") {
|
||||||
|
let expression = try! parseExpression(components: ["lhs", "!=", "rhs"], tokenParser: parser)
|
||||||
|
|
||||||
|
$0.it("evaluates to true with inequal lhs/rhs") {
|
||||||
|
try expect(expression.evaluate(context: Context(dictionary: ["lhs": "a", "rhs": "b"]))).to.beTrue()
|
||||||
|
}
|
||||||
|
|
||||||
|
$0.it("evaluates to false with equal lhs/rhs") {
|
||||||
|
try expect(expression.evaluate(context: Context(dictionary: ["lhs": "b", "rhs": "b"]))).to.beFalse()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$0.describe("more than expression") {
|
||||||
|
let expression = try! parseExpression(components: ["lhs", ">", "rhs"], tokenParser: parser)
|
||||||
|
|
||||||
|
$0.it("evaluates to true with lhs > rhs") {
|
||||||
|
try expect(expression.evaluate(context: Context(dictionary: ["lhs": 5.0, "rhs": 4]))).to.beTrue()
|
||||||
|
}
|
||||||
|
|
||||||
|
$0.it("evaluates to false with lhs == rhs") {
|
||||||
|
try expect(expression.evaluate(context: Context(dictionary: ["lhs": 5.0, "rhs": 5.0]))).to.beFalse()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$0.describe("more than equal expression") {
|
||||||
|
let expression = try! parseExpression(components: ["lhs", ">=", "rhs"], tokenParser: parser)
|
||||||
|
|
||||||
|
$0.it("evaluates to true with lhs == rhs") {
|
||||||
|
try expect(expression.evaluate(context: Context(dictionary: ["lhs": 5.0, "rhs": 5]))).to.beTrue()
|
||||||
|
}
|
||||||
|
|
||||||
|
$0.it("evaluates to false with lhs < rhs") {
|
||||||
|
try expect(expression.evaluate(context: Context(dictionary: ["lhs": 5.0, "rhs": 5.1]))).to.beFalse()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$0.describe("less than expression") {
|
||||||
|
let expression = try! parseExpression(components: ["lhs", "<", "rhs"], tokenParser: parser)
|
||||||
|
|
||||||
|
$0.it("evaluates to true with lhs < rhs") {
|
||||||
|
try expect(expression.evaluate(context: Context(dictionary: ["lhs": 4, "rhs": 4.5]))).to.beTrue()
|
||||||
|
}
|
||||||
|
|
||||||
|
$0.it("evaluates to false with lhs == rhs") {
|
||||||
|
try expect(expression.evaluate(context: Context(dictionary: ["lhs": 5.0, "rhs": 5.0]))).to.beFalse()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$0.describe("less than equal expression") {
|
||||||
|
let expression = try! parseExpression(components: ["lhs", "<=", "rhs"], tokenParser: parser)
|
||||||
|
|
||||||
|
$0.it("evaluates to true with lhs == rhs") {
|
||||||
|
try expect(expression.evaluate(context: Context(dictionary: ["lhs": 5.0, "rhs": 5]))).to.beTrue()
|
||||||
|
}
|
||||||
|
|
||||||
|
$0.it("evaluates to false with lhs > rhs") {
|
||||||
|
try expect(expression.evaluate(context: Context(dictionary: ["lhs": 5.1, "rhs": 5.0]))).to.beFalse()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$0.describe("multiple expression") {
|
||||||
|
let expression = try! parseExpression(components: ["one", "or", "two", "and", "not", "three"], tokenParser: parser)
|
||||||
|
|
||||||
|
$0.it("evaluates to true with one") {
|
||||||
|
try expect(expression.evaluate(context: Context(dictionary: ["one": true]))).to.beTrue()
|
||||||
|
}
|
||||||
|
|
||||||
|
$0.it("evaluates to true with one and three") {
|
||||||
|
try expect(expression.evaluate(context: Context(dictionary: ["one": true, "three": true]))).to.beTrue()
|
||||||
|
}
|
||||||
|
|
||||||
|
$0.it("evaluates to true with two") {
|
||||||
|
try expect(expression.evaluate(context: Context(dictionary: ["two": true]))).to.beTrue()
|
||||||
|
}
|
||||||
|
|
||||||
|
$0.it("evaluates to false with two and three") {
|
||||||
|
try expect(expression.evaluate(context: Context(dictionary: ["two": true, "three": true]))).to.beFalse()
|
||||||
|
}
|
||||||
|
|
||||||
|
$0.it("evaluates to false with two and three") {
|
||||||
|
try expect(expression.evaluate(context: Context(dictionary: ["two": true, "three": true]))).to.beFalse()
|
||||||
|
}
|
||||||
|
|
||||||
|
$0.it("evaluates to false with nothing") {
|
||||||
|
try expect(expression.evaluate(context: Context())).to.beFalse()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
166
Tests/StencilTests/FilterSpec.swift
Normal file
166
Tests/StencilTests/FilterSpec.swift
Normal file
@@ -0,0 +1,166 @@
|
|||||||
|
import Spectre
|
||||||
|
@testable import Stencil
|
||||||
|
|
||||||
|
|
||||||
|
func testFilter() {
|
||||||
|
describe("template filters") {
|
||||||
|
let context: [String: Any] = ["name": "Kyle"]
|
||||||
|
|
||||||
|
$0.it("allows you to register a custom filter") {
|
||||||
|
let template = Template(templateString: "{{ name|repeat }}")
|
||||||
|
|
||||||
|
let repeatExtension = Extension()
|
||||||
|
repeatExtension.registerFilter("repeat") { (value: Any?) in
|
||||||
|
if let value = value as? String {
|
||||||
|
return "\(value) \(value)"
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
let result = try template.render(Context(dictionary: context, environment: Environment(extensions: [repeatExtension])))
|
||||||
|
try expect(result) == "Kyle Kyle"
|
||||||
|
}
|
||||||
|
|
||||||
|
$0.it("allows you to register a custom filter which accepts single argument") {
|
||||||
|
let template = Template(templateString: "{{ name|repeat:'value1, \"value2\"' }}")
|
||||||
|
|
||||||
|
let repeatExtension = Extension()
|
||||||
|
repeatExtension.registerFilter("repeat") { value, arguments in
|
||||||
|
if !arguments.isEmpty {
|
||||||
|
return "\(value!) \(value!) with args \(arguments.first!!)"
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
let result = try template.render(Context(dictionary: context, environment: Environment(extensions: [repeatExtension])))
|
||||||
|
try expect(result) == "Kyle Kyle with args value1, \"value2\""
|
||||||
|
}
|
||||||
|
|
||||||
|
$0.it("allows you to register a custom filter which accepts several arguments") {
|
||||||
|
let template = Template(templateString: "{{ name|repeat:'value\"1\"',\"value'2'\",'(key, value)' }}")
|
||||||
|
|
||||||
|
let repeatExtension = Extension()
|
||||||
|
repeatExtension.registerFilter("repeat") { value, arguments in
|
||||||
|
if !arguments.isEmpty {
|
||||||
|
return "\(value!) \(value!) with args 0: \(arguments[0]!), 1: \(arguments[1]!), 2: \(arguments[2]!)"
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
let result = try template.render(Context(dictionary: context, environment: Environment(extensions: [repeatExtension])))
|
||||||
|
try expect(result) == "Kyle Kyle with args 0: value\"1\", 1: value'2', 2: (key, value)"
|
||||||
|
}
|
||||||
|
|
||||||
|
$0.it("allows you to register a custom which throws") {
|
||||||
|
let template = Template(templateString: "{{ name|repeat }}")
|
||||||
|
let repeatExtension = Extension()
|
||||||
|
repeatExtension.registerFilter("repeat") { (value: Any?) in
|
||||||
|
throw TemplateSyntaxError("No Repeat")
|
||||||
|
}
|
||||||
|
|
||||||
|
let context = Context(dictionary: context, environment: Environment(extensions: [repeatExtension]))
|
||||||
|
try expect(try template.render(context)).toThrow(TemplateSyntaxError("No Repeat"))
|
||||||
|
}
|
||||||
|
|
||||||
|
$0.it("allows you to override a default filter") {
|
||||||
|
let template = Template(templateString: "{{ name|join }}")
|
||||||
|
|
||||||
|
let repeatExtension = Extension()
|
||||||
|
repeatExtension.registerFilter("join") { (value: Any?) in
|
||||||
|
return "joined"
|
||||||
|
}
|
||||||
|
|
||||||
|
let result = try template.render(Context(dictionary: context, environment: Environment(extensions: [repeatExtension])))
|
||||||
|
try expect(result) == "joined"
|
||||||
|
}
|
||||||
|
|
||||||
|
$0.it("allows whitespace in expression") {
|
||||||
|
let template = Template(templateString: "{{ name | uppercase }}")
|
||||||
|
let result = try template.render(Context(dictionary: ["name": "kyle"]))
|
||||||
|
try expect(result) == "KYLE"
|
||||||
|
}
|
||||||
|
|
||||||
|
$0.it("throws when you pass arguments to simple filter") {
|
||||||
|
let template = Template(templateString: "{{ name|uppercase:5 }}")
|
||||||
|
try expect(try template.render(Context(dictionary: ["name": "kyle"]))).toThrow()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
describe("capitalize filter") {
|
||||||
|
let template = Template(templateString: "{{ name|capitalize }}")
|
||||||
|
|
||||||
|
$0.it("capitalizes a string") {
|
||||||
|
let result = try template.render(Context(dictionary: ["name": "kyle"]))
|
||||||
|
try expect(result) == "Kyle"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
describe("uppercase filter") {
|
||||||
|
let template = Template(templateString: "{{ name|uppercase }}")
|
||||||
|
|
||||||
|
$0.it("transforms a string to be uppercase") {
|
||||||
|
let result = try template.render(Context(dictionary: ["name": "kyle"]))
|
||||||
|
try expect(result) == "KYLE"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
describe("lowercase filter") {
|
||||||
|
let template = Template(templateString: "{{ name|lowercase }}")
|
||||||
|
|
||||||
|
$0.it("transforms a string to be lowercase") {
|
||||||
|
let result = try template.render(Context(dictionary: ["name": "Kyle"]))
|
||||||
|
try expect(result) == "kyle"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
describe("default filter") {
|
||||||
|
let template = Template(templateString: "Hello {{ name|default:\"World\" }}")
|
||||||
|
|
||||||
|
$0.it("shows the variable value") {
|
||||||
|
let result = try template.render(Context(dictionary: ["name": "Kyle"]))
|
||||||
|
try expect(result) == "Hello Kyle"
|
||||||
|
}
|
||||||
|
|
||||||
|
$0.it("shows the default value") {
|
||||||
|
let result = try template.render(Context(dictionary: [:]))
|
||||||
|
try expect(result) == "Hello World"
|
||||||
|
}
|
||||||
|
|
||||||
|
$0.it("supports multiple defaults") {
|
||||||
|
let template = Template(templateString: "Hello {{ name|default:a,b,c,\"World\" }}")
|
||||||
|
let result = try template.render(Context(dictionary: [:]))
|
||||||
|
try expect(result) == "Hello World"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
describe("join filter") {
|
||||||
|
let template = Template(templateString: "{{ value|join:\", \" }}")
|
||||||
|
|
||||||
|
$0.it("joins a collection of strings") {
|
||||||
|
let result = try template.render(Context(dictionary: ["value": ["One", "Two"]]))
|
||||||
|
try expect(result) == "One, Two"
|
||||||
|
}
|
||||||
|
|
||||||
|
$0.it("joins a mixed-type collection") {
|
||||||
|
let result = try template.render(Context(dictionary: ["value": ["One", 2, true, 10.5, "Five"]]))
|
||||||
|
try expect(result) == "One, 2, true, 10.5, Five"
|
||||||
|
}
|
||||||
|
|
||||||
|
$0.it("can join by non string") {
|
||||||
|
let template = Template(templateString: "{{ value|join:separator }}")
|
||||||
|
let result = try template.render(Context(dictionary: ["value": ["One", "Two"], "separator": true]))
|
||||||
|
try expect(result) == "OnetrueTwo"
|
||||||
|
}
|
||||||
|
|
||||||
|
$0.it("can join without arguments") {
|
||||||
|
let template = Template(templateString: "{{ value|join }}")
|
||||||
|
let result = try template.render(Context(dictionary: ["value": ["One", "Two"]]))
|
||||||
|
try expect(result) == "OneTwo"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
25
Tests/StencilTests/FilterTagSpec.swift
Normal file
25
Tests/StencilTests/FilterTagSpec.swift
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
import Spectre
|
||||||
|
import Stencil
|
||||||
|
|
||||||
|
|
||||||
|
func testFilterTag() {
|
||||||
|
describe("Filter Tag") {
|
||||||
|
$0.it("allows you to use a filter") {
|
||||||
|
let template = Template(templateString: "{% filter uppercase %}Test{% endfilter %}")
|
||||||
|
let result = try template.render()
|
||||||
|
try expect(result) == "TEST"
|
||||||
|
}
|
||||||
|
|
||||||
|
$0.it("allows you to chain filters") {
|
||||||
|
let template = Template(templateString: "{% filter lowercase|capitalize %}TEST{% endfilter %}")
|
||||||
|
let result = try template.render()
|
||||||
|
try expect(result) == "Test"
|
||||||
|
}
|
||||||
|
|
||||||
|
$0.it("errors without a filter") {
|
||||||
|
let template = Template(templateString: "{% filter %}Test{% endfilter %}")
|
||||||
|
try expect(try template.render()).toThrow()
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
128
Tests/StencilTests/ForNodeSpec.swift
Normal file
128
Tests/StencilTests/ForNodeSpec.swift
Normal file
@@ -0,0 +1,128 @@
|
|||||||
|
import Spectre
|
||||||
|
@testable import Stencil
|
||||||
|
import Foundation
|
||||||
|
|
||||||
|
|
||||||
|
func testForNode() {
|
||||||
|
describe("ForNode") {
|
||||||
|
let context = Context(dictionary: [
|
||||||
|
"items": [1, 2, 3],
|
||||||
|
"emptyItems": [Int](),
|
||||||
|
"dict": [
|
||||||
|
"one": "I",
|
||||||
|
"two": "II",
|
||||||
|
]
|
||||||
|
])
|
||||||
|
|
||||||
|
$0.it("renders the given nodes for each item") {
|
||||||
|
let nodes: [NodeType] = [VariableNode(variable: "item")]
|
||||||
|
let node = ForNode(resolvable: Variable("items"), loopVariables: ["item"], nodes: nodes, emptyNodes: [])
|
||||||
|
try expect(try node.render(context)) == "123"
|
||||||
|
}
|
||||||
|
|
||||||
|
$0.it("renders the given empty nodes when no items found item") {
|
||||||
|
let nodes: [NodeType] = [VariableNode(variable: "item")]
|
||||||
|
let emptyNodes: [NodeType] = [TextNode(text: "empty")]
|
||||||
|
let node = ForNode(resolvable: Variable("emptyItems"), loopVariables: ["item"], nodes: nodes, emptyNodes: emptyNodes)
|
||||||
|
try expect(try node.render(context)) == "empty"
|
||||||
|
}
|
||||||
|
|
||||||
|
$0.it("renders a context variable of type Array<Any>") {
|
||||||
|
let any_context = Context(dictionary: [
|
||||||
|
"items": ([1, 2, 3] as [Any])
|
||||||
|
])
|
||||||
|
|
||||||
|
let nodes: [NodeType] = [VariableNode(variable: "item")]
|
||||||
|
let node = ForNode(resolvable: Variable("items"), loopVariables: ["item"], nodes: nodes, emptyNodes: [])
|
||||||
|
try expect(try node.render(any_context)) == "123"
|
||||||
|
}
|
||||||
|
|
||||||
|
#if os(OSX)
|
||||||
|
$0.it("renders a context variable of type NSArray") {
|
||||||
|
let nsarray_context = Context(dictionary: [
|
||||||
|
"items": NSArray(array: [1, 2, 3])
|
||||||
|
])
|
||||||
|
|
||||||
|
let nodes: [NodeType] = [VariableNode(variable: "item")]
|
||||||
|
let node = ForNode(resolvable: Variable("items"), loopVariables: ["item"], nodes: nodes, emptyNodes: [])
|
||||||
|
try expect(try node.render(nsarray_context)) == "123"
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
$0.it("renders the given nodes while providing if the item is first in the context") {
|
||||||
|
let nodes: [NodeType] = [VariableNode(variable: "item"), VariableNode(variable: "forloop.first")]
|
||||||
|
let node = ForNode(resolvable: Variable("items"), loopVariables: ["item"], nodes: nodes, emptyNodes: [])
|
||||||
|
try expect(try node.render(context)) == "1true2false3false"
|
||||||
|
}
|
||||||
|
|
||||||
|
$0.it("renders the given nodes while providing if the item is last in the context") {
|
||||||
|
let nodes: [NodeType] = [VariableNode(variable: "item"), VariableNode(variable: "forloop.last")]
|
||||||
|
let node = ForNode(resolvable: Variable("items"), loopVariables: ["item"], nodes: nodes, emptyNodes: [])
|
||||||
|
try expect(try node.render(context)) == "1false2false3true"
|
||||||
|
}
|
||||||
|
|
||||||
|
$0.it("renders the given nodes while providing item counter") {
|
||||||
|
let nodes: [NodeType] = [VariableNode(variable: "item"), VariableNode(variable: "forloop.counter")]
|
||||||
|
let node = ForNode(resolvable: Variable("items"), loopVariables: ["item"], nodes: nodes, emptyNodes: [])
|
||||||
|
try expect(try node.render(context)) == "112233"
|
||||||
|
}
|
||||||
|
|
||||||
|
$0.it("renders the given nodes while filtering items using where expression") {
|
||||||
|
let nodes: [NodeType] = [VariableNode(variable: "item"), VariableNode(variable: "forloop.counter")]
|
||||||
|
let `where` = try parseExpression(components: ["item", ">", "1"], tokenParser: TokenParser(tokens: [], environment: Environment()))
|
||||||
|
let node = ForNode(resolvable: Variable("items"), loopVariables: ["item"], nodes: nodes, emptyNodes: [], where: `where`)
|
||||||
|
try expect(try node.render(context)) == "2132"
|
||||||
|
}
|
||||||
|
|
||||||
|
$0.it("renders the given empty nodes when all items filtered out with where expression") {
|
||||||
|
let nodes: [NodeType] = [VariableNode(variable: "item")]
|
||||||
|
let emptyNodes: [NodeType] = [TextNode(text: "empty")]
|
||||||
|
let `where` = try parseExpression(components: ["item", "==", "0"], tokenParser: TokenParser(tokens: [], environment: Environment()))
|
||||||
|
let node = ForNode(resolvable: Variable("emptyItems"), loopVariables: ["item"], nodes: nodes, emptyNodes: emptyNodes, where: `where`)
|
||||||
|
try expect(try node.render(context)) == "empty"
|
||||||
|
}
|
||||||
|
|
||||||
|
$0.it("can render a filter") {
|
||||||
|
let templateString = "{% for article in ars|default:articles %}" +
|
||||||
|
"- {{ article.title }} by {{ article.author }}.\n" +
|
||||||
|
"{% endfor %}\n"
|
||||||
|
|
||||||
|
let context = Context(dictionary: [
|
||||||
|
"articles": [
|
||||||
|
Article(title: "Migrating from OCUnit to XCTest", author: "Kyle Fuller"),
|
||||||
|
Article(title: "Memory Management with ARC", author: "Kyle Fuller"),
|
||||||
|
]
|
||||||
|
])
|
||||||
|
|
||||||
|
let template = Template(templateString: templateString)
|
||||||
|
let result = try template.render(context)
|
||||||
|
|
||||||
|
let fixture = "" +
|
||||||
|
"- Migrating from OCUnit to XCTest by Kyle Fuller.\n" +
|
||||||
|
"- Memory Management with ARC by Kyle Fuller.\n" +
|
||||||
|
"\n"
|
||||||
|
|
||||||
|
try expect(result) == fixture
|
||||||
|
}
|
||||||
|
|
||||||
|
$0.it("renders supports iterating over dictionary") {
|
||||||
|
let nodes: [NodeType] = [VariableNode(variable: "key")]
|
||||||
|
let emptyNodes: [NodeType] = [TextNode(text: "empty")]
|
||||||
|
let node = ForNode(resolvable: Variable("dict"), loopVariables: ["key"], nodes: nodes, emptyNodes: emptyNodes, where: nil)
|
||||||
|
try expect(try node.render(context)) == "onetwo"
|
||||||
|
}
|
||||||
|
|
||||||
|
$0.it("renders supports iterating over dictionary") {
|
||||||
|
let nodes: [NodeType] = [VariableNode(variable: "key"), VariableNode(variable: "value")]
|
||||||
|
let emptyNodes: [NodeType] = [TextNode(text: "empty")]
|
||||||
|
let node = ForNode(resolvable: Variable("dict"), loopVariables: ["key", "value"], nodes: nodes, emptyNodes: emptyNodes, where: nil)
|
||||||
|
try expect(try node.render(context)) == "oneItwoII"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
fileprivate struct Article {
|
||||||
|
let title: String
|
||||||
|
let author: String
|
||||||
|
}
|
||||||
257
Tests/StencilTests/IfNodeSpec.swift
Normal file
257
Tests/StencilTests/IfNodeSpec.swift
Normal file
@@ -0,0 +1,257 @@
|
|||||||
|
import Spectre
|
||||||
|
@testable import Stencil
|
||||||
|
|
||||||
|
|
||||||
|
func testIfNode() {
|
||||||
|
describe("IfNode") {
|
||||||
|
$0.describe("parsing") {
|
||||||
|
$0.it("can parse an if block") {
|
||||||
|
let tokens: [Token] = [
|
||||||
|
.block(value: "if value"),
|
||||||
|
.text(value: "true"),
|
||||||
|
.block(value: "endif")
|
||||||
|
]
|
||||||
|
|
||||||
|
let parser = TokenParser(tokens: tokens, environment: Environment())
|
||||||
|
let nodes = try parser.parse()
|
||||||
|
let node = nodes.first as? IfNode
|
||||||
|
|
||||||
|
let conditions = node?.conditions
|
||||||
|
try expect(conditions?.count) == 1
|
||||||
|
try expect(conditions?[0].nodes.count) == 1
|
||||||
|
let trueNode = conditions?[0].nodes.first as? TextNode
|
||||||
|
try expect(trueNode?.text) == "true"
|
||||||
|
}
|
||||||
|
|
||||||
|
$0.it("can parse an if with else block") {
|
||||||
|
let tokens: [Token] = [
|
||||||
|
.block(value: "if value"),
|
||||||
|
.text(value: "true"),
|
||||||
|
.block(value: "else"),
|
||||||
|
.text(value: "false"),
|
||||||
|
.block(value: "endif")
|
||||||
|
]
|
||||||
|
|
||||||
|
let parser = TokenParser(tokens: tokens, environment: Environment())
|
||||||
|
let nodes = try parser.parse()
|
||||||
|
let node = nodes.first as? IfNode
|
||||||
|
|
||||||
|
let conditions = node?.conditions
|
||||||
|
try expect(conditions?.count) == 2
|
||||||
|
|
||||||
|
try expect(conditions?[0].nodes.count) == 1
|
||||||
|
let trueNode = conditions?[0].nodes.first as? TextNode
|
||||||
|
try expect(trueNode?.text) == "true"
|
||||||
|
|
||||||
|
try expect(conditions?[1].nodes.count) == 1
|
||||||
|
let falseNode = conditions?[1].nodes.first as? TextNode
|
||||||
|
try expect(falseNode?.text) == "false"
|
||||||
|
}
|
||||||
|
|
||||||
|
$0.it("can parse an if with elif block") {
|
||||||
|
let tokens: [Token] = [
|
||||||
|
.block(value: "if value"),
|
||||||
|
.text(value: "true"),
|
||||||
|
.block(value: "elif something"),
|
||||||
|
.text(value: "some"),
|
||||||
|
.block(value: "else"),
|
||||||
|
.text(value: "false"),
|
||||||
|
.block(value: "endif")
|
||||||
|
]
|
||||||
|
|
||||||
|
let parser = TokenParser(tokens: tokens, environment: Environment())
|
||||||
|
let nodes = try parser.parse()
|
||||||
|
let node = nodes.first as? IfNode
|
||||||
|
|
||||||
|
let conditions = node?.conditions
|
||||||
|
try expect(conditions?.count) == 3
|
||||||
|
|
||||||
|
try expect(conditions?[0].nodes.count) == 1
|
||||||
|
let trueNode = conditions?[0].nodes.first as? TextNode
|
||||||
|
try expect(trueNode?.text) == "true"
|
||||||
|
|
||||||
|
try expect(conditions?[1].nodes.count) == 1
|
||||||
|
let elifNode = conditions?[1].nodes.first as? TextNode
|
||||||
|
try expect(elifNode?.text) == "some"
|
||||||
|
|
||||||
|
try expect(conditions?[2].nodes.count) == 1
|
||||||
|
let falseNode = conditions?[2].nodes.first as? TextNode
|
||||||
|
try expect(falseNode?.text) == "false"
|
||||||
|
}
|
||||||
|
|
||||||
|
$0.it("can parse an if with elif block without else") {
|
||||||
|
let tokens: [Token] = [
|
||||||
|
.block(value: "if value"),
|
||||||
|
.text(value: "true"),
|
||||||
|
.block(value: "elif something"),
|
||||||
|
.text(value: "some"),
|
||||||
|
.block(value: "endif")
|
||||||
|
]
|
||||||
|
|
||||||
|
let parser = TokenParser(tokens: tokens, environment: Environment())
|
||||||
|
let nodes = try parser.parse()
|
||||||
|
let node = nodes.first as? IfNode
|
||||||
|
|
||||||
|
let conditions = node?.conditions
|
||||||
|
try expect(conditions?.count) == 2
|
||||||
|
|
||||||
|
try expect(conditions?[0].nodes.count) == 1
|
||||||
|
let trueNode = conditions?[0].nodes.first as? TextNode
|
||||||
|
try expect(trueNode?.text) == "true"
|
||||||
|
|
||||||
|
try expect(conditions?[1].nodes.count) == 1
|
||||||
|
let elifNode = conditions?[1].nodes.first as? TextNode
|
||||||
|
try expect(elifNode?.text) == "some"
|
||||||
|
}
|
||||||
|
|
||||||
|
$0.it("can parse an if with multiple elif block") {
|
||||||
|
let tokens: [Token] = [
|
||||||
|
.block(value: "if value"),
|
||||||
|
.text(value: "true"),
|
||||||
|
.block(value: "elif something1"),
|
||||||
|
.text(value: "some1"),
|
||||||
|
.block(value: "elif something2"),
|
||||||
|
.text(value: "some2"),
|
||||||
|
.block(value: "else"),
|
||||||
|
.text(value: "false"),
|
||||||
|
.block(value: "endif")
|
||||||
|
]
|
||||||
|
|
||||||
|
let parser = TokenParser(tokens: tokens, environment: Environment())
|
||||||
|
let nodes = try parser.parse()
|
||||||
|
let node = nodes.first as? IfNode
|
||||||
|
|
||||||
|
let conditions = node?.conditions
|
||||||
|
try expect(conditions?.count) == 4
|
||||||
|
|
||||||
|
try expect(conditions?[0].nodes.count) == 1
|
||||||
|
let trueNode = conditions?[0].nodes.first as? TextNode
|
||||||
|
try expect(trueNode?.text) == "true"
|
||||||
|
|
||||||
|
try expect(conditions?[1].nodes.count) == 1
|
||||||
|
let elifNode = conditions?[1].nodes.first as? TextNode
|
||||||
|
try expect(elifNode?.text) == "some1"
|
||||||
|
|
||||||
|
try expect(conditions?[2].nodes.count) == 1
|
||||||
|
let elif2Node = conditions?[2].nodes.first as? TextNode
|
||||||
|
try expect(elif2Node?.text) == "some2"
|
||||||
|
|
||||||
|
try expect(conditions?[3].nodes.count) == 1
|
||||||
|
let falseNode = conditions?[3].nodes.first as? TextNode
|
||||||
|
try expect(falseNode?.text) == "false"
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
$0.it("can parse an if with complex expression") {
|
||||||
|
let tokens: [Token] = [
|
||||||
|
.block(value: "if value == \"test\" and not name"),
|
||||||
|
.text(value: "true"),
|
||||||
|
.block(value: "endif")
|
||||||
|
]
|
||||||
|
|
||||||
|
let parser = TokenParser(tokens: tokens, environment: Environment())
|
||||||
|
let nodes = try parser.parse()
|
||||||
|
try expect(nodes.first is IfNode).beTrue()
|
||||||
|
}
|
||||||
|
|
||||||
|
$0.it("can parse an ifnot block") {
|
||||||
|
let tokens: [Token] = [
|
||||||
|
.block(value: "ifnot value"),
|
||||||
|
.text(value: "false"),
|
||||||
|
.block(value: "else"),
|
||||||
|
.text(value: "true"),
|
||||||
|
.block(value: "endif")
|
||||||
|
]
|
||||||
|
|
||||||
|
let parser = TokenParser(tokens: tokens, environment: Environment())
|
||||||
|
let nodes = try parser.parse()
|
||||||
|
let node = nodes.first as? IfNode
|
||||||
|
let conditions = node?.conditions
|
||||||
|
try expect(conditions?.count) == 2
|
||||||
|
|
||||||
|
try expect(conditions?[0].nodes.count) == 1
|
||||||
|
let trueNode = conditions?[0].nodes.first as? TextNode
|
||||||
|
try expect(trueNode?.text) == "true"
|
||||||
|
|
||||||
|
try expect(conditions?[1].nodes.count) == 1
|
||||||
|
let falseNode = conditions?[1].nodes.first as? TextNode
|
||||||
|
try expect(falseNode?.text) == "false"
|
||||||
|
}
|
||||||
|
|
||||||
|
$0.it("throws an error when parsing an if block without an endif") {
|
||||||
|
let tokens: [Token] = [
|
||||||
|
.block(value: "if value"),
|
||||||
|
]
|
||||||
|
|
||||||
|
let parser = TokenParser(tokens: tokens, environment: Environment())
|
||||||
|
let error = TemplateSyntaxError("`endif` was not found.")
|
||||||
|
try expect(try parser.parse()).toThrow(error)
|
||||||
|
}
|
||||||
|
|
||||||
|
$0.it("throws an error when parsing an ifnot without an endif") {
|
||||||
|
let tokens: [Token] = [
|
||||||
|
.block(value: "ifnot value"),
|
||||||
|
]
|
||||||
|
|
||||||
|
let parser = TokenParser(tokens: tokens, environment: Environment())
|
||||||
|
let error = TemplateSyntaxError("`endif` was not found.")
|
||||||
|
try expect(try parser.parse()).toThrow(error)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$0.describe("rendering") {
|
||||||
|
$0.it("renders a true expression") {
|
||||||
|
let node = IfNode(conditions: [
|
||||||
|
IfCondition(expression: StaticExpression(value: true), nodes: [TextNode(text: "1")]),
|
||||||
|
IfCondition(expression: StaticExpression(value: true), nodes: [TextNode(text: "2")]),
|
||||||
|
IfCondition(expression: nil, nodes: [TextNode(text: "3")]),
|
||||||
|
])
|
||||||
|
|
||||||
|
try expect(try node.render(Context())) == "1"
|
||||||
|
}
|
||||||
|
|
||||||
|
$0.it("renders the first true expression") {
|
||||||
|
let node = IfNode(conditions: [
|
||||||
|
IfCondition(expression: StaticExpression(value: false), nodes: [TextNode(text: "1")]),
|
||||||
|
IfCondition(expression: StaticExpression(value: true), nodes: [TextNode(text: "2")]),
|
||||||
|
IfCondition(expression: nil, nodes: [TextNode(text: "3")]),
|
||||||
|
])
|
||||||
|
|
||||||
|
try expect(try node.render(Context())) == "2"
|
||||||
|
}
|
||||||
|
|
||||||
|
$0.it("renders the empty expression when other conditions are falsy") {
|
||||||
|
let node = IfNode(conditions: [
|
||||||
|
IfCondition(expression: StaticExpression(value: false), nodes: [TextNode(text: "1")]),
|
||||||
|
IfCondition(expression: StaticExpression(value: false), nodes: [TextNode(text: "2")]),
|
||||||
|
IfCondition(expression: nil, nodes: [TextNode(text: "3")]),
|
||||||
|
])
|
||||||
|
|
||||||
|
try expect(try node.render(Context())) == "3"
|
||||||
|
}
|
||||||
|
|
||||||
|
$0.it("renders empty when no truthy conditions") {
|
||||||
|
let node = IfNode(conditions: [
|
||||||
|
IfCondition(expression: StaticExpression(value: false), nodes: [TextNode(text: "1")]),
|
||||||
|
IfCondition(expression: StaticExpression(value: false), nodes: [TextNode(text: "2")]),
|
||||||
|
])
|
||||||
|
|
||||||
|
try expect(try node.render(Context())) == ""
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$0.it("supports variable filters in the if expression") {
|
||||||
|
let tokens: [Token] = [
|
||||||
|
.block(value: "if value|uppercase == \"TEST\""),
|
||||||
|
.text(value: "true"),
|
||||||
|
.block(value: "endif")
|
||||||
|
]
|
||||||
|
|
||||||
|
let parser = TokenParser(tokens: tokens, environment: Environment())
|
||||||
|
let nodes = try parser.parse()
|
||||||
|
|
||||||
|
let result = try renderNodes(nodes, Context(dictionary: ["value": "test"]))
|
||||||
|
try expect(result) == "true"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,25 +1,26 @@
|
|||||||
import Spectre
|
import Spectre
|
||||||
import Stencil
|
@testable import Stencil
|
||||||
import PathKit
|
import PathKit
|
||||||
|
|
||||||
|
|
||||||
func testInclude() {
|
func testInclude() {
|
||||||
describe("Include") {
|
describe("Include") {
|
||||||
let path = Path(__FILE__) + ".." + ".." + "fixtures"
|
let path = Path(#file) + ".." + "fixtures"
|
||||||
let loader = TemplateLoader(paths: [path])
|
let loader = FileSystemLoader(paths: [path])
|
||||||
|
let environment = Environment(loader: loader)
|
||||||
|
|
||||||
$0.describe("parsing") {
|
$0.describe("parsing") {
|
||||||
$0.it("throws an error when no template is given") {
|
$0.it("throws an error when no template is given") {
|
||||||
let tokens = [ Token.Block(value: "include") ]
|
let tokens: [Token] = [ .block(value: "include") ]
|
||||||
let parser = TokenParser(tokens: tokens, namespace: Namespace())
|
let parser = TokenParser(tokens: tokens, environment: Environment())
|
||||||
|
|
||||||
let error = TemplateSyntaxError("'include' tag takes one argument, the template file to be included")
|
let error = TemplateSyntaxError("'include' tag takes one argument, the template file to be included")
|
||||||
try expect(try parser.parse()).toThrow(error)
|
try expect(try parser.parse()).toThrow(error)
|
||||||
}
|
}
|
||||||
|
|
||||||
$0.it("can parse a valid include block") {
|
$0.it("can parse a valid include block") {
|
||||||
let tokens = [ Token.Block(value: "include \"test.html\"") ]
|
let tokens: [Token] = [ .block(value: "include \"test.html\"") ]
|
||||||
let parser = TokenParser(tokens: tokens, namespace: Namespace())
|
let parser = TokenParser(tokens: tokens, environment: Environment())
|
||||||
|
|
||||||
let nodes = try parser.parse()
|
let nodes = try parser.parse()
|
||||||
let node = nodes.first as? IncludeNode
|
let node = nodes.first as? IncludeNode
|
||||||
@@ -33,9 +34,9 @@ func testInclude() {
|
|||||||
let node = IncludeNode(templateName: Variable("\"test.html\""))
|
let node = IncludeNode(templateName: Variable("\"test.html\""))
|
||||||
|
|
||||||
do {
|
do {
|
||||||
try node.render(Context())
|
_ = try node.render(Context())
|
||||||
} catch {
|
} catch {
|
||||||
try expect("\(error)") == "Template loader not in context"
|
try expect("\(error)") == "Template named `test.html` does not exist. No loaders found"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -43,15 +44,15 @@ func testInclude() {
|
|||||||
let node = IncludeNode(templateName: Variable("\"unknown.html\""))
|
let node = IncludeNode(templateName: Variable("\"unknown.html\""))
|
||||||
|
|
||||||
do {
|
do {
|
||||||
try node.render(Context(dictionary: ["loader": loader]))
|
_ = try node.render(Context(environment: environment))
|
||||||
} catch {
|
} catch {
|
||||||
try expect("\(error)".hasPrefix("'unknown.html' template not found")).to.beTrue()
|
try expect("\(error)".hasPrefix("Template named `unknown.html` does not exist in loader")).to.beTrue()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$0.it("successfully renders a found included template") {
|
$0.it("successfully renders a found included template") {
|
||||||
let node = IncludeNode(templateName: Variable("\"test.html\""))
|
let node = IncludeNode(templateName: Variable("\"test.html\""))
|
||||||
let context = Context(dictionary: ["loader":loader, "target": "World"])
|
let context = Context(dictionary: ["target": "World"], environment: environment)
|
||||||
let value = try node.render(context)
|
let value = try node.render(context)
|
||||||
try expect(value) == "Hello World!"
|
try expect(value) == "Hello World!"
|
||||||
}
|
}
|
||||||
27
Tests/StencilTests/InheritenceSpec.swift
Normal file
27
Tests/StencilTests/InheritenceSpec.swift
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
import Spectre
|
||||||
|
import Stencil
|
||||||
|
import PathKit
|
||||||
|
|
||||||
|
|
||||||
|
func testInheritence() {
|
||||||
|
describe("Inheritence") {
|
||||||
|
let path = Path(#file) + ".." + "fixtures"
|
||||||
|
let loader = FileSystemLoader(paths: [path])
|
||||||
|
let environment = Environment(loader: loader)
|
||||||
|
|
||||||
|
$0.it("can inherit from another template") {
|
||||||
|
let template = try environment.loadTemplate(name: "child.html")
|
||||||
|
try expect(try template.render()) == "Header\nChild"
|
||||||
|
}
|
||||||
|
|
||||||
|
$0.it("can inherit from another template inheriting from another template") {
|
||||||
|
let template = try environment.loadTemplate(name: "child-child.html")
|
||||||
|
try expect(try template.render()) == "Child Child Header\nChild"
|
||||||
|
}
|
||||||
|
|
||||||
|
$0.it("can inherit from a template that calls a super block") {
|
||||||
|
let template = try environment.loadTemplate(name: "child-super.html")
|
||||||
|
try expect(try template.render()) == "Header\nChild Body"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
import Spectre
|
import Spectre
|
||||||
import Stencil
|
@testable import Stencil
|
||||||
|
|
||||||
|
|
||||||
func testLexer() {
|
func testLexer() {
|
||||||
@@ -9,7 +9,7 @@ func testLexer() {
|
|||||||
let tokens = lexer.tokenize()
|
let tokens = lexer.tokenize()
|
||||||
|
|
||||||
try expect(tokens.count) == 1
|
try expect(tokens.count) == 1
|
||||||
try expect(tokens.first) == Token.Text(value: "Hello World")
|
try expect(tokens.first) == .text(value: "Hello World")
|
||||||
}
|
}
|
||||||
|
|
||||||
$0.it("can tokenize a comment") {
|
$0.it("can tokenize a comment") {
|
||||||
@@ -17,7 +17,7 @@ func testLexer() {
|
|||||||
let tokens = lexer.tokenize()
|
let tokens = lexer.tokenize()
|
||||||
|
|
||||||
try expect(tokens.count) == (1)
|
try expect(tokens.count) == (1)
|
||||||
try expect(tokens.first) == Token.Comment(value: "Comment")
|
try expect(tokens.first) == .comment(value: "Comment")
|
||||||
}
|
}
|
||||||
|
|
||||||
$0.it("can tokenize a variable") {
|
$0.it("can tokenize a variable") {
|
||||||
@@ -25,7 +25,7 @@ func testLexer() {
|
|||||||
let tokens = lexer.tokenize()
|
let tokens = lexer.tokenize()
|
||||||
|
|
||||||
try expect(tokens.count) == 1
|
try expect(tokens.count) == 1
|
||||||
try expect(tokens.first) == Token.Variable(value: "Variable")
|
try expect(tokens.first) == .variable(value: "Variable")
|
||||||
}
|
}
|
||||||
|
|
||||||
$0.it("can tokenize a mixture of content") {
|
$0.it("can tokenize a mixture of content") {
|
||||||
@@ -33,9 +33,9 @@ func testLexer() {
|
|||||||
let tokens = lexer.tokenize()
|
let tokens = lexer.tokenize()
|
||||||
|
|
||||||
try expect(tokens.count) == 3
|
try expect(tokens.count) == 3
|
||||||
try expect(tokens[0]) == Token.Text(value: "My name is ")
|
try expect(tokens[0]) == Token.text(value: "My name is ")
|
||||||
try expect(tokens[1]) == Token.Variable(value: "name")
|
try expect(tokens[1]) == Token.variable(value: "name")
|
||||||
try expect(tokens[2]) == Token.Text(value: ".")
|
try expect(tokens[2]) == Token.text(value: ".")
|
||||||
}
|
}
|
||||||
|
|
||||||
$0.it("can tokenize two variables without being greedy") {
|
$0.it("can tokenize two variables without being greedy") {
|
||||||
@@ -43,8 +43,8 @@ func testLexer() {
|
|||||||
let tokens = lexer.tokenize()
|
let tokens = lexer.tokenize()
|
||||||
|
|
||||||
try expect(tokens.count) == 2
|
try expect(tokens.count) == 2
|
||||||
try expect(tokens[0]) == Token.Variable(value: "thing")
|
try expect(tokens[0]) == Token.variable(value: "thing")
|
||||||
try expect(tokens[1]) == Token.Variable(value: "name")
|
try expect(tokens[1]) == Token.variable(value: "name")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
32
Tests/StencilTests/LoaderSpec.swift
Normal file
32
Tests/StencilTests/LoaderSpec.swift
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
import Spectre
|
||||||
|
import Stencil
|
||||||
|
import PathKit
|
||||||
|
|
||||||
|
|
||||||
|
func testTemplateLoader() {
|
||||||
|
describe("FileSystemLoader") {
|
||||||
|
let path = Path(#file) + ".." + "fixtures"
|
||||||
|
let loader = FileSystemLoader(paths: [path])
|
||||||
|
let environment = Environment(loader: loader)
|
||||||
|
|
||||||
|
$0.it("errors when a template cannot be found") {
|
||||||
|
try expect(try environment.loadTemplate(name: "unknown.html")).toThrow()
|
||||||
|
}
|
||||||
|
|
||||||
|
$0.it("errors when an array of templates cannot be found") {
|
||||||
|
try expect(try environment.loadTemplate(names: ["unknown.html", "unknown2.html"])).toThrow()
|
||||||
|
}
|
||||||
|
|
||||||
|
$0.it("can load a template from a file") {
|
||||||
|
_ = try environment.loadTemplate(name: "test.html")
|
||||||
|
}
|
||||||
|
|
||||||
|
$0.it("errors when loading absolute file outside of the selected path") {
|
||||||
|
try expect(try environment.loadTemplate(name: "/etc/hosts")).toThrow()
|
||||||
|
}
|
||||||
|
|
||||||
|
$0.it("errors when loading relative file outside of the selected path") {
|
||||||
|
try expect(try environment.loadTemplate(name: "../LoaderSpec.swift")).toThrow()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,9 +1,9 @@
|
|||||||
import Spectre
|
import Spectre
|
||||||
import Stencil
|
@testable import Stencil
|
||||||
|
|
||||||
|
|
||||||
class ErrorNode : NodeType {
|
class ErrorNode : NodeType {
|
||||||
func render(context: Context) throws -> String {
|
func render(_ context: Context) throws -> String {
|
||||||
throw TemplateSyntaxError("Custom Error")
|
throw TemplateSyntaxError("Custom Error")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
import Foundation
|
import Foundation
|
||||||
import Spectre
|
import Spectre
|
||||||
import Stencil
|
@testable import Stencil
|
||||||
|
|
||||||
|
|
||||||
func testNowNode() {
|
func testNowNode() {
|
||||||
@@ -8,8 +8,8 @@ func testNowNode() {
|
|||||||
describe("NowNode") {
|
describe("NowNode") {
|
||||||
$0.describe("parsing") {
|
$0.describe("parsing") {
|
||||||
$0.it("parses default format without any now arguments") {
|
$0.it("parses default format without any now arguments") {
|
||||||
let tokens = [ Token.Block(value: "now") ]
|
let tokens: [Token] = [ .block(value: "now") ]
|
||||||
let parser = TokenParser(tokens: tokens, namespace: Namespace())
|
let parser = TokenParser(tokens: tokens, environment: Environment())
|
||||||
|
|
||||||
let nodes = try parser.parse()
|
let nodes = try parser.parse()
|
||||||
let node = nodes.first as? NowNode
|
let node = nodes.first as? NowNode
|
||||||
@@ -18,8 +18,8 @@ func testNowNode() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
$0.it("parses now with a format") {
|
$0.it("parses now with a format") {
|
||||||
let tokens = [ Token.Block(value: "now \"HH:mm\"") ]
|
let tokens: [Token] = [ .block(value: "now \"HH:mm\"") ]
|
||||||
let parser = TokenParser(tokens: tokens, namespace: Namespace())
|
let parser = TokenParser(tokens: tokens, environment: Environment())
|
||||||
let nodes = try parser.parse()
|
let nodes = try parser.parse()
|
||||||
let node = nodes.first as? NowNode
|
let node = nodes.first as? NowNode
|
||||||
try expect(nodes.count) == 1
|
try expect(nodes.count) == 1
|
||||||
@@ -31,9 +31,9 @@ func testNowNode() {
|
|||||||
$0.it("renders the date") {
|
$0.it("renders the date") {
|
||||||
let node = NowNode(format: Variable("\"yyyy-MM-dd\""))
|
let node = NowNode(format: Variable("\"yyyy-MM-dd\""))
|
||||||
|
|
||||||
let formatter = NSDateFormatter()
|
let formatter = DateFormatter()
|
||||||
formatter.dateFormat = "yyyy-MM-dd"
|
formatter.dateFormat = "yyyy-MM-dd"
|
||||||
let date = formatter.stringFromDate(NSDate())
|
let date = formatter.string(from: NSDate() as Date)
|
||||||
|
|
||||||
try expect(try node.render(Context())) == date
|
try expect(try node.render(Context())) == date
|
||||||
}
|
}
|
||||||
@@ -1,13 +1,13 @@
|
|||||||
import Spectre
|
import Spectre
|
||||||
import Stencil
|
@testable import Stencil
|
||||||
|
|
||||||
|
|
||||||
func testTokenParser() {
|
func testTokenParser() {
|
||||||
describe("TokenParser") {
|
describe("TokenParser") {
|
||||||
$0.it("can parse a text token") {
|
$0.it("can parse a text token") {
|
||||||
let parser = TokenParser(tokens: [
|
let parser = TokenParser(tokens: [
|
||||||
Token.Text(value: "Hello World")
|
.text(value: "Hello World")
|
||||||
], namespace: Namespace())
|
], environment: Environment())
|
||||||
|
|
||||||
let nodes = try parser.parse()
|
let nodes = try parser.parse()
|
||||||
let node = nodes.first as? TextNode
|
let node = nodes.first as? TextNode
|
||||||
@@ -18,8 +18,8 @@ func testTokenParser() {
|
|||||||
|
|
||||||
$0.it("can parse a variable token") {
|
$0.it("can parse a variable token") {
|
||||||
let parser = TokenParser(tokens: [
|
let parser = TokenParser(tokens: [
|
||||||
Token.Variable(value: "'name'")
|
.variable(value: "'name'")
|
||||||
], namespace: Namespace())
|
], environment: Environment())
|
||||||
|
|
||||||
let nodes = try parser.parse()
|
let nodes = try parser.parse()
|
||||||
let node = nodes.first as? VariableNode
|
let node = nodes.first as? VariableNode
|
||||||
@@ -30,22 +30,22 @@ func testTokenParser() {
|
|||||||
|
|
||||||
$0.it("can parse a comment token") {
|
$0.it("can parse a comment token") {
|
||||||
let parser = TokenParser(tokens: [
|
let parser = TokenParser(tokens: [
|
||||||
Token.Comment(value: "Secret stuff!")
|
.comment(value: "Secret stuff!")
|
||||||
], namespace: Namespace())
|
], environment: Environment())
|
||||||
|
|
||||||
let nodes = try parser.parse()
|
let nodes = try parser.parse()
|
||||||
try expect(nodes.count) == 0
|
try expect(nodes.count) == 0
|
||||||
}
|
}
|
||||||
|
|
||||||
$0.it("can parse a tag token") {
|
$0.it("can parse a tag token") {
|
||||||
let namespace = Namespace()
|
let simpleExtension = Extension()
|
||||||
namespace.registerSimpleTag("known") { _ in
|
simpleExtension.registerSimpleTag("known") { _ in
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
let parser = TokenParser(tokens: [
|
let parser = TokenParser(tokens: [
|
||||||
Token.Block(value: "known"),
|
.block(value: "known"),
|
||||||
], namespace: namespace)
|
], environment: Environment(extensions: [simpleExtension]))
|
||||||
|
|
||||||
let nodes = try parser.parse()
|
let nodes = try parser.parse()
|
||||||
try expect(nodes.count) == 1
|
try expect(nodes.count) == 1
|
||||||
@@ -53,8 +53,8 @@ func testTokenParser() {
|
|||||||
|
|
||||||
$0.it("errors when parsing an unknown tag") {
|
$0.it("errors when parsing an unknown tag") {
|
||||||
let parser = TokenParser(tokens: [
|
let parser = TokenParser(tokens: [
|
||||||
Token.Block(value: "unknown"),
|
.block(value: "unknown"),
|
||||||
], namespace: Namespace())
|
], environment: Environment())
|
||||||
|
|
||||||
try expect(try parser.parse()).toThrow(TemplateSyntaxError("Unknown template tag 'unknown'"))
|
try expect(try parser.parse()).toThrow(TemplateSyntaxError("Unknown template tag 'unknown'"))
|
||||||
}
|
}
|
||||||
@@ -2,28 +2,47 @@ import Spectre
|
|||||||
import Stencil
|
import Stencil
|
||||||
|
|
||||||
|
|
||||||
class CustomNode : NodeType {
|
fileprivate class CustomNode : NodeType {
|
||||||
func render(context:Context) throws -> String {
|
func render(_ context:Context) throws -> String {
|
||||||
return "Hello World"
|
return "Hello World"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
fileprivate struct Article {
|
||||||
|
let title: String
|
||||||
|
let author: String
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
func testStencil() {
|
func testStencil() {
|
||||||
describe("Stencil") {
|
describe("Stencil") {
|
||||||
|
let exampleExtension = Extension()
|
||||||
|
|
||||||
|
exampleExtension.registerSimpleTag("simpletag") { context in
|
||||||
|
return "Hello World"
|
||||||
|
}
|
||||||
|
|
||||||
|
exampleExtension.registerTag("customtag") { parser, token in
|
||||||
|
return CustomNode()
|
||||||
|
}
|
||||||
|
|
||||||
|
let environment = Environment(extensions: [exampleExtension])
|
||||||
|
|
||||||
$0.it("can render the README example") {
|
$0.it("can render the README example") {
|
||||||
|
|
||||||
let templateString = "There are {{ articles.count }} articles.\n" +
|
let templateString = "There are {{ articles.count }} articles.\n" +
|
||||||
"\n" +
|
"\n" +
|
||||||
"{% for article in articles %}" +
|
"{% for article in articles %}" +
|
||||||
" - {{ article.title }} by {{ article.author }}.\n" +
|
" - {{ article.title }} by {{ article.author }}.\n" +
|
||||||
"{% endfor %}\n"
|
"{% endfor %}\n"
|
||||||
|
|
||||||
let context = Context(dictionary: [
|
let context = [
|
||||||
"articles": [
|
"articles": [
|
||||||
[ "title": "Migrating from OCUnit to XCTest", "author": "Kyle Fuller" ],
|
Article(title: "Migrating from OCUnit to XCTest", author: "Kyle Fuller"),
|
||||||
[ "title": "Memory Management with ARC", "author": "Kyle Fuller" ],
|
Article(title: "Memory Management with ARC", author: "Kyle Fuller"),
|
||||||
]
|
]
|
||||||
])
|
]
|
||||||
|
|
||||||
let template = Template(templateString: templateString)
|
let template = Template(templateString: templateString)
|
||||||
let result = try template.render(context)
|
let result = try template.render(context)
|
||||||
@@ -38,28 +57,13 @@ func testStencil() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
$0.it("can render a custom template tag") {
|
$0.it("can render a custom template tag") {
|
||||||
let templateString = "{% custom %}"
|
let result = try environment.renderTemplate(string: "{% customtag %}")
|
||||||
let template = Template(templateString: templateString)
|
|
||||||
|
|
||||||
let namespace = Namespace()
|
|
||||||
namespace.registerTag("custom") { parser, token in
|
|
||||||
return CustomNode()
|
|
||||||
}
|
|
||||||
|
|
||||||
let result = try template.render(Context(namespace: namespace))
|
|
||||||
try expect(result) == "Hello World"
|
try expect(result) == "Hello World"
|
||||||
}
|
}
|
||||||
|
|
||||||
$0.it("can render a simple custom tag") {
|
$0.it("can render a simple custom tag") {
|
||||||
let templateString = "{% custom %}"
|
let result = try environment.renderTemplate(string: "{% simpletag %}")
|
||||||
let template = Template(templateString: templateString)
|
try expect(result) == "Hello World"
|
||||||
|
|
||||||
let namespace = Namespace()
|
|
||||||
namespace.registerSimpleTag("custom") { context in
|
|
||||||
return "Hello World"
|
|
||||||
}
|
|
||||||
|
|
||||||
try expect(try template.render(Context(namespace: namespace))) == "Hello World"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
19
Tests/StencilTests/TemplateSpec.swift
Normal file
19
Tests/StencilTests/TemplateSpec.swift
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
import Spectre
|
||||||
|
import Stencil
|
||||||
|
|
||||||
|
|
||||||
|
func testTemplate() {
|
||||||
|
describe("Template") {
|
||||||
|
$0.it("can render a template from a string") {
|
||||||
|
let template = Template(templateString: "Hello World")
|
||||||
|
let result = try template.render([ "name": "Kyle" ])
|
||||||
|
try expect(result) == "Hello World"
|
||||||
|
}
|
||||||
|
|
||||||
|
$0.it("can render a template from a string literal") {
|
||||||
|
let template: Template = "Hello World"
|
||||||
|
let result = try template.render([ "name": "Kyle" ])
|
||||||
|
try expect(result) == "Hello World"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -5,7 +5,7 @@ import Stencil
|
|||||||
func testToken() {
|
func testToken() {
|
||||||
describe("Token") {
|
describe("Token") {
|
||||||
$0.it("can split the contents into components") {
|
$0.it("can split the contents into components") {
|
||||||
let token = Token.Text(value: "hello world")
|
let token = Token.text(value: "hello world")
|
||||||
let components = token.components()
|
let components = token.components()
|
||||||
|
|
||||||
try expect(components.count) == 2
|
try expect(components.count) == 2
|
||||||
@@ -14,7 +14,7 @@ func testToken() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
$0.it("can split the contents into components with single quoted strings") {
|
$0.it("can split the contents into components with single quoted strings") {
|
||||||
let token = Token.Text(value: "hello 'kyle fuller'")
|
let token = Token.text(value: "hello 'kyle fuller'")
|
||||||
let components = token.components()
|
let components = token.components()
|
||||||
|
|
||||||
try expect(components.count) == 2
|
try expect(components.count) == 2
|
||||||
@@ -23,7 +23,7 @@ func testToken() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
$0.it("can split the contents into components with double quoted strings") {
|
$0.it("can split the contents into components with double quoted strings") {
|
||||||
let token = Token.Text(value: "hello \"kyle fuller\"")
|
let token = Token.text(value: "hello \"kyle fuller\"")
|
||||||
let components = token.components()
|
let components = token.components()
|
||||||
|
|
||||||
try expect(components.count) == 2
|
try expect(components.count) == 2
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
import Foundation
|
import Foundation
|
||||||
import Spectre
|
import Spectre
|
||||||
import Stencil
|
@testable import Stencil
|
||||||
|
|
||||||
|
|
||||||
#if os(OSX)
|
#if os(OSX)
|
||||||
@@ -9,6 +9,14 @@ import Stencil
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
fileprivate struct Person {
|
||||||
|
let name: String
|
||||||
|
}
|
||||||
|
|
||||||
|
fileprivate struct Article {
|
||||||
|
let author: Person
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
func testVariable() {
|
func testVariable() {
|
||||||
describe("Variable") {
|
describe("Variable") {
|
||||||
@@ -18,6 +26,7 @@ func testVariable() {
|
|||||||
"profiles": [
|
"profiles": [
|
||||||
"github": "kylef",
|
"github": "kylef",
|
||||||
],
|
],
|
||||||
|
"article": Article(author: Person(name: "Kyle"))
|
||||||
])
|
])
|
||||||
|
|
||||||
#if os(OSX)
|
#if os(OSX)
|
||||||
@@ -36,6 +45,18 @@ func testVariable() {
|
|||||||
try expect(result) == "name"
|
try expect(result) == "name"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$0.it("can resolve an integer literal") {
|
||||||
|
let variable = Variable("5")
|
||||||
|
let result = try variable.resolve(context) as? Number
|
||||||
|
try expect(result) == 5
|
||||||
|
}
|
||||||
|
|
||||||
|
$0.it("can resolve an float literal") {
|
||||||
|
let variable = Variable("3.14")
|
||||||
|
let result = try variable.resolve(context) as? Number
|
||||||
|
try expect(result) == 3.14
|
||||||
|
}
|
||||||
|
|
||||||
$0.it("can resolve a string variable") {
|
$0.it("can resolve a string variable") {
|
||||||
let variable = Variable("name")
|
let variable = Variable("name")
|
||||||
let result = try variable.resolve(context) as? String
|
let result = try variable.resolve(context) as? String
|
||||||
@@ -52,6 +73,20 @@ func testVariable() {
|
|||||||
let variable = Variable("contacts.0")
|
let variable = Variable("contacts.0")
|
||||||
let result = try variable.resolve(context) as? String
|
let result = try variable.resolve(context) as? String
|
||||||
try expect(result) == "Katie"
|
try expect(result) == "Katie"
|
||||||
|
|
||||||
|
let variable1 = Variable("contacts.1")
|
||||||
|
let result1 = try variable1.resolve(context) as? String
|
||||||
|
try expect(result1) == "Carlton"
|
||||||
|
}
|
||||||
|
|
||||||
|
$0.it("can resolve an item from an array via unknown index") {
|
||||||
|
let variable = Variable("contacts.5")
|
||||||
|
let result = try variable.resolve(context) as? String
|
||||||
|
try expect(result).to.beNil()
|
||||||
|
|
||||||
|
let variable1 = Variable("contacts.-5")
|
||||||
|
let result1 = try variable1.resolve(context) as? String
|
||||||
|
try expect(result1).to.beNil()
|
||||||
}
|
}
|
||||||
|
|
||||||
$0.it("can resolve the first item from an array") {
|
$0.it("can resolve the first item from an array") {
|
||||||
@@ -66,6 +101,12 @@ func testVariable() {
|
|||||||
try expect(result) == "Carlton"
|
try expect(result) == "Carlton"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$0.it("can resolve a property with reflection") {
|
||||||
|
let variable = Variable("article.author.name")
|
||||||
|
let result = try variable.resolve(context) as? String
|
||||||
|
try expect(result) == "Kyle"
|
||||||
|
}
|
||||||
|
|
||||||
#if os(OSX)
|
#if os(OSX)
|
||||||
$0.it("can resolve a value via KVO") {
|
$0.it("can resolve a value via KVO") {
|
||||||
let variable = Variable("object.title")
|
let variable = Variable("object.title")
|
||||||
30
Tests/StencilTests/XCTest.swift
Normal file
30
Tests/StencilTests/XCTest.swift
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
import XCTest
|
||||||
|
|
||||||
|
|
||||||
|
public func stencilTests() {
|
||||||
|
testContext()
|
||||||
|
testFilter()
|
||||||
|
testLexer()
|
||||||
|
testToken()
|
||||||
|
testTokenParser()
|
||||||
|
testTemplateLoader()
|
||||||
|
testTemplate()
|
||||||
|
testVariable()
|
||||||
|
testNode()
|
||||||
|
testForNode()
|
||||||
|
testExpressions()
|
||||||
|
testIfNode()
|
||||||
|
testNowNode()
|
||||||
|
testInclude()
|
||||||
|
testInheritence()
|
||||||
|
testFilterTag()
|
||||||
|
testEnvironment()
|
||||||
|
testStencil()
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
class StencilTests: XCTestCase {
|
||||||
|
func testRunStencilTests() {
|
||||||
|
stencilTests()
|
||||||
|
}
|
||||||
|
}
|
||||||
2
Tests/StencilTests/fixtures/child-child.html
Normal file
2
Tests/StencilTests/fixtures/child-child.html
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
{% extends "child.html" %}
|
||||||
|
{% block header %}Child Child Header{% endblock %}
|
||||||
3
Tests/StencilTests/fixtures/child-super.html
Normal file
3
Tests/StencilTests/fixtures/child-super.html
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
{% extends "base.html" %}
|
||||||
|
{% block body %}Child {{ block.super }}{% endblock %}
|
||||||
|
|
||||||
@@ -1,17 +0,0 @@
|
|||||||
import Spectre
|
|
||||||
import Stencil
|
|
||||||
import PathKit
|
|
||||||
|
|
||||||
|
|
||||||
func testInheritence() {
|
|
||||||
describe("Inheritence") {
|
|
||||||
let path = Path(__FILE__) + ".." + ".." + "fixtures"
|
|
||||||
let loader = TemplateLoader(paths: [path])
|
|
||||||
|
|
||||||
$0.it("can inherit from another template") {
|
|
||||||
let context = Context(dictionary: ["loader": loader])
|
|
||||||
let template = loader.loadTemplate("child.html")
|
|
||||||
try expect(try template?.render(context)) == "Header\nChild"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,25 +0,0 @@
|
|||||||
import Spectre
|
|
||||||
import Stencil
|
|
||||||
import PathKit
|
|
||||||
|
|
||||||
|
|
||||||
func testTemplateLoader() {
|
|
||||||
describe("TemplateLoader") {
|
|
||||||
let path = Path(__FILE__) + ".." + ".." + "Tests" + "fixtures"
|
|
||||||
let loader = TemplateLoader(paths: [path])
|
|
||||||
|
|
||||||
$0.it("returns nil when a template cannot be found") {
|
|
||||||
try expect(loader.loadTemplate("unknown.html")).to.beNil()
|
|
||||||
}
|
|
||||||
|
|
||||||
$0.it("returns nil when an array of templates cannot be found") {
|
|
||||||
try expect(loader.loadTemplate(["unknown.html", "unknown2.html"])).to.beNil()
|
|
||||||
}
|
|
||||||
|
|
||||||
$0.it("can load a template from a file") {
|
|
||||||
if loader.loadTemplate("test.html") == nil {
|
|
||||||
throw failure("didn't find the template")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,14 +0,0 @@
|
|||||||
import Spectre
|
|
||||||
import Stencil
|
|
||||||
|
|
||||||
|
|
||||||
func testTemplate() {
|
|
||||||
describe("Template") {
|
|
||||||
$0.it("can render a template from a string") {
|
|
||||||
let context = Context(dictionary: [ "name": "Kyle" ])
|
|
||||||
let template = Template(templateString: "Hello World")
|
|
||||||
let result = try template.render(context)
|
|
||||||
try expect(result) == "Hello World"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,15 +0,0 @@
|
|||||||
testContext()
|
|
||||||
testFilter()
|
|
||||||
testLexer()
|
|
||||||
testToken()
|
|
||||||
testTokenParser()
|
|
||||||
testTemplateLoader()
|
|
||||||
testTemplate()
|
|
||||||
testVariable()
|
|
||||||
testNode()
|
|
||||||
testForNode()
|
|
||||||
testIfNode()
|
|
||||||
testNowNode()
|
|
||||||
testInclude()
|
|
||||||
testInheritence()
|
|
||||||
testStencil()
|
|
||||||
225
docs/Makefile
Normal file
225
docs/Makefile
Normal file
@@ -0,0 +1,225 @@
|
|||||||
|
# Makefile for Sphinx documentation
|
||||||
|
#
|
||||||
|
|
||||||
|
# You can set these variables from the command line.
|
||||||
|
SPHINXOPTS =
|
||||||
|
SPHINXBUILD = sphinx-build
|
||||||
|
PAPER =
|
||||||
|
BUILDDIR = _build
|
||||||
|
|
||||||
|
# Internal variables.
|
||||||
|
PAPEROPT_a4 = -D latex_paper_size=a4
|
||||||
|
PAPEROPT_letter = -D latex_paper_size=letter
|
||||||
|
ALLSPHINXOPTS = -d $(BUILDDIR)/doctrees $(PAPEROPT_$(PAPER)) $(SPHINXOPTS) .
|
||||||
|
# the i18n builder cannot share the environment and doctrees with the others
|
||||||
|
I18NSPHINXOPTS = $(PAPEROPT_$(PAPER)) $(SPHINXOPTS) .
|
||||||
|
|
||||||
|
.PHONY: help
|
||||||
|
help:
|
||||||
|
@echo "Please use \`make <target>' where <target> is one of"
|
||||||
|
@echo " html to make standalone HTML files"
|
||||||
|
@echo " dirhtml to make HTML files named index.html in directories"
|
||||||
|
@echo " singlehtml to make a single large HTML file"
|
||||||
|
@echo " pickle to make pickle files"
|
||||||
|
@echo " json to make JSON files"
|
||||||
|
@echo " htmlhelp to make HTML files and a HTML help project"
|
||||||
|
@echo " qthelp to make HTML files and a qthelp project"
|
||||||
|
@echo " applehelp to make an Apple Help Book"
|
||||||
|
@echo " devhelp to make HTML files and a Devhelp project"
|
||||||
|
@echo " epub to make an epub"
|
||||||
|
@echo " epub3 to make an epub3"
|
||||||
|
@echo " latex to make LaTeX files, you can set PAPER=a4 or PAPER=letter"
|
||||||
|
@echo " latexpdf to make LaTeX files and run them through pdflatex"
|
||||||
|
@echo " latexpdfja to make LaTeX files and run them through platex/dvipdfmx"
|
||||||
|
@echo " text to make text files"
|
||||||
|
@echo " man to make manual pages"
|
||||||
|
@echo " texinfo to make Texinfo files"
|
||||||
|
@echo " info to make Texinfo files and run them through makeinfo"
|
||||||
|
@echo " gettext to make PO message catalogs"
|
||||||
|
@echo " changes to make an overview of all changed/added/deprecated items"
|
||||||
|
@echo " xml to make Docutils-native XML files"
|
||||||
|
@echo " pseudoxml to make pseudoxml-XML files for display purposes"
|
||||||
|
@echo " linkcheck to check all external links for integrity"
|
||||||
|
@echo " doctest to run all doctests embedded in the documentation (if enabled)"
|
||||||
|
@echo " coverage to run coverage check of the documentation (if enabled)"
|
||||||
|
@echo " dummy to check syntax errors of document sources"
|
||||||
|
|
||||||
|
.PHONY: clean
|
||||||
|
clean:
|
||||||
|
rm -rf $(BUILDDIR)/*
|
||||||
|
|
||||||
|
.PHONY: html
|
||||||
|
html:
|
||||||
|
$(SPHINXBUILD) -b html $(ALLSPHINXOPTS) $(BUILDDIR)/html
|
||||||
|
@echo
|
||||||
|
@echo "Build finished. The HTML pages are in $(BUILDDIR)/html."
|
||||||
|
|
||||||
|
.PHONY: dirhtml
|
||||||
|
dirhtml:
|
||||||
|
$(SPHINXBUILD) -b dirhtml $(ALLSPHINXOPTS) $(BUILDDIR)/dirhtml
|
||||||
|
@echo
|
||||||
|
@echo "Build finished. The HTML pages are in $(BUILDDIR)/dirhtml."
|
||||||
|
|
||||||
|
.PHONY: singlehtml
|
||||||
|
singlehtml:
|
||||||
|
$(SPHINXBUILD) -b singlehtml $(ALLSPHINXOPTS) $(BUILDDIR)/singlehtml
|
||||||
|
@echo
|
||||||
|
@echo "Build finished. The HTML page is in $(BUILDDIR)/singlehtml."
|
||||||
|
|
||||||
|
.PHONY: pickle
|
||||||
|
pickle:
|
||||||
|
$(SPHINXBUILD) -b pickle $(ALLSPHINXOPTS) $(BUILDDIR)/pickle
|
||||||
|
@echo
|
||||||
|
@echo "Build finished; now you can process the pickle files."
|
||||||
|
|
||||||
|
.PHONY: json
|
||||||
|
json:
|
||||||
|
$(SPHINXBUILD) -b json $(ALLSPHINXOPTS) $(BUILDDIR)/json
|
||||||
|
@echo
|
||||||
|
@echo "Build finished; now you can process the JSON files."
|
||||||
|
|
||||||
|
.PHONY: htmlhelp
|
||||||
|
htmlhelp:
|
||||||
|
$(SPHINXBUILD) -b htmlhelp $(ALLSPHINXOPTS) $(BUILDDIR)/htmlhelp
|
||||||
|
@echo
|
||||||
|
@echo "Build finished; now you can run HTML Help Workshop with the" \
|
||||||
|
".hhp project file in $(BUILDDIR)/htmlhelp."
|
||||||
|
|
||||||
|
.PHONY: qthelp
|
||||||
|
qthelp:
|
||||||
|
$(SPHINXBUILD) -b qthelp $(ALLSPHINXOPTS) $(BUILDDIR)/qthelp
|
||||||
|
@echo
|
||||||
|
@echo "Build finished; now you can run "qcollectiongenerator" with the" \
|
||||||
|
".qhcp project file in $(BUILDDIR)/qthelp, like this:"
|
||||||
|
@echo "# qcollectiongenerator $(BUILDDIR)/qthelp/Stencil.qhcp"
|
||||||
|
@echo "To view the help file:"
|
||||||
|
@echo "# assistant -collectionFile $(BUILDDIR)/qthelp/Stencil.qhc"
|
||||||
|
|
||||||
|
.PHONY: applehelp
|
||||||
|
applehelp:
|
||||||
|
$(SPHINXBUILD) -b applehelp $(ALLSPHINXOPTS) $(BUILDDIR)/applehelp
|
||||||
|
@echo
|
||||||
|
@echo "Build finished. The help book is in $(BUILDDIR)/applehelp."
|
||||||
|
@echo "N.B. You won't be able to view it unless you put it in" \
|
||||||
|
"~/Library/Documentation/Help or install it in your application" \
|
||||||
|
"bundle."
|
||||||
|
|
||||||
|
.PHONY: devhelp
|
||||||
|
devhelp:
|
||||||
|
$(SPHINXBUILD) -b devhelp $(ALLSPHINXOPTS) $(BUILDDIR)/devhelp
|
||||||
|
@echo
|
||||||
|
@echo "Build finished."
|
||||||
|
@echo "To view the help file:"
|
||||||
|
@echo "# mkdir -p $$HOME/.local/share/devhelp/Stencil"
|
||||||
|
@echo "# ln -s $(BUILDDIR)/devhelp $$HOME/.local/share/devhelp/Stencil"
|
||||||
|
@echo "# devhelp"
|
||||||
|
|
||||||
|
.PHONY: epub
|
||||||
|
epub:
|
||||||
|
$(SPHINXBUILD) -b epub $(ALLSPHINXOPTS) $(BUILDDIR)/epub
|
||||||
|
@echo
|
||||||
|
@echo "Build finished. The epub file is in $(BUILDDIR)/epub."
|
||||||
|
|
||||||
|
.PHONY: epub3
|
||||||
|
epub3:
|
||||||
|
$(SPHINXBUILD) -b epub3 $(ALLSPHINXOPTS) $(BUILDDIR)/epub3
|
||||||
|
@echo
|
||||||
|
@echo "Build finished. The epub3 file is in $(BUILDDIR)/epub3."
|
||||||
|
|
||||||
|
.PHONY: latex
|
||||||
|
latex:
|
||||||
|
$(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex
|
||||||
|
@echo
|
||||||
|
@echo "Build finished; the LaTeX files are in $(BUILDDIR)/latex."
|
||||||
|
@echo "Run \`make' in that directory to run these through (pdf)latex" \
|
||||||
|
"(use \`make latexpdf' here to do that automatically)."
|
||||||
|
|
||||||
|
.PHONY: latexpdf
|
||||||
|
latexpdf:
|
||||||
|
$(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex
|
||||||
|
@echo "Running LaTeX files through pdflatex..."
|
||||||
|
$(MAKE) -C $(BUILDDIR)/latex all-pdf
|
||||||
|
@echo "pdflatex finished; the PDF files are in $(BUILDDIR)/latex."
|
||||||
|
|
||||||
|
.PHONY: latexpdfja
|
||||||
|
latexpdfja:
|
||||||
|
$(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex
|
||||||
|
@echo "Running LaTeX files through platex and dvipdfmx..."
|
||||||
|
$(MAKE) -C $(BUILDDIR)/latex all-pdf-ja
|
||||||
|
@echo "pdflatex finished; the PDF files are in $(BUILDDIR)/latex."
|
||||||
|
|
||||||
|
.PHONY: text
|
||||||
|
text:
|
||||||
|
$(SPHINXBUILD) -b text $(ALLSPHINXOPTS) $(BUILDDIR)/text
|
||||||
|
@echo
|
||||||
|
@echo "Build finished. The text files are in $(BUILDDIR)/text."
|
||||||
|
|
||||||
|
.PHONY: man
|
||||||
|
man:
|
||||||
|
$(SPHINXBUILD) -b man $(ALLSPHINXOPTS) $(BUILDDIR)/man
|
||||||
|
@echo
|
||||||
|
@echo "Build finished. The manual pages are in $(BUILDDIR)/man."
|
||||||
|
|
||||||
|
.PHONY: texinfo
|
||||||
|
texinfo:
|
||||||
|
$(SPHINXBUILD) -b texinfo $(ALLSPHINXOPTS) $(BUILDDIR)/texinfo
|
||||||
|
@echo
|
||||||
|
@echo "Build finished. The Texinfo files are in $(BUILDDIR)/texinfo."
|
||||||
|
@echo "Run \`make' in that directory to run these through makeinfo" \
|
||||||
|
"(use \`make info' here to do that automatically)."
|
||||||
|
|
||||||
|
.PHONY: info
|
||||||
|
info:
|
||||||
|
$(SPHINXBUILD) -b texinfo $(ALLSPHINXOPTS) $(BUILDDIR)/texinfo
|
||||||
|
@echo "Running Texinfo files through makeinfo..."
|
||||||
|
make -C $(BUILDDIR)/texinfo info
|
||||||
|
@echo "makeinfo finished; the Info files are in $(BUILDDIR)/texinfo."
|
||||||
|
|
||||||
|
.PHONY: gettext
|
||||||
|
gettext:
|
||||||
|
$(SPHINXBUILD) -b gettext $(I18NSPHINXOPTS) $(BUILDDIR)/locale
|
||||||
|
@echo
|
||||||
|
@echo "Build finished. The message catalogs are in $(BUILDDIR)/locale."
|
||||||
|
|
||||||
|
.PHONY: changes
|
||||||
|
changes:
|
||||||
|
$(SPHINXBUILD) -b changes $(ALLSPHINXOPTS) $(BUILDDIR)/changes
|
||||||
|
@echo
|
||||||
|
@echo "The overview file is in $(BUILDDIR)/changes."
|
||||||
|
|
||||||
|
.PHONY: linkcheck
|
||||||
|
linkcheck:
|
||||||
|
$(SPHINXBUILD) -b linkcheck $(ALLSPHINXOPTS) $(BUILDDIR)/linkcheck
|
||||||
|
@echo
|
||||||
|
@echo "Link check complete; look for any errors in the above output " \
|
||||||
|
"or in $(BUILDDIR)/linkcheck/output.txt."
|
||||||
|
|
||||||
|
.PHONY: doctest
|
||||||
|
doctest:
|
||||||
|
$(SPHINXBUILD) -b doctest $(ALLSPHINXOPTS) $(BUILDDIR)/doctest
|
||||||
|
@echo "Testing of doctests in the sources finished, look at the " \
|
||||||
|
"results in $(BUILDDIR)/doctest/output.txt."
|
||||||
|
|
||||||
|
.PHONY: coverage
|
||||||
|
coverage:
|
||||||
|
$(SPHINXBUILD) -b coverage $(ALLSPHINXOPTS) $(BUILDDIR)/coverage
|
||||||
|
@echo "Testing of coverage in the sources finished, look at the " \
|
||||||
|
"results in $(BUILDDIR)/coverage/python.txt."
|
||||||
|
|
||||||
|
.PHONY: xml
|
||||||
|
xml:
|
||||||
|
$(SPHINXBUILD) -b xml $(ALLSPHINXOPTS) $(BUILDDIR)/xml
|
||||||
|
@echo
|
||||||
|
@echo "Build finished. The XML files are in $(BUILDDIR)/xml."
|
||||||
|
|
||||||
|
.PHONY: pseudoxml
|
||||||
|
pseudoxml:
|
||||||
|
$(SPHINXBUILD) -b pseudoxml $(ALLSPHINXOPTS) $(BUILDDIR)/pseudoxml
|
||||||
|
@echo
|
||||||
|
@echo "Build finished. The pseudo-XML files are in $(BUILDDIR)/pseudoxml."
|
||||||
|
|
||||||
|
.PHONY: dummy
|
||||||
|
dummy:
|
||||||
|
$(SPHINXBUILD) -b dummy $(ALLSPHINXOPTS) $(BUILDDIR)/dummy
|
||||||
|
@echo
|
||||||
|
@echo "Build finished. Dummy builder generates no files."
|
||||||
33
docs/_templates/sidebar_intro.html
vendored
Normal file
33
docs/_templates/sidebar_intro.html
vendored
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
<h1><a href="/">Stencil</a></h1>
|
||||||
|
|
||||||
|
<p>
|
||||||
|
<iframe
|
||||||
|
src="https://ghbtns.com/github-btn.html?user=kylef&repo=Stencil&type=watch&count=true&size=large"
|
||||||
|
allowtransparency="true" frameborder="0" scrolling="0" width="200px" height="35px">
|
||||||
|
</iframe>
|
||||||
|
</p>
|
||||||
|
|
||||||
|
<p>Stencil is a simple and powerful template language for Swift.</p>
|
||||||
|
|
||||||
|
<div class="social">
|
||||||
|
<p>
|
||||||
|
<iframe
|
||||||
|
src="https://ghbtns.com/github-btn.html?user=kylef&type=follow&count=false"
|
||||||
|
allowtransparency="true" frameborder="0" scrolling="0" width="200" height="20">
|
||||||
|
</iframe>
|
||||||
|
</p>
|
||||||
|
|
||||||
|
<p>
|
||||||
|
<a href="https://twitter.com/kylefuller" class="twitter-follow-button" data-show-count="false">Follow @kylefuller</a> <script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+'://platform.twitter.com/widgets.js';fjs.parentNode.insertBefore(js,fjs);}}(document, 'script', 'twitter-wjs');</script>
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<h3>Other Projects</h3>
|
||||||
|
|
||||||
|
<p>More <a href="https://fuller.li/">Kyle Fuller</a> projects:</p>
|
||||||
|
<ul>
|
||||||
|
<li><a href="https://github.com/kylef/Commander">Commander</a></li>
|
||||||
|
<li><a href="https://curassow.fuller.li/">Curassow</a></li>
|
||||||
|
<li><a href="https://github.com/kylef/Spectre">Spectre</a></li>
|
||||||
|
<li><a href="https://github.com/kylef/heroku-buildpack-swift">Heroku Swift buildpack</a></li>
|
||||||
|
</ul>
|
||||||
140
docs/api.rst
Normal file
140
docs/api.rst
Normal file
@@ -0,0 +1,140 @@
|
|||||||
|
Template API
|
||||||
|
============
|
||||||
|
|
||||||
|
This document describes Stencils Swift API, and not the Swift template language.
|
||||||
|
|
||||||
|
.. contents:: :depth: 2
|
||||||
|
|
||||||
|
Environment
|
||||||
|
-----------
|
||||||
|
|
||||||
|
An environment contains shared configuration such as custom filters and tags
|
||||||
|
along with template loaders.
|
||||||
|
|
||||||
|
.. code-block:: swift
|
||||||
|
|
||||||
|
let environment = Environment()
|
||||||
|
|
||||||
|
You can optionally provide a loader or extensions when creating an environment:
|
||||||
|
|
||||||
|
.. code-block:: swift
|
||||||
|
|
||||||
|
let environment = Environment(loader: ..., extensions: [...])
|
||||||
|
|
||||||
|
Rendering a Template
|
||||||
|
~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
|
Environment provides convinience methods to render a template either from a
|
||||||
|
string or a template loader.
|
||||||
|
|
||||||
|
.. code-block:: swift
|
||||||
|
|
||||||
|
let template = "Hello {{ name }}"
|
||||||
|
let context = ["name": "Kyle"]
|
||||||
|
let rendered = environment.renderTemplate(string: template, context: context)
|
||||||
|
|
||||||
|
Rendering a template from the configured loader:
|
||||||
|
|
||||||
|
.. code-block:: swift
|
||||||
|
|
||||||
|
let context = ["name": "Kyle"]
|
||||||
|
let rendered = environment.renderTemplate(name: "example.html", context: context)
|
||||||
|
|
||||||
|
Loading a Template
|
||||||
|
~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
|
Environment provides an API to load a template from the configured loader.
|
||||||
|
|
||||||
|
.. code-block:: swift
|
||||||
|
|
||||||
|
let template = try environment.loadTemplate(name: "example.html")
|
||||||
|
|
||||||
|
Loader
|
||||||
|
------
|
||||||
|
|
||||||
|
Loaders are responsible for loading templates from a resource such as the file
|
||||||
|
system.
|
||||||
|
|
||||||
|
Stencil provides a ``FileSytemLoader`` which allows you to load a template
|
||||||
|
directly from the file system.
|
||||||
|
|
||||||
|
FileSystemLoader
|
||||||
|
~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
|
Loads templates from the file system. This loader can find templates in folders
|
||||||
|
on the file system.
|
||||||
|
|
||||||
|
.. code-block:: swift
|
||||||
|
|
||||||
|
FileSystemLoader(paths: ["./templates"])
|
||||||
|
|
||||||
|
.. code-block:: swift
|
||||||
|
|
||||||
|
FileSystemLoader(bundle: [Bundle.main])
|
||||||
|
|
||||||
|
|
||||||
|
Custom Loaders
|
||||||
|
~~~~~~~~~~~~~~
|
||||||
|
|
||||||
|
``Loader`` is a protocol, so you can implement your own compatible loaders. You
|
||||||
|
will need to implement a ``loadTemplate`` method to load the template,
|
||||||
|
throwing a ``TemplateDoesNotExist`` when the template is not found.
|
||||||
|
|
||||||
|
.. code-block:: swift
|
||||||
|
|
||||||
|
class ExampleMemoryLoader: Loader {
|
||||||
|
func loadTemplate(name: String, environment: Environment) throws -> Template {
|
||||||
|
if name == "index.html" {
|
||||||
|
return Template(templateString: "Hello", environment: environment)
|
||||||
|
}
|
||||||
|
|
||||||
|
throw TemplateDoesNotExist(name: name, loader: self)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Context
|
||||||
|
-------
|
||||||
|
|
||||||
|
A ``Context`` is a structure containing any templates you would like to use in
|
||||||
|
a template. It’s somewhat like a dictionary, however you can push and pop to
|
||||||
|
scope variables. So that means that when iterating over a for loop, you can
|
||||||
|
push a new scope into the context to store any variables local to the scope.
|
||||||
|
|
||||||
|
You would normally only access the ``Context`` within a custom template tag or
|
||||||
|
filter.
|
||||||
|
|
||||||
|
Subscripting
|
||||||
|
~~~~~~~~~~~~
|
||||||
|
|
||||||
|
You can use subscripting to get and set values from the context.
|
||||||
|
|
||||||
|
.. code-block:: swift
|
||||||
|
|
||||||
|
context["key"] = value
|
||||||
|
let value = context["key"]
|
||||||
|
|
||||||
|
``push()``
|
||||||
|
~~~~~~~~~~
|
||||||
|
|
||||||
|
A ``Context`` is a stack. You can push a new level onto the ``Context`` so that
|
||||||
|
modifications can easily be poped off. This is useful for isolating mutations
|
||||||
|
into scope of a template tag. Such as ``{% if %}`` and ``{% for %}`` tags.
|
||||||
|
|
||||||
|
.. code-block:: swift
|
||||||
|
|
||||||
|
context.push(["name": "example"]) {
|
||||||
|
// context contains name which is `example`.
|
||||||
|
}
|
||||||
|
|
||||||
|
// name is popped off the context after the duration of the closure.
|
||||||
|
|
||||||
|
``flatten()``
|
||||||
|
~~~~~~~~~~~~~
|
||||||
|
|
||||||
|
Using ``flatten()`` method you can get whole ``Context`` stack as one
|
||||||
|
dictionary including all variables.
|
||||||
|
|
||||||
|
.. code-block:: swift
|
||||||
|
|
||||||
|
let dictionary = context.flatten()
|
||||||
311
docs/builtins.rst
Normal file
311
docs/builtins.rst
Normal file
@@ -0,0 +1,311 @@
|
|||||||
|
Built-in template tags and filters
|
||||||
|
==================================
|
||||||
|
|
||||||
|
.. _built-in-tags:
|
||||||
|
|
||||||
|
Built-in Tags
|
||||||
|
-------------
|
||||||
|
|
||||||
|
``for``
|
||||||
|
~~~~~~~
|
||||||
|
|
||||||
|
A for loop allows you to iterate over an array found by variable lookup.
|
||||||
|
|
||||||
|
.. code-block:: html+django
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
{% for user in users %}
|
||||||
|
<li>{{ user }}</li>
|
||||||
|
{% endfor %}
|
||||||
|
</ul>
|
||||||
|
|
||||||
|
The ``for`` tag can iterate over dictionaries.
|
||||||
|
|
||||||
|
.. code-block:: html+django
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
{% for key, value in dict %}
|
||||||
|
<li>{{ key }}: {{ value }}</li>
|
||||||
|
{% endfor %}
|
||||||
|
</ul>
|
||||||
|
|
||||||
|
The ``for`` tag can contain optional ``where`` expression to filter out
|
||||||
|
elements on which this expression evaluates to false.
|
||||||
|
|
||||||
|
.. code-block:: html+django
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
{% for user in users where user.name != "Kyle" %}
|
||||||
|
<li>{{ user }}</li>
|
||||||
|
{% endfor %}
|
||||||
|
</ul>
|
||||||
|
|
||||||
|
The ``for`` tag can take an optional ``{% empty %}`` block that will be
|
||||||
|
displayed if the given list is empty or could not be found.
|
||||||
|
|
||||||
|
.. code-block:: html+django
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
{% for user in users %}
|
||||||
|
<li>{{ user }}</li>
|
||||||
|
{% empty %}
|
||||||
|
<li>There are no users.</li>
|
||||||
|
{% endfor %}
|
||||||
|
</ul>
|
||||||
|
|
||||||
|
The for block sets a few variables available within the loop:
|
||||||
|
|
||||||
|
- ``first`` - True if this is the first time through the loop
|
||||||
|
- ``last`` - True if this is the last time through the loop
|
||||||
|
- ``counter`` - The current iteration of the loop
|
||||||
|
|
||||||
|
``if``
|
||||||
|
~~~~~~
|
||||||
|
|
||||||
|
The ``{% if %}`` tag evaluates a variable, and if that variable evaluates to
|
||||||
|
true the contents of the block are processed. Being true is defined as:
|
||||||
|
|
||||||
|
* Present in the context
|
||||||
|
* Being non-empty (dictionaries or arrays)
|
||||||
|
* Not being a false boolean value
|
||||||
|
* Not being a numerical value of 0 or below
|
||||||
|
* Not being an empty string
|
||||||
|
|
||||||
|
.. code-block:: html+django
|
||||||
|
|
||||||
|
{% if admin %}
|
||||||
|
The user is an administrator.
|
||||||
|
{% elif user %}
|
||||||
|
A user is logged in.
|
||||||
|
{% else %}
|
||||||
|
No user was found.
|
||||||
|
{% endif %}
|
||||||
|
|
||||||
|
Operators
|
||||||
|
^^^^^^^^^
|
||||||
|
|
||||||
|
``if`` tags may combine ``and``, ``or`` and ``not`` to test multiple variables
|
||||||
|
or to negate a variable.
|
||||||
|
|
||||||
|
.. code-block:: html+django
|
||||||
|
|
||||||
|
{% if one and two %}
|
||||||
|
Both one and two evaluate to true.
|
||||||
|
{% endif %}
|
||||||
|
|
||||||
|
{% if not one %}
|
||||||
|
One evaluates to false
|
||||||
|
{% endif %}
|
||||||
|
|
||||||
|
{% if one or two %}
|
||||||
|
Either one or two evaluates to true.
|
||||||
|
{% endif %}
|
||||||
|
|
||||||
|
{% if not one or two %}
|
||||||
|
One does not evaluate to false or two evaluates to true.
|
||||||
|
{% endif %}
|
||||||
|
|
||||||
|
You may use ``and``, ``or`` and ``not`` multiple times together. ``not`` has
|
||||||
|
higest prescidence followed by ``and``. For example:
|
||||||
|
|
||||||
|
.. code-block:: html+django
|
||||||
|
|
||||||
|
{% if one or two and three %}
|
||||||
|
|
||||||
|
Will be treated as:
|
||||||
|
|
||||||
|
.. code-block:: text
|
||||||
|
|
||||||
|
one or (two and three)
|
||||||
|
|
||||||
|
``==`` operator
|
||||||
|
"""""""""""""""
|
||||||
|
|
||||||
|
.. code-block:: html+django
|
||||||
|
|
||||||
|
{% if value == other_value %}
|
||||||
|
value is equal to other_value
|
||||||
|
{% endif %}
|
||||||
|
|
||||||
|
.. note:: The equality operator only supports numerical, string and boolean types.
|
||||||
|
|
||||||
|
``!=`` operator
|
||||||
|
"""""""""""""""
|
||||||
|
|
||||||
|
.. code-block:: html+django
|
||||||
|
|
||||||
|
{% if value != other_value %}
|
||||||
|
value is not equal to other_value
|
||||||
|
{% endif %}
|
||||||
|
|
||||||
|
.. note:: The inequality operator only supports numerical, string and boolean types.
|
||||||
|
|
||||||
|
``<`` operator
|
||||||
|
"""""""""""""""
|
||||||
|
|
||||||
|
.. code-block:: html+django
|
||||||
|
|
||||||
|
{% if value < other_value %}
|
||||||
|
value is less than other_value
|
||||||
|
{% endif %}
|
||||||
|
|
||||||
|
.. note:: The less than operator only supports numerical types.
|
||||||
|
|
||||||
|
``<=`` operator
|
||||||
|
"""""""""""""""
|
||||||
|
|
||||||
|
.. code-block:: html+django
|
||||||
|
|
||||||
|
{% if value <= other_value %}
|
||||||
|
value is less than or equal to other_value
|
||||||
|
{% endif %}
|
||||||
|
|
||||||
|
.. note:: The less than equal operator only supports numerical types.
|
||||||
|
|
||||||
|
``>`` operator
|
||||||
|
"""""""""""""""
|
||||||
|
|
||||||
|
.. code-block:: html+django
|
||||||
|
|
||||||
|
{% if value > other_value %}
|
||||||
|
value is more than other_value
|
||||||
|
{% endif %}
|
||||||
|
|
||||||
|
.. note:: The more than operator only supports numerical types.
|
||||||
|
|
||||||
|
``>=`` operator
|
||||||
|
"""""""""""""""
|
||||||
|
|
||||||
|
.. code-block:: html+django
|
||||||
|
|
||||||
|
{% if value >= other_value %}
|
||||||
|
value is more than or equal to other_value
|
||||||
|
{% endif %}
|
||||||
|
|
||||||
|
.. note:: The more than equal operator only supports numerical types.
|
||||||
|
|
||||||
|
``ifnot``
|
||||||
|
~~~~~~~~~
|
||||||
|
|
||||||
|
.. note:: ``{% ifnot %}`` is deprecated. You should use ``{% if not %}``.
|
||||||
|
|
||||||
|
.. code-block:: html+django
|
||||||
|
|
||||||
|
{% ifnot variable %}
|
||||||
|
The variable was NOT found in the current context.
|
||||||
|
{% else %}
|
||||||
|
The variable was found.
|
||||||
|
{% endif %}
|
||||||
|
|
||||||
|
``now``
|
||||||
|
~~~~~~~
|
||||||
|
|
||||||
|
``filter``
|
||||||
|
~~~~~~~~~~
|
||||||
|
|
||||||
|
Filters the contents of the block.
|
||||||
|
|
||||||
|
.. code-block:: html+django
|
||||||
|
|
||||||
|
{% filter lowercase %}
|
||||||
|
This Text Will Be Lowercased.
|
||||||
|
{% endfilter %}
|
||||||
|
|
||||||
|
You can chain multiple filters with a pipe (`|`).
|
||||||
|
|
||||||
|
.. code-block:: html+django
|
||||||
|
|
||||||
|
{% filter lowercase|capitalize %}
|
||||||
|
This Text Will First Be Lowercased, Then The First Character Will BE
|
||||||
|
Capitalised.
|
||||||
|
{% endfilter %}
|
||||||
|
|
||||||
|
``include``
|
||||||
|
~~~~~~~~~~~
|
||||||
|
|
||||||
|
You can include another template using the `include` tag.
|
||||||
|
|
||||||
|
.. code-block:: html+django
|
||||||
|
|
||||||
|
{% include "comment.html" %}
|
||||||
|
|
||||||
|
The `include` tag requires you to provide a loader which will be used to lookup
|
||||||
|
the template.
|
||||||
|
|
||||||
|
.. code-block:: swift
|
||||||
|
|
||||||
|
let environment = Environment(bundle: [Bundle.main])
|
||||||
|
let template = environment.loadTemplate(name: "index.html")
|
||||||
|
|
||||||
|
``extends``
|
||||||
|
~~~~~~~~~~~
|
||||||
|
|
||||||
|
Extends the template from a parent template.
|
||||||
|
|
||||||
|
.. code-block:: html+django
|
||||||
|
|
||||||
|
{% extends "base.html" %}
|
||||||
|
|
||||||
|
See :ref:`template-inheritance` for more information.
|
||||||
|
|
||||||
|
``block``
|
||||||
|
~~~~~~~~~
|
||||||
|
|
||||||
|
Defines a block that can be overridden by child templates. See
|
||||||
|
:ref:`template-inheritance` for more information.
|
||||||
|
|
||||||
|
.. _built-in-filters:
|
||||||
|
|
||||||
|
Built-in Filters
|
||||||
|
----------------
|
||||||
|
|
||||||
|
``capitalize``
|
||||||
|
~~~~~~~~~~~~~~
|
||||||
|
|
||||||
|
The capitalize filter allows you to capitalize a string.
|
||||||
|
For example, `stencil` to `Stencil`.
|
||||||
|
|
||||||
|
.. code-block:: html+django
|
||||||
|
|
||||||
|
{{ "stencil"|capitalize }}
|
||||||
|
|
||||||
|
``uppercase``
|
||||||
|
~~~~~~~~~~~~~
|
||||||
|
|
||||||
|
The uppercase filter allows you to transform a string to uppercase.
|
||||||
|
For example, `Stencil` to `STENCIL`.
|
||||||
|
|
||||||
|
.. code-block:: html+django
|
||||||
|
|
||||||
|
{{ "Stencil"|uppercase }}
|
||||||
|
|
||||||
|
``lowercase``
|
||||||
|
~~~~~~~~~~~~~
|
||||||
|
|
||||||
|
The uppercase filter allows you to transform a string to lowercase.
|
||||||
|
For example, `Stencil` to `stencil`.
|
||||||
|
|
||||||
|
.. code-block:: html+django
|
||||||
|
|
||||||
|
{{ "Stencil"|lowercase }}
|
||||||
|
|
||||||
|
``default``
|
||||||
|
~~~~~~~~~~~
|
||||||
|
|
||||||
|
If a variable not present in the context, use given default. Otherwise, use the
|
||||||
|
value of the variable. For example:
|
||||||
|
|
||||||
|
.. code-block:: html+django
|
||||||
|
|
||||||
|
Hello {{ name|default:"World" }}
|
||||||
|
|
||||||
|
``join``
|
||||||
|
~~~~~~~~
|
||||||
|
|
||||||
|
Join an array of items.
|
||||||
|
|
||||||
|
.. code-block:: html+django
|
||||||
|
|
||||||
|
{{ value|join:", " }}
|
||||||
|
|
||||||
|
.. note:: The value MUST be an array.
|
||||||
341
docs/conf.py
Normal file
341
docs/conf.py
Normal file
@@ -0,0 +1,341 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
#
|
||||||
|
# Stencil documentation build configuration file, created by
|
||||||
|
# sphinx-quickstart on Sun Nov 27 05:54:36 2016.
|
||||||
|
#
|
||||||
|
# This file is execfile()d with the current directory set to its
|
||||||
|
# containing dir.
|
||||||
|
#
|
||||||
|
# Note that not all possible configuration values are present in this
|
||||||
|
# autogenerated file.
|
||||||
|
#
|
||||||
|
# All configuration values have a default; values that are commented out
|
||||||
|
# serve to show the default.
|
||||||
|
|
||||||
|
# If extensions (or modules to document with autodoc) are in another directory,
|
||||||
|
# add these directories to sys.path here. If the directory is relative to the
|
||||||
|
# documentation root, use os.path.abspath to make it absolute, like shown here.
|
||||||
|
#
|
||||||
|
# import os
|
||||||
|
# import sys
|
||||||
|
# sys.path.insert(0, os.path.abspath('.'))
|
||||||
|
|
||||||
|
# -- General configuration ------------------------------------------------
|
||||||
|
|
||||||
|
# If your documentation needs a minimal Sphinx version, state it here.
|
||||||
|
#
|
||||||
|
# needs_sphinx = '1.0'
|
||||||
|
|
||||||
|
# Add any Sphinx extension module names here, as strings. They can be
|
||||||
|
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
|
||||||
|
# ones.
|
||||||
|
extensions = []
|
||||||
|
|
||||||
|
# Add any paths that contain templates here, relative to this directory.
|
||||||
|
templates_path = ['_templates']
|
||||||
|
|
||||||
|
# The suffix(es) of source filenames.
|
||||||
|
# You can specify multiple suffix as a list of string:
|
||||||
|
#
|
||||||
|
# source_suffix = ['.rst', '.md']
|
||||||
|
source_suffix = '.rst'
|
||||||
|
|
||||||
|
# The encoding of source files.
|
||||||
|
#
|
||||||
|
# source_encoding = 'utf-8-sig'
|
||||||
|
|
||||||
|
# The master toctree document.
|
||||||
|
master_doc = 'index'
|
||||||
|
|
||||||
|
# General information about the project.
|
||||||
|
project = 'Stencil'
|
||||||
|
copyright = '2016, Kyle Fuller'
|
||||||
|
author = 'Kyle Fuller'
|
||||||
|
|
||||||
|
# The version info for the project you're documenting, acts as replacement for
|
||||||
|
# |version| and |release|, also used in various other places throughout the
|
||||||
|
# built documents.
|
||||||
|
#
|
||||||
|
# The short X.Y version.
|
||||||
|
version = '0.7.0'
|
||||||
|
# The full version, including alpha/beta/rc tags.
|
||||||
|
release = '0.7.0'
|
||||||
|
|
||||||
|
# The language for content autogenerated by Sphinx. Refer to documentation
|
||||||
|
# for a list of supported languages.
|
||||||
|
#
|
||||||
|
# This is also used if you do content translation via gettext catalogs.
|
||||||
|
# Usually you set "language" from the command line for these cases.
|
||||||
|
language = None
|
||||||
|
|
||||||
|
# There are two options for replacing |today|: either, you set today to some
|
||||||
|
# non-false value, then it is used:
|
||||||
|
#
|
||||||
|
# today = ''
|
||||||
|
#
|
||||||
|
# Else, today_fmt is used as the format for a strftime call.
|
||||||
|
#
|
||||||
|
# today_fmt = '%B %d, %Y'
|
||||||
|
|
||||||
|
# List of patterns, relative to source directory, that match files and
|
||||||
|
# directories to ignore when looking for source files.
|
||||||
|
# This patterns also effect to html_static_path and html_extra_path
|
||||||
|
exclude_patterns = ['_build', 'Thumbs.db', '.DS_Store']
|
||||||
|
|
||||||
|
# The reST default role (used for this markup: `text`) to use for all
|
||||||
|
# documents.
|
||||||
|
#
|
||||||
|
# default_role = None
|
||||||
|
|
||||||
|
# If true, '()' will be appended to :func: etc. cross-reference text.
|
||||||
|
#
|
||||||
|
# add_function_parentheses = True
|
||||||
|
|
||||||
|
# If true, the current module name will be prepended to all description
|
||||||
|
# unit titles (such as .. function::).
|
||||||
|
#
|
||||||
|
# add_module_names = True
|
||||||
|
|
||||||
|
# If true, sectionauthor and moduleauthor directives will be shown in the
|
||||||
|
# output. They are ignored by default.
|
||||||
|
#
|
||||||
|
# show_authors = False
|
||||||
|
|
||||||
|
# The name of the Pygments (syntax highlighting) style to use.
|
||||||
|
pygments_style = 'sphinx'
|
||||||
|
|
||||||
|
# A list of ignored prefixes for module index sorting.
|
||||||
|
# modindex_common_prefix = []
|
||||||
|
|
||||||
|
# If true, keep warnings as "system message" paragraphs in the built documents.
|
||||||
|
# keep_warnings = False
|
||||||
|
|
||||||
|
# If true, `todo` and `todoList` produce output, else they produce nothing.
|
||||||
|
todo_include_todos = False
|
||||||
|
|
||||||
|
|
||||||
|
# -- Options for HTML output ----------------------------------------------
|
||||||
|
|
||||||
|
# The theme to use for HTML and HTML Help pages. See the documentation for
|
||||||
|
# a list of builtin themes.
|
||||||
|
#
|
||||||
|
html_theme = 'alabaster'
|
||||||
|
|
||||||
|
# Theme options are theme-specific and customize the look and feel of a theme
|
||||||
|
# further. For a list of options available for each theme, see the
|
||||||
|
# documentation.
|
||||||
|
#
|
||||||
|
# html_theme_options = {}
|
||||||
|
|
||||||
|
# Add any paths that contain custom themes here, relative to this directory.
|
||||||
|
# html_theme_path = []
|
||||||
|
|
||||||
|
# The name for this set of Sphinx documents.
|
||||||
|
# "<project> v<release> documentation" by default.
|
||||||
|
#
|
||||||
|
# html_title = 'Stencil v0.6.0'
|
||||||
|
|
||||||
|
# A shorter title for the navigation bar. Default is the same as html_title.
|
||||||
|
#
|
||||||
|
# html_short_title = None
|
||||||
|
|
||||||
|
# The name of an image file (relative to this directory) to place at the top
|
||||||
|
# of the sidebar.
|
||||||
|
#
|
||||||
|
# html_logo = None
|
||||||
|
|
||||||
|
# The name of an image file (relative to this directory) to use as a favicon of
|
||||||
|
# the docs. This file should be a Windows icon file (.ico) being 16x16 or 32x32
|
||||||
|
# pixels large.
|
||||||
|
#
|
||||||
|
# html_favicon = None
|
||||||
|
|
||||||
|
# Add any paths that contain custom static files (such as style sheets) here,
|
||||||
|
# relative to this directory. They are copied after the builtin static files,
|
||||||
|
# so a file named "default.css" will overwrite the builtin "default.css".
|
||||||
|
html_static_path = ['_static']
|
||||||
|
|
||||||
|
# Add any extra paths that contain custom files (such as robots.txt or
|
||||||
|
# .htaccess) here, relative to this directory. These files are copied
|
||||||
|
# directly to the root of the documentation.
|
||||||
|
#
|
||||||
|
# html_extra_path = []
|
||||||
|
|
||||||
|
# If not None, a 'Last updated on:' timestamp is inserted at every page
|
||||||
|
# bottom, using the given strftime format.
|
||||||
|
# The empty string is equivalent to '%b %d, %Y'.
|
||||||
|
#
|
||||||
|
# html_last_updated_fmt = None
|
||||||
|
|
||||||
|
# If true, SmartyPants will be used to convert quotes and dashes to
|
||||||
|
# typographically correct entities.
|
||||||
|
#
|
||||||
|
# html_use_smartypants = True
|
||||||
|
|
||||||
|
# Custom sidebar templates, maps document names to template names.
|
||||||
|
html_sidebars = {
|
||||||
|
'index': ['sidebar_intro.html', 'searchbox.html'],
|
||||||
|
'**': ['sidebar_intro.html', 'localtoc.html', 'relations.html', 'searchbox.html'],
|
||||||
|
}
|
||||||
|
|
||||||
|
# Additional templates that should be rendered to pages, maps page names to
|
||||||
|
# template names.
|
||||||
|
#
|
||||||
|
# html_additional_pages = {}
|
||||||
|
|
||||||
|
# If false, no module index is generated.
|
||||||
|
#
|
||||||
|
# html_domain_indices = True
|
||||||
|
|
||||||
|
# If false, no index is generated.
|
||||||
|
#
|
||||||
|
# html_use_index = True
|
||||||
|
|
||||||
|
# If true, the index is split into individual pages for each letter.
|
||||||
|
#
|
||||||
|
# html_split_index = False
|
||||||
|
|
||||||
|
# If true, links to the reST sources are added to the pages.
|
||||||
|
#
|
||||||
|
# html_show_sourcelink = True
|
||||||
|
|
||||||
|
# If true, "Created using Sphinx" is shown in the HTML footer. Default is True.
|
||||||
|
#
|
||||||
|
# html_show_sphinx = True
|
||||||
|
|
||||||
|
# If true, "(C) Copyright ..." is shown in the HTML footer. Default is True.
|
||||||
|
#
|
||||||
|
# html_show_copyright = True
|
||||||
|
|
||||||
|
# If true, an OpenSearch description file will be output, and all pages will
|
||||||
|
# contain a <link> tag referring to it. The value of this option must be the
|
||||||
|
# base URL from which the finished HTML is served.
|
||||||
|
#
|
||||||
|
# html_use_opensearch = ''
|
||||||
|
|
||||||
|
# This is the file name suffix for HTML files (e.g. ".xhtml").
|
||||||
|
# html_file_suffix = None
|
||||||
|
|
||||||
|
# Language to be used for generating the HTML full-text search index.
|
||||||
|
# Sphinx supports the following languages:
|
||||||
|
# 'da', 'de', 'en', 'es', 'fi', 'fr', 'h', 'it', 'ja'
|
||||||
|
# 'nl', 'no', 'pt', 'ro', 'r', 'sv', 'tr', 'zh'
|
||||||
|
#
|
||||||
|
# html_search_language = 'en'
|
||||||
|
|
||||||
|
# A dictionary with options for the search language support, empty by default.
|
||||||
|
# 'ja' uses this config value.
|
||||||
|
# 'zh' user can custom change `jieba` dictionary path.
|
||||||
|
#
|
||||||
|
# html_search_options = {'type': 'default'}
|
||||||
|
|
||||||
|
# The name of a javascript file (relative to the configuration directory) that
|
||||||
|
# implements a search results scorer. If empty, the default will be used.
|
||||||
|
#
|
||||||
|
# html_search_scorer = 'scorer.js'
|
||||||
|
|
||||||
|
# Output file base name for HTML help builder.
|
||||||
|
htmlhelp_basename = 'Stencildoc'
|
||||||
|
|
||||||
|
# -- Options for LaTeX output ---------------------------------------------
|
||||||
|
|
||||||
|
latex_elements = {
|
||||||
|
# The paper size ('letterpaper' or 'a4paper').
|
||||||
|
#
|
||||||
|
# 'papersize': 'letterpaper',
|
||||||
|
|
||||||
|
# The font size ('10pt', '11pt' or '12pt').
|
||||||
|
#
|
||||||
|
# 'pointsize': '10pt',
|
||||||
|
|
||||||
|
# Additional stuff for the LaTeX preamble.
|
||||||
|
#
|
||||||
|
# 'preamble': '',
|
||||||
|
|
||||||
|
# Latex figure (float) alignment
|
||||||
|
#
|
||||||
|
# 'figure_align': 'htbp',
|
||||||
|
}
|
||||||
|
|
||||||
|
# Grouping the document tree into LaTeX files. List of tuples
|
||||||
|
# (source start file, target name, title,
|
||||||
|
# author, documentclass [howto, manual, or own class]).
|
||||||
|
latex_documents = [
|
||||||
|
(master_doc, 'Stencil.tex', 'Stencil Documentation',
|
||||||
|
'Kyle Fuller', 'manual'),
|
||||||
|
]
|
||||||
|
|
||||||
|
# The name of an image file (relative to this directory) to place at the top of
|
||||||
|
# the title page.
|
||||||
|
#
|
||||||
|
# latex_logo = None
|
||||||
|
|
||||||
|
# For "manual" documents, if this is true, then toplevel headings are parts,
|
||||||
|
# not chapters.
|
||||||
|
#
|
||||||
|
# latex_use_parts = False
|
||||||
|
|
||||||
|
# If true, show page references after internal links.
|
||||||
|
#
|
||||||
|
# latex_show_pagerefs = False
|
||||||
|
|
||||||
|
# If true, show URL addresses after external links.
|
||||||
|
#
|
||||||
|
# latex_show_urls = False
|
||||||
|
|
||||||
|
# Documents to append as an appendix to all manuals.
|
||||||
|
#
|
||||||
|
# latex_appendices = []
|
||||||
|
|
||||||
|
# It false, will not define \strong, \code, itleref, \crossref ... but only
|
||||||
|
# \sphinxstrong, ..., \sphinxtitleref, ... To help avoid clash with user added
|
||||||
|
# packages.
|
||||||
|
#
|
||||||
|
# latex_keep_old_macro_names = True
|
||||||
|
|
||||||
|
# If false, no module index is generated.
|
||||||
|
#
|
||||||
|
# latex_domain_indices = True
|
||||||
|
|
||||||
|
|
||||||
|
# -- Options for manual page output ---------------------------------------
|
||||||
|
|
||||||
|
# One entry per manual page. List of tuples
|
||||||
|
# (source start file, name, description, authors, manual section).
|
||||||
|
man_pages = [
|
||||||
|
(master_doc, 'stencil', 'Stencil Documentation',
|
||||||
|
[author], 1)
|
||||||
|
]
|
||||||
|
|
||||||
|
# If true, show URL addresses after external links.
|
||||||
|
#
|
||||||
|
# man_show_urls = False
|
||||||
|
|
||||||
|
|
||||||
|
# -- Options for Texinfo output -------------------------------------------
|
||||||
|
|
||||||
|
# Grouping the document tree into Texinfo files. List of tuples
|
||||||
|
# (source start file, target name, title, author,
|
||||||
|
# dir menu entry, description, category)
|
||||||
|
texinfo_documents = [
|
||||||
|
(master_doc, 'Stencil', 'Stencil Documentation',
|
||||||
|
author, 'Stencil', 'One line description of project.',
|
||||||
|
'Miscellaneous'),
|
||||||
|
]
|
||||||
|
|
||||||
|
# Documents to append as an appendix to all manuals.
|
||||||
|
#
|
||||||
|
# texinfo_appendices = []
|
||||||
|
|
||||||
|
# If false, no module index is generated.
|
||||||
|
#
|
||||||
|
# texinfo_domain_indices = True
|
||||||
|
|
||||||
|
# How to display URL addresses: 'footnote', 'no', or 'inline'.
|
||||||
|
#
|
||||||
|
# texinfo_show_urls = 'footnote'
|
||||||
|
|
||||||
|
# If true, do not generate a @detailmenu in the "Top" node's menu.
|
||||||
|
#
|
||||||
|
# texinfo_no_detailmenu = False
|
||||||
70
docs/custom-template-tags-and-filters.rst
Normal file
70
docs/custom-template-tags-and-filters.rst
Normal file
@@ -0,0 +1,70 @@
|
|||||||
|
Custom Template Tags and Filters
|
||||||
|
================================
|
||||||
|
|
||||||
|
You can build your own custom filters and tags and pass them down while
|
||||||
|
rendering your template. Any custom filters or tags must be registered with a
|
||||||
|
extension which contains all filters and tags available to the template.
|
||||||
|
|
||||||
|
.. code-block:: swift
|
||||||
|
|
||||||
|
let ext = Extension()
|
||||||
|
// Register your filters and tags with the extension
|
||||||
|
|
||||||
|
let environment = Environment(extensions: [ext])
|
||||||
|
try environment.renderTemplate(name: "example.html")
|
||||||
|
|
||||||
|
Custom Filters
|
||||||
|
--------------
|
||||||
|
|
||||||
|
Registering custom filters:
|
||||||
|
|
||||||
|
.. code-block:: swift
|
||||||
|
|
||||||
|
ext.registerFilter("double") { (value: Any?) in
|
||||||
|
if let value = value as? Int {
|
||||||
|
return value * 2
|
||||||
|
}
|
||||||
|
|
||||||
|
return value
|
||||||
|
}
|
||||||
|
|
||||||
|
Registering custom filters with arguments:
|
||||||
|
|
||||||
|
.. code-block:: swift
|
||||||
|
|
||||||
|
ext.registerFilter("multiply") { (value: Any?, arguments: [Any?]) in
|
||||||
|
let amount: Int
|
||||||
|
|
||||||
|
if let value = arguments.first as? Int {
|
||||||
|
amount = value
|
||||||
|
} else {
|
||||||
|
throw TemplateSyntaxError("multiple tag must be called with an integer argument")
|
||||||
|
}
|
||||||
|
|
||||||
|
if let value = value as? Int {
|
||||||
|
return value * 2
|
||||||
|
}
|
||||||
|
|
||||||
|
return value
|
||||||
|
}
|
||||||
|
|
||||||
|
Custom Tags
|
||||||
|
-----------
|
||||||
|
|
||||||
|
You can build a custom template tag. There are a couple of APIs to allow you to
|
||||||
|
write your own custom tags. The following is the simplest form:
|
||||||
|
|
||||||
|
.. code-block:: swift
|
||||||
|
|
||||||
|
ext.registerSimpleTag("custom") { context in
|
||||||
|
return "Hello World"
|
||||||
|
}
|
||||||
|
|
||||||
|
When your tag is used via ``{% custom %}`` it will execute the registered block
|
||||||
|
of code allowing you to modify or retrieve a value from the context. Then
|
||||||
|
return either a string rendered in your template, or throw an error.
|
||||||
|
|
||||||
|
If you want to accept arguments or to capture different tokens between two sets
|
||||||
|
of template tags. You will need to call the ``registerTag`` API which accepts a
|
||||||
|
closure to handle the parsing. You can find examples of the ``now``, ``if`` and
|
||||||
|
``for`` tags found inside Stencil source code.
|
||||||
37
docs/getting-started.rst
Normal file
37
docs/getting-started.rst
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
Getting Started
|
||||||
|
===============
|
||||||
|
|
||||||
|
The easiest way to render a template using Stencil is to create a template and
|
||||||
|
call render on it providing a context.
|
||||||
|
|
||||||
|
.. code-block:: swift
|
||||||
|
|
||||||
|
let template = Template(templateString: "Hello {{ name }}")
|
||||||
|
try template.render(["name": "kyle"])
|
||||||
|
|
||||||
|
For more advanced uses, you would normally create an ``Environment`` and call
|
||||||
|
the ``renderTemplate`` convinience method.
|
||||||
|
|
||||||
|
.. code-block:: swift
|
||||||
|
|
||||||
|
let environment = Environment()
|
||||||
|
|
||||||
|
let context = ["name": "kyle"]
|
||||||
|
try template.renderTemplate(string: "Hello {{ name }}", context: context)
|
||||||
|
|
||||||
|
Template Loaders
|
||||||
|
----------------
|
||||||
|
|
||||||
|
A template loader allows you to load files from disk or elsewhere. Using a
|
||||||
|
``FileSystemLoader`` we can easily render a template from disk.
|
||||||
|
|
||||||
|
For example, to render a template called ``index.html`` inside the
|
||||||
|
``templates/`` directory we can use the following:
|
||||||
|
|
||||||
|
.. code-block:: swift
|
||||||
|
|
||||||
|
let fsLoader = FileSystemLoader(paths: ["templates/"])
|
||||||
|
let environment = Environment(loader: fsLoader)
|
||||||
|
|
||||||
|
let context = ["name": "kyle"]
|
||||||
|
try template.renderTemplate(name: "index.html", context: context)
|
||||||
64
docs/index.rst
Normal file
64
docs/index.rst
Normal file
@@ -0,0 +1,64 @@
|
|||||||
|
The Stencil template language
|
||||||
|
=============================
|
||||||
|
|
||||||
|
Stencil is a simple and powerful template language for Swift. It provides a
|
||||||
|
syntax similar to Django and Mustache. If you're familiar with these, you will
|
||||||
|
feel right at home with Stencil.
|
||||||
|
|
||||||
|
.. code-block:: html+django
|
||||||
|
|
||||||
|
There are {{ articles.count }} articles.
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
{% for article in articles %}
|
||||||
|
<li>{{ article.title }} by {{ article.author }}</li>
|
||||||
|
{% endfor %}
|
||||||
|
</ul>
|
||||||
|
|
||||||
|
.. code-block:: swift
|
||||||
|
|
||||||
|
import Stencil
|
||||||
|
|
||||||
|
struct Article {
|
||||||
|
let title: String
|
||||||
|
let author: String
|
||||||
|
}
|
||||||
|
|
||||||
|
let context = [
|
||||||
|
"articles": [
|
||||||
|
Article(title: "Migrating from OCUnit to XCTest", author: "Kyle Fuller"),
|
||||||
|
Article(title: "Memory Management with ARC", author: "Kyle Fuller"),
|
||||||
|
]
|
||||||
|
]
|
||||||
|
|
||||||
|
let environment = Environment(loader: FileSystemLoader(paths: ["templates/"])
|
||||||
|
let rendered = try environment.renderTemplate(name: context)
|
||||||
|
|
||||||
|
print(rendered)
|
||||||
|
|
||||||
|
The User Guide
|
||||||
|
--------------
|
||||||
|
|
||||||
|
For Template Writers
|
||||||
|
~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
|
Resources for Stencil template authors to write Stencil templates.
|
||||||
|
|
||||||
|
.. toctree::
|
||||||
|
:maxdepth: 2
|
||||||
|
|
||||||
|
templates
|
||||||
|
builtins
|
||||||
|
|
||||||
|
For Developers
|
||||||
|
~~~~~~~~~~~~~~
|
||||||
|
|
||||||
|
Resources to help you integrate Stencil into a Swift project.
|
||||||
|
|
||||||
|
.. toctree::
|
||||||
|
:maxdepth: 1
|
||||||
|
|
||||||
|
installation
|
||||||
|
getting-started
|
||||||
|
api
|
||||||
|
custom-template-tags-and-filters
|
||||||
52
docs/installation.rst
Normal file
52
docs/installation.rst
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
Installation
|
||||||
|
============
|
||||||
|
|
||||||
|
Swift Package Mangaer
|
||||||
|
---------------------
|
||||||
|
|
||||||
|
If you're using the Swift Package Manager, you can add ``Stencil`` to your
|
||||||
|
dependencies inside ``Package.swift``.
|
||||||
|
|
||||||
|
.. code-block:: swift
|
||||||
|
|
||||||
|
import PackageDescription
|
||||||
|
|
||||||
|
let package = Package(
|
||||||
|
name: "MyApplication",
|
||||||
|
dependencies: [
|
||||||
|
.Package(url: "https://github.com/kylef/Stencil.git", majorVersion: 0, minor: 8),
|
||||||
|
]
|
||||||
|
)
|
||||||
|
|
||||||
|
CocoaPods
|
||||||
|
---------
|
||||||
|
|
||||||
|
If you're using CocoaPods, you can add Stencil to your ``Podfile`` and then run
|
||||||
|
``pod install``.
|
||||||
|
|
||||||
|
.. code-block:: ruby
|
||||||
|
|
||||||
|
pod 'Stencil', '~> 0.8.0'
|
||||||
|
|
||||||
|
Carthage
|
||||||
|
--------
|
||||||
|
|
||||||
|
.. note:: Use at your own risk. We don't offer support for Carthage and instead recommend you use Swift Package Manager.
|
||||||
|
|
||||||
|
1) Add ``Stencil`` to your ``Cartfile``:
|
||||||
|
|
||||||
|
.. code-block:: text
|
||||||
|
|
||||||
|
github "kylef/Stencil" ~> 0.8.0
|
||||||
|
|
||||||
|
2) Checkout your dependencies, generate the Stencil Xcode project, and then use Carthage to build Stencil:
|
||||||
|
|
||||||
|
.. code-block:: shell
|
||||||
|
|
||||||
|
$ carthage update
|
||||||
|
$ (cd Carthage/Checkouts/Stencil && swift package generate-xcodeproj)
|
||||||
|
$ carthage build
|
||||||
|
|
||||||
|
3) Follow the Carthage steps to add the built frameworks to your project.
|
||||||
|
|
||||||
|
To learn more about this approach see `Using Swift Package Manager with Carthage <https://fuller.li/posts/using-swift-package-manager-with-carthage/>`_.
|
||||||
167
docs/templates.rst
Normal file
167
docs/templates.rst
Normal file
@@ -0,0 +1,167 @@
|
|||||||
|
Language overview
|
||||||
|
==================
|
||||||
|
|
||||||
|
- ``{{ ... }}`` for variables to print to the template output
|
||||||
|
- ``{% ... %}`` for tags
|
||||||
|
- ``{# ... #}`` for comments not included in the template output
|
||||||
|
|
||||||
|
Variables
|
||||||
|
---------
|
||||||
|
|
||||||
|
A variable can be defined in your template using the following:
|
||||||
|
|
||||||
|
.. code-block:: html+django
|
||||||
|
|
||||||
|
{{ variable }}
|
||||||
|
|
||||||
|
Stencil will look up the variable inside the current variable context and
|
||||||
|
evaluate it. When a variable contains a dot, it will try doing the
|
||||||
|
following lookup:
|
||||||
|
|
||||||
|
- Context lookup
|
||||||
|
- Dictionary lookup
|
||||||
|
- Array lookup (first, last, count, index)
|
||||||
|
- Key value coding lookup
|
||||||
|
- Type introspection
|
||||||
|
|
||||||
|
For example, if `people` was an array:
|
||||||
|
|
||||||
|
.. code-block:: html+django
|
||||||
|
|
||||||
|
There are {{ people.count }} people. {{ people.first }} is the first
|
||||||
|
person, followed by {{ people.1 }}.
|
||||||
|
|
||||||
|
Filters
|
||||||
|
~~~~~~~
|
||||||
|
|
||||||
|
Filters allow you to transform the values of variables. For example, they look like:
|
||||||
|
|
||||||
|
.. code-block:: html+django
|
||||||
|
|
||||||
|
{{ variable|uppercase }}
|
||||||
|
|
||||||
|
See :ref:`all builtin filters <built-in-filters>`.
|
||||||
|
|
||||||
|
Tags
|
||||||
|
----
|
||||||
|
|
||||||
|
Tags are a mechanism to execute a piece of code, allowing you to have
|
||||||
|
control flow within your template.
|
||||||
|
|
||||||
|
.. code-block:: html+django
|
||||||
|
|
||||||
|
{% if variable %}
|
||||||
|
{{ variable }} was found.
|
||||||
|
{% endif %}
|
||||||
|
|
||||||
|
A tag can also affect the context and define variables as follows:
|
||||||
|
|
||||||
|
.. code-block:: html+django
|
||||||
|
|
||||||
|
{% for item in items %}
|
||||||
|
{{ item }}
|
||||||
|
{% endfor %}
|
||||||
|
|
||||||
|
Stencil includes of built-in tags which are listed below. You can also
|
||||||
|
extend Stencil by providing your own tags.
|
||||||
|
|
||||||
|
See :ref:`all builtin tags <built-in-tags>`.
|
||||||
|
|
||||||
|
Comments
|
||||||
|
--------
|
||||||
|
|
||||||
|
To comment out part of your template, you can use the following syntax:
|
||||||
|
|
||||||
|
.. code-block:: html+django
|
||||||
|
|
||||||
|
{# My comment is completely hidden #}
|
||||||
|
|
||||||
|
.. _template-inheritance:
|
||||||
|
|
||||||
|
Template inheritance
|
||||||
|
--------------------
|
||||||
|
|
||||||
|
Template inheritance allows the common components surrounding individual pages
|
||||||
|
to be shared across other templates. You can define blocks which can be
|
||||||
|
overidden in any child template.
|
||||||
|
|
||||||
|
Let's take a look at an example. Here is our base template (``base.html``):
|
||||||
|
|
||||||
|
.. code-block:: html+django
|
||||||
|
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<title>{% block title %}Example{% endblock %}</title>
|
||||||
|
</head>
|
||||||
|
|
||||||
|
<body>
|
||||||
|
<aside>
|
||||||
|
{% block sidebar %}
|
||||||
|
<ul>
|
||||||
|
<li><a href="/">Home</a></li>
|
||||||
|
<li><a href="/notes/">Notes</a></li>
|
||||||
|
</ul>
|
||||||
|
{% endblock %}
|
||||||
|
</aside>
|
||||||
|
|
||||||
|
<section>
|
||||||
|
{% block content %}{% endblock %}
|
||||||
|
</section>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
|
||||||
|
This example declares three blocks, ``title``, ``sidebar`` and ``content``. We
|
||||||
|
can use the ``{% extends %}`` template tag to inherit from out base template
|
||||||
|
and then use ``{% block %}`` to override any blocks from our base template.
|
||||||
|
|
||||||
|
A child template might look like the following:
|
||||||
|
|
||||||
|
.. code-block:: html+django
|
||||||
|
|
||||||
|
{% extends "base.html" %}
|
||||||
|
|
||||||
|
{% block title %}Notes{% endblock %}
|
||||||
|
|
||||||
|
{% block content %}
|
||||||
|
{% for note in notes %}
|
||||||
|
<h2>{{ note }}</h2>
|
||||||
|
{% endfor %}
|
||||||
|
{% endblock %}
|
||||||
|
|
||||||
|
.. note:: You can use ``{{ block.super }}` inside a block to render the contents of the parent block inline.
|
||||||
|
|
||||||
|
Since our child template doesn't declare a sidebar block. The original sidebar
|
||||||
|
from our base template will be used. Depending on the content of ``notes`` our
|
||||||
|
template might be rendered like the following:
|
||||||
|
|
||||||
|
.. code-block:: html
|
||||||
|
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<title>Notes</title>
|
||||||
|
</head>
|
||||||
|
|
||||||
|
<body>
|
||||||
|
<aside>
|
||||||
|
<ul>
|
||||||
|
<li><a href="/">Home</a></li>
|
||||||
|
<li><a href="/notes/">Notes</a></li>
|
||||||
|
</ul>
|
||||||
|
</aside>
|
||||||
|
|
||||||
|
<section>
|
||||||
|
<h2>Pick up food</h2>
|
||||||
|
<h2>Do laundry</h2>
|
||||||
|
</section>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
|
||||||
|
You can use as many levels of inheritance as needed. One common way of using
|
||||||
|
inheritance is the following three-level approach:
|
||||||
|
|
||||||
|
* Create a ``base.html`` template that holds the main look-and-feel of your site.
|
||||||
|
* Create a ``base_SECTIONNAME.html`` template for each “section” of your site.
|
||||||
|
For example, ``base_news.html``, ``base_news.html``. These templates all
|
||||||
|
extend ``base.html`` and include section-specific styles/design.
|
||||||
|
* Create individual templates for each type of page, such as a news article or
|
||||||
|
blog entry. These templates extend the appropriate section template.
|
||||||
Reference in New Issue
Block a user