Skip to content

Providing a "slugify" functionality #31

@Arteneko

Description

@Arteneko

I have a template where I request two related info.

  • A name (i.e. a title)
  • A slug (to name the file and resulting URL)

This slug is closely related to the name, and I'd like to be able to have something to slugify the name the user entered, and use that resulting value as template value.

This would introduce a change, with a concept of transformations.

I see a few ways this could be done.

transforms inside the template.toml file

One way would be, for a given variable (one [[variables]]), to be able to provide a new key, whose
format would be expected as a key-value object.

[[variables]]
name="project"
default="My super project"
prompt="Name your project!"

[variables.transforms]
project_slug = slugify

The resulting object would look like this.

{
	"variables": [
		{
			"name": "project",
			"default": "My super project",
			"prompt": "Name your project",
			"transforms": {
				"project_slug": "slugify"
			}
		}
	]
}

Template variable usage filter application

The second proposal would be to allow a pipe syntax in the placeholder keys,
which would then be ran, and the resulting string would be used.

$ ls template/
{{project_name|slugify}}

The algorithm here would be pretty simple.

transform(fullkey):
	[key, ...methods] = split(fullkey, |)
	result = value_for(key) # original value
	for method in methods do
		result = method(result)
		assert result.length, "${method} returned an empty value, cannot continue"
	end
	return result

End

Of course, every method is built-in, and documented.

What do you think?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions