slim-template/slim — pull request #572 by dmke (2014-12-23 16:47:24+00:00)
Title: `--data` command line option to pass locals via JSON string

Hi,

this PR resolves around a feature request in cognitom/gulp-slim#20 which needs to pass data over the language barrier (JS→Ruby).

After some superficial research I found another command line option much nicer to implement that, say, environment variable trickery:

```
$ cat template.slim
p Hello #{name}!
$ slimrb --data '{"name":"from slim"}' template.slim

Hello from slim!

```

Another option I tried were multiple `--data` options (much like `--option`: `slimrb --data name="from slim" --data foo="bar"`), but I found the code on both sides (`Slim::Command` and the gulp-slim glue to be too messy).

Other things I'll just leave this here for further discussion:

- Maybe the option should be named `--json` to enable other formats like YAML? Or just `--locals`?
- Currently, this addition only works if neither `--erb`, nor `--compile` are given, as I did not find an easy way to inject the local variables hash into `Slim::ErbConverter` nor `Slim::Engine` (just `Slim::Template#render` seems to accept these).
- There are no tests for `slimrb`/`Slim::Command` (yet). I'm going on a train tomorrow, so maybe there will be some tests later that day :wink:

Have some great Holidays,
Dominik