feat(docs): Add custom tags and filters to documentation

This commit is contained in:
Kyle Fuller
2016-11-28 03:11:38 +00:00
parent 3b5e8f2468
commit 9b6ee14aa3
2 changed files with 69 additions and 0 deletions

View File

@@ -0,0 +1,68 @@
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
namespace which contains all filters and tags available to the template.
.. code-block:: swift
let namespace = Namespace()
// Register your filters and tags with the namespace
let rendered = try template.render(context, namespace: namespace)
Custom Filters
--------------
Registering custom filters:
.. code-block:: swift
namespace.registerFilter("double") { (value: Any?) in
if let value = value as? Int {
return value * 2
}
return value
}
Registering custom filters with arguments:
.. code-block:: swift
namespace.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
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 Stencil source code.

View File

@@ -39,3 +39,4 @@ Contents:
templates templates
builtins builtins
custom-template-tags-and-filters