#handlebars

handlebars_misc_helpers

A collection of helpers for handlebars (rust)

8 releases (4 breaking)

✓ Uses Rust 2018 edition

0.5.2 Sep 15, 2019
0.5.1 Sep 5, 2019
0.5.0 Aug 11, 2019
0.4.1 Aug 10, 2019
0.1.0 Jul 21, 2019

#28 in Template engine

Download history 27/week @ 2019-07-15 69/week @ 2019-07-22 27/week @ 2019-07-29 76/week @ 2019-08-05 92/week @ 2019-08-12 20/week @ 2019-08-19 111/week @ 2019-08-26 137/week @ 2019-09-02 30/week @ 2019-09-09 119/week @ 2019-09-16 24/week @ 2019-09-23 27/week @ 2019-09-30 50/week @ 2019-10-07

195 downloads per month
Used in 1 crate

CC0 license

30KB
566 lines

handlebars_misc_helpers

Crates.io Crates.io

Project Status: WIP – Initial development is in progress, but there has not yet been a stable, usable release suitable for the public. Build Status Documentation

A collection of helpers for handlebars (rust).

Helpers extend the template to generate or to transform content. Few helpers are included, but if you need more helpers, ask via an issue or a PR.

To use an helper:

{{ helper_name argument}}

To chain helpers, use parenthesis:

{{ to_upper_case (to_singular "Hello foo-bars") }}
// -> "BAR"

see Handlebars templating language

String transformation

helper signature usage sample sample out
replace s:String from:String to:String replace "Hello old" "old" "new" "Hello new"
to_lower_case s:String to_lower_case "Hello foo-bars" "hello foo-bars"
to_upper_case s:String to_upper_case "Hello foo-bars" "HELLO FOO-BARS"
to_camel_case s:String to_camel_case "Hello foo-bars" "helloFooBars"
to_pascal_case s:String to_pascal_case "Hello foo-bars" "HelloFooBars"
to_snake_case s:String to_snake_case "Hello foo-bars" "hello_foo_bars"
to_screaming_snake_case s:String to_screaming_snake_case "Hello foo-bars" "HELLO_FOO_BARS"
to_kebab_case s:String to_kebab_case "Hello foo-bars" "hello-foo-bars"
to_train_case s:String to_train_case "Hello foo-bars" "Hello-Foo-Bars"
to_sentence_case s:String to_sentence_case "Hello foo-bars" "Hello foo" bars
to_title_case s:String to_title_case "Hello foo-bars" "Hello Foo Bars"
to_class_case s:String to_class_case "Hello foo-bars" "HelloFooBar"
to_table_case s:String to_table_case "Hello foo-bars" "hello_foo_bars"
to_plural s:String to_plural "Hello foo-bars" "bars"
to_singular s:String to_singular "Hello foo-bars" "bar"
trim s:String trim " foo " "foo"
trim_start s:String trim_start " foo " "foo "
trim_end s:String trim_end " foo " " foo"

Http content

Helper able to render body response from an http request.

helper signature usage sample
http_get url:String http_get "http://hello/..."
gitignore_io templates:String gitignore_io "rust,visualstudiocode"

Path extraction

Helper able to extract (or transform) path (defined as string).

for the same input: "/hello/bar/foo.txt"

helper_name sample output
file_name "foo.txt"
parent "/hello/bar"
extension "txt"

Environment variable

Helper able to get environment variables.

helper_name usage
env_var env_var "HOME"

Specials environment variables are predefined (some of them come from std::env::consts - Rust):

variable possible values
"ARCH"
  • x86
  • x86_64
  • arm
  • aarch64
  • mips
  • mips64
  • powerpc
  • powerpc64
  • s390x
  • sparc64
"DLL_EXTENSION"
  • so
  • dylib
  • dll
"DLL_PREFIX"
  • lib
  • "" (an empty string)
"DLL_SUFFIX"
  • .so
  • .dylib
  • .dll
"EXE_EXTENSION"
  • exe
  • "" (an empty string)
"EXE_SUFFIX"
  • .exe
  • .nexe
  • .pexe
  • "" (an empty string)
"FAMILY"
  • unix
  • windows
"OS"
  • linux
  • macos
  • ios
  • freebsd
  • dragonfly
  • netbsd
  • openbsd
  • solaris
  • android
  • windows
"USERNAME"try to find the current username, in the order:
  1. environment variable "USERNAME"
  2. environment variable "username"
  3. environment variable "USER"
  4. environment variable "user"

JSON

helpers:

  • json_query query:String data:Json: Helper able to extract information from json using JMESPath syntax for query.
  • json_str_query query:String data:String: Helper able to extract information from json using JMESPath syntax for query.
  • json_to_str data:Json: convert a json into a string (no pretty).
  • json_to_str data:String: convert(parse) a string into a json.
usage output
{{ json_query "foo" {} }} ``
{{ json_to_str ( json_query "foo" {"foo":{"bar":{"baz":true}}} ) }} {"bar":{"baz":true}}
{{ json_to_str ( json_query "foo" (str_to_json "{\"foo\":{\"bar\":{\"baz\":true}}}" ) ) }} `{"bar":{"baz":true}}
{{ json_str_query "foo" "{\"foo\":{\"bar\":{\"baz\":true}}}" }} {"bar":{"baz":true}}
{{ json_str_query "foo.bar.baz" "{\"foo\":{\"bar\":{\"baz\":true}}}" }} true

Assign

Helper able to assign (to set) a variable to use later in the template.

usage output
{{ assign "foo" "{}" }} ``
{{ assign "foo" "{}" }}{{ foo }} {}
{{ assign "foo" "hello world" }}{{ foo }} hello world
{{ assign "foo" {} }}{{ foo }} [object]
{{ assign "foo" {"bar": 33} }}{{ foo }} [object]

Dependencies

~2.7–3.5MB
~76K SLoC