No description, website, or topics provided.
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
example
spice
.gitignore
LICENSE
README.md
dub.json

README.md

Spice

Spice is a templating engine written in D and built for simplicity and speed, using a PEG. Spice is meant to be used in a straightforward data-to-html manner. As such, it's currently impossible to, within the template itself, change data provided to the template. I currently have no plans to change this model.

Usage

Backend

Setup

The first step in the (fairly simple) process of using Spice is to add it to your dub dependencies. After that, you can import spice; and create a Spice object.

import spice;
Spice spice = new Spice;

Rendering a file

Spice files can be converted to HTML with a call to render(), which returns a string.

spice.render("<h1>{{ my_variable }}</h1>");

Defining values

Values can be defined with a call to defineValue(name, value).

spice.defineValue("hello", toValue("Hello, World!"));

The above code is equivalent to this:

spice.defineValue("hello", Value(SPICE_TYPE.STRING, "Hello, World!"));

However, you should always use toValue() so that you don't have to mess with types yourself, which could produce unexpected bugs.

Frontend

Output

Anything within {{ }} will be treated as a Spice value, which will be evaluated and outputted to the resulting HTML.

<h1>{{ "a string" }}</h1>

The above code would result in the following HTML:

<h1>a string</h1>

To output a value defined on the server:

spice.defineValue("hello", toValue("Hello, World!"));
spice.render("<h1>{{ hello }}</h1>");

This would result in the following HTML:

<h1>Hello, World!</h1>

Spice code

Anything within {% %} will be treated as Spice code.

Lists

A list is defined within square brackets in this manner:

["item1", true, 0, "item4"]

An item can be obtained from a list using square brackets.

my_list[0]

Maps

A map is defined within square brackets as well, with colon-separated key-value pairs.

["foo": "bar", "hello": "world", 123: "a number"]

An item can be obtained from a map in the same way that an item can be obtained from a list.

my_map["key_name"]

Spreads

A spread is an inclusive method of generating a list with the .. operator.

[1..5]

Which is equivalent to this:

[1, 2, 3, 4, 5]

Loops

Because Spice currently doesn't allow you to change data on the frontend, the foreach loop is the only type of usable loop. foreach loops can be used to iterate over both lists and maps.

{% foreach(value; [1..10]) %}
	<p>{{ value }}</p>
{% end %}

The above code would produce 10 paragraph tags containing their respective number.

{% foreach(index, value; [1, 2, 3]) %}
	<p>{{ index }}: {{ value }}</p>
{% end %}

The above code would produce this output:

<p>0: 1</p>
<p>1: 2</p>
<p>2: 3</p>

A foreach loop operating on a map would look something like this:

{% foreach(key, value; map) %}

If statement

if statements are pretty straightforward in Spice.

{% if(my_value == true and my_second_value == false) %}
	The first one is true and the second one is false!
{% elif(my_value == false and my_second_value == false) %}
	They're both false!
{% else %}
	Something else!
{% end %}

Length/empty

length() will return the length of the given list or map and empty() will return a boolean indicating whether or not the given list or map is void of items.

{% if(length([1, 2, 3]) == 3) %}
	The list has 3 items!
{% end %}

Getting a type

A value's type can be obtained using type(), which returns a string with the type name of the given value.

{{ type("hello") }}

The above code would result in the word string in the resulting HTML.

License

Spice is licensed under the MIT license.

Credits