An open API service indexing awesome lists of open source software.

https://github.com/mcasimir/swig-traverse

A Swig plugin providing tags to traverse tree structures.
https://github.com/mcasimir/swig-traverse

Last synced: 2 months ago
JSON representation

A Swig plugin providing tags to traverse tree structures.

Awesome Lists containing this project

README

        

# swig-traverse

A [Swig](http://paularmstrong.github.io/swig/) plugin providing tags to traverse tree structures.

**swig-traverse** assumes tree nodes to be objects with a `children` property that
is either `undefined` or an array of child nodes.

``` js
var tree = {
// ...
children: [
{
// ...
children: [
// ...
]
}
]
};
```

## Install

``` sh
npm i swig-traverse --save-dev
```

## Usage

``` js
var swig = require('swig'),
swigTraverse = require('swig-traverse');

swigTraverse( swig );
```

``` html
{% traverse node in tree -%}

{% postvisit -%}

{% endtraverse -%}
```

Note. `{% postvisit %}` section is optional:

``` html
{% traverse node in tree -%}

{% endtraverse -%}
```

You can pass array as tree, in that case it will considered to be a _"children"_ array. Useful
to exclude root from traversing:

```
{% traverse child in node.children -%}

{% endtraverse -%}
```

### Example

``` html
Description:

{{ node.description }}

Submodules:


    {% traverse child in node.children -%}
    {% if child.type == 'module' %}

  • {{child.title}}

      {% endif -%}
      {% postvisit -%}
      {% if child.type == 'module' %}


  • {% endif -%}
    {% endtraverse -%}

```

## License

Released under the [MIT license](https://github.com/mcasimir/swig-traverse/blob/master/LICENSE).