Swiftpack.co - Package - SwiftGen/StencilSwiftKit


CircleCI CocoaPods Compatible Platform Swift 5.0

StencilSwiftKit is a framework bringing additional Stencil nodes & filters dedicated to Swift code generation.


  • Macro & Call
    • {% macro <Name> <Params> %}…{% endmacro %}
    • Defines a macro that will be replaced by the nodes inside of this block later when called
    • {% call <Name> <Args> %}
    • Calls a previously defined macro, passing it some arguments
  • Set
    • {% set <Name> %}…{% endset %}
    • Renders the nodes inside this block immediately, and stores the result in the <Name> variable of the current context.
  • Map
    • {% map <Variable> into <Name> using <ItemName> %}…{% endmap %}
    • Apply a map operator to an array, and store the result into a new array variable <Name> in the current context.
    • Inside the map loop, a maploop special variable is available (akin to the forloop variable in for nodes). It exposes maploop.counter, maploop.first, maploop.last and maploop.item.


  • String filters:
    • basename: Get the filename from a path.
    • camelToSnakeCase: Transforms text from camelCase to snake_case. By default it converts to lower case, unless a single optional argument is set to "false", "no" or "0".
    • contains: Check if a string contains a specific substring.
    • dirname: Get the path to the parent folder from a path.
    • escapeReservedKeywords: Escape keywords reserved in the Swift language, by wrapping them inside backticks so that the can be used as regular escape keywords in Swift code.
    • hasPrefix / hasSuffix: Check if a string starts/ends with a specific substring.
    • lowerFirstLetter: Lowercases only the first letter of a string.
    • lowerFirstWord: Lowercases only the first word of a string.
    • removeNewlines: Removes newlines and other whitespace characters, depending on the mode ("all" or "leading").
    • replace: Replaces instances of a substring with a new string.
    • snakeToCamelCase: Transforms text from snake_case to camelCase. By default it keeps leading underscores, unless a single optional argument is set to "true", "yes" or "1".
    • swiftIdentifier: Transforms an arbitrary string into a valid Swift identifier (using only valid characters for a Swift identifier as defined in the Swift language reference). In "pretty" mode, it will also apply the snakeToCamelCase filter afterwards, and other manipulations if needed for a "prettier" but still valid identifier.
    • upperFirstLetter: Uppercases only the first character
  • Number filters:
    • int255toFloat
    • hexToInt
    • percent


This framework also contains a StencilSwiftTemplate class, which is a subclass of Stencil.Template dedicated to remove extra newlines when rendering the template.

Indeed, such extra newlines could otherwise be inserted in the generated output because you want your template to be well formatted, and that can end up with Stencil nodes like {% for … %} and {% if … %} be alone in some lines of your template and that would render into nothing by themselves, generating empty lines in the output.

This template subclass aims to remove those lines generated by using a simple workaround when rendering, until there's an embeded way to handle that in Stencil proper (see Stencil/#22).

Stencil.Extension & swiftStencilEnvironment

This framework also contains helper methods for Stencil.Extension and Stencil.Environment, to easily register all the tags and filters listed above on an existing Stencil.Extension, as well as to easily get a Stencil.Environment preconfigured with both those tags & filters Extension and the StencilSwiftTemplate.


This framework contains an additional parser, meant to parse a list of parameters from the CLI. For example, using Commander, if you receive a [String] from a VariadicOption<String>, you can use the parser to convert it into a structured dictionary. For example:

["foo=1", "bar=2", "baz.qux=hello", "baz.items=a", "baz.items=b", "something"]

will become

  "foo": "1",
  "bar": "2",
  "baz": [
    "qux": "hello",
    "items": [
  something: true

For easier use, you can use the StencilContext.enrich(context:parameters:environment:) function to add the following variables to a context:

  • param: the parsed parameters using the parser mentioned above.
  • env: a dictionary with all available environment variables (such as PATH).


This code and tool is under the MIT Licence. See the LICENCE file in this repository.


Stars: 156


Used By

Total: 0


2.7.2 - 2019-01-28 00:12:41

Bug Fixes

  • Parameters: ensure the flatten function correctly handles a flag with a false value.
    David Jennes #108

Internal Changes

2.7.1 - 2018-10-02 22:24:27

Bug Fixes

2.7.0 - 2018-09-26 00:42:44

New Features

Internal Changes

  • Improved the documentation of string filters a bit for a better overview of the inputs & outputs.
    David Jennes #102
  • Updated to latest Xcode (10.0).
    David Jennes #103

2.6.0 - 2018-08-30 14:06:56

Bug fixes

New Features

  • The set tag can now directly accept an expression as value, see the documentation for an explanation on how this differs with the normal set/endset pair.
    David Jennes #247
  • Updated Stencil to the latest version (0.12.1).
    David Jennes #95 #99

Internal Changes

2.5.0 - 2018-07-30 12:53:27

New Features

Internal Changes

2.4.0 - 2018-03-17 15:07:03

New Features

2.3.0 - 2017-10-22 15:03:14

Bug Fixes

New Features

  • Added Parameters.flatten(dictionary:) method to do the opposite of Parameters.parse(items:) and turn a dictionary into the list of parameters to pass from the command line.
    Olivier Halligon #70

- 2017-10-01 21:39:01

New Features

  • Accept LosslessStringConvertible input for strings filters.
    Antondomashnev #65
  • StencilContext.enrich now also accept a Dictionary for specifying parameters (in preparation for supporting Config files in SwiftGen).
    Olivier Halligon #66

Internal Changes

2.1.0 - 2017-08-20 18:02:46

New Features

  • Added the basename and dirname string filters for getting a filename, or parent folder (respectively), out of a path.
    David Jennes #60
  • Modify the swiftIdentifier string filter to accept an optional "pretty" mode, to also apply the snakeToCamelCase filter and other manipulations if needed for a "prettier" but still valid identifier.
    David Jennes #61

Internal Changes

- 2017-08-12 17:43:56

- 2017-07-21 17:11:09

Due to the removal of legacy code, there are a few breaking changes in this new version that affect both template writers as well as developers. We've provided a migration guide to help you through these changes, which you can find here: Migration Guide for 2.0

Bug Fixes

  • Fix snakeToCamelCase parameters information in README.
    Liquidsoul #45

Breaking Changes

  • The ParametersError enum has been replaced by the Parameters.Error nested type.
    Olivier Halligon #37
  • The FilterError enum has been replaced by the Filters.Error nested type.
    Olivier Halligon #37
  • The filters in StringFilters and NumFilters are now located under Filters.Strings and Filters.Numbers.
    Olivier Halligon #40
  • Removed the join filter, as it's now integrated in Stencil proper.
    David Jennes #10
  • Refactored the snakeToCamelCase filter to now accept an (optional) boolean parameter to control the noPrefix behaviour.
    David Jennes #41
  • Rename the stringToSwiftIdentifier function to swiftIdentifier to better match the other method names.
    David Jennes #46

New Features

  • Added the removeNewlines filter to remove newlines (and spaces) from a string.
    David Jennes #47 #48

- 2017-05-24 08:31:50

Bug Fixes

  • The context enrich function won't overwrite existing values in the env and param variables.
    David Jennes #29

New Features

Internal Changes


  • The ParametersError enum has been replaced by the Parameters.Error nested type. ParametersError still works (it is now typealias) but will be removed in the next major release.
    Olivier Halligon
  • The FilterError enum has been replaced by the Filters.Error nested type. FilterError still works (it is now typealias) but will be removed in the next major release.
    Olivier Halligon

- 2017-03-01 14:41:21

Internal Changes

- 2017-02-20 11:59:33

New Features

  • Added support for Swift Package Manager.
    Krzysztof Zabłocki #15
  • Added MapNode to apply a map operator to an array. You can now use {% map someArray into result using item %} to do the equivalent of the result = someArray.map { item in … } Swift code.
    David Jennes #11
  • Add a "parameters parser" able to transform parameters passed as a set of strings (a=1 b.x=2 b.y=3 c=4 c=5) — typically provided as the command line arguments of a CLI — into a Dictionary suitable for Stencil contexts.
    David Jennes #8
  • Add a StencilContext.enrich function to enrich Stencil contexts with param and env dictionaries.
    The param dictionary typically contains parameters parsed via the parameters parser above.
    The env dictionary contains all the environment variables. You can thus access them in your templates using env.USER, env.LANG, env.PRODUCT_MODULE_NAME, etc.

Internal Changes

  • Renamed SwiftTemplate to StencilSwiftTemplate.
    David Jennes #14
  • Refactor stencil swift extensions registration for easier use with an existing Extension.
    David Jennes #16
  • Refactor stencil node tests to not use templates and output files.
    David Jennes #17

0.0.1 - 2017-01-30 15:47:17

  • First version supporting Swift Package Manager