Skip to content
Newer
Older
100644 102 lines (58 sloc) 4.05 KB
9bb5c8a @bsiggelkow RTFM
authored Aug 26, 2011
1 # Jsonify-Rails — Jsonify templates for Rails views [![Build Status](https://secure.travis-ci.org/bsiggelkow/jsonify-rails.png)](http://travis-ci.org/bsiggelkow/jsonify-rails)
bd9c451 @bsiggelkow Initial commit
authored Aug 1, 2011
2
3 [Jsonify](https://github.com/bsiggelkow/jsonify) is to JSON as [Builder](https://github.com/jimweirich/builder) is to XML.
4
5 ## Installation
6
7 `gem install jsonify-rails`
8
9 ## Usage
10
3c4c4f3 @bsiggelkow Minor typo (and trying to get Travis build png to update)
authored Aug 26, 2011
11 Jsonify-Rails provides a Rails 3 template handler allowing you to create
bd9c451 @bsiggelkow Initial commit
authored Aug 1, 2011
12 view templates using Jsonify. Any template with a `.jsonify` will be handled by Rails.
13
14 The Jsonify template handler exposes a `Jsonify::Builder` instance to your template with the `json` variable:
15
16 json.hello do
17 json.world "Jsonify is Working!"
18 end
19
20 Just like with any other template, your Jsonify template will have access to
21 any instance variables that are exposed through the controller.
22
74a5d4b @bsiggelkow Support for pretty generation
authored Aug 17, 2011
23 For optimum performance, Jsonify outputs the generated JSON string in plain format -- no newlines, or spaces -- this is best for production. However, if you want "pretty" output, you can configure Jsonify by adding the following configuration line to `application.rb`.
24
25 config.jsonify_format = :pretty
26
bd9c451 @bsiggelkow Initial commit
authored Aug 1, 2011
27 #### Partials
28
29 You can use partials from Jsonify views, and you can create Jsonify partials. How your Jsonify template uses a partial depends on how the information the partial returns is structured. Keep in mind that any paritial, be it a Jsonify template, erb, or anything else, always a returns its result as a string.
30
31 ##### Jsonify partials
32
33 Any Jsonify partial — that is, the file has a `.jsonify` extension —
34 will return, by design, a string that is valid JSON. It will represent either a JSON object,wrapped in curly braces ( {} ), or a JSON array, wrapped in square brackets ( [] ).
35
36 To incorporate such a value into a Jsonify template, use the `ingest!` method.
37
38 `ingest!` assumes that the value it receives is valid JSON representation. It parses the JSON into a Jsonify object graph, and then adds it to the current Jsonify builder.
39
40 Let's assume this this is your main template, `index.jsonify`:
41
42 json << 1
43 json.ingest! (render :partial=>'my_partial')
44
45 From the first line, you can tell that an array will be created as this line uses the append operator.
6877d0d @laribee correct typo.
laribee authored Nov 9, 2011
46 On the second line, a partial is being added to the builder. Note that you cannot simply place `render :partial ...` on a line by itself as you can do with other templates like `erb` and `haml`. You have to explicitly tell Jsonify to add it to the builder.
bd9c451 @bsiggelkow Initial commit
authored Aug 1, 2011
47
48 Let's say that the partial file, `_my_partial.jsonify`, is as follows:
49
50 json << 3
51 json << 4
52
53 This `json` variable in this partial is a separate distinct `Jsonify::Builder` instance from the `json` variable in the main template.
54
55 > Note: Figure out if a the `json` instance can be passed to the Jsonify partial.
56 > It would make things easier and we wouldn't have to ingest the result.
57
58 This partial results in the following string:
59
60 "[3,4]"
61
62 The `ingest!` method will actually parse this string back into a Jsonify-based object, and adds it to the builder's current state. The resulting output will be:
63
64 "[1,[3,4]]"
65
66 ##### Other partials
67
68 You can also use output from non-Jsonify templates (e.g. erb); just remember that the output from a template is always a string and that you have to tell the builder how to include the result of the partial.
69
70 For example, suppose you have the partial `_today.erb` with the following content:
71
72 <%= Date.today %>
73
74 You can then incorporate this partial into your Jsonify template just as you would any other string value:
75
76 json << 1
77 json << {:date => (render :partial => 'today')}
78
79 renders ...
80
81 [1,{"date":"2011-07-30"}]
82
83
84 ## Related Projects
85 - [Argonaut](https://github.com/jbr/argonaut)
86 - [JSON Builder](https://github.com/dewski/json_builder)
87 - [RABL](https://github.com/nesquena/rabl)
88 - [Representative](https://github.com/mdub/representative)
89 - [Tokamak](https://github.com/abril/tokamak)
90
91 ## License
92
93 This project is released under the MIT license.
94
95 ## Authors
96
97 * [Bill Siggelkow](https://github.com/bsiggelkow)
109b651 @bsiggelkow Version bump and doco
authored Aug 5, 2011
98
99 ## Contributors
100
101 * [Trym Skaar](https://github.com/trym)
Something went wrong with that request. Please try again.