Skip to content

[Feature] Provide a way to format the data source before rendering the template #3

@alephao

Description

@alephao

Some times we have a data source that is not optimal for our template, so I would like to format the data before I pass it to the template. For example, consider the data source below:

- category: A
  name: foo
- category: A
  name: bar
- category: B
  name: qux
- category: B
  name: baz

I want to render:

enum A {
  case foo, bar
}

enum B {
  case qux, baz
}

I'm not sure if it's even possible to write a stencil template that generates the code above, but it would be much easier if we could format the object before rendering the stencil template.

It would be great if we can provide a way for the user to write some Swift code to format the object from the data source and use the result as the context for the Stencil template.

I'm not sure how that would work yet, maybe the user can provide a function and somehow Stencly evaluates it before rendering.

// MyFormatter.swift
func format(_ context: Any) -> Any { ... }

Then when running stencly, the user can also provide a swift file to use as the formatter e.g.:

stencly -d datasource.yaml -t template.stencil -f MyFormatter.swift -o File.generated.swift

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions