Skip to content

Commit 3f88a5c

Browse files
Alex Helfetsparkprime
authored andcommitted
Added importstr to the tutorial.
1 parent 3210d6c commit 3f88a5c

File tree

4 files changed

+46
-11
lines changed

4 files changed

+46
-11
lines changed

doc/docs/tutorial.html

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -167,24 +167,56 @@ <h2 id="modularity">Modularity and Encapsulation</h2>
167167

168168
<p>As the amount of JSON grows, its size makes it harder to manage. Jsonnet has various constructs
169169
to help. A file can be broken into parts, as one Jsonnet file can import other Jsonnet files (and
170-
therefore other JSON files). Values can be held in local variables and fields, which are only
171-
visible within their scopes. Functions can be defined to factor out common descriptions, and error
172-
statements can be used to validate inputs. Jsonnet provides a standard library that is implicitly
173-
imported and contains useful functions for data manipulation, among other things.</p>
170+
therefore other JSON files), or a whole text file can be imported as a string value. Values can be
171+
held in local variables and fields, which are only visible within their scopes. Functions can be
172+
defined to factor out common descriptions, and error statements can be used to validate inputs.
173+
Jsonnet provides a standard library that is implicitly imported and contains useful functions for
174+
data manipulation, among other things.</p>
174175

175176
<h3 id="imports">Imports</h3>
176177

177178
<p>The first example below factors out some cocktails into a separate file. This may be useful to
178-
allow concurrent modifications by different mixologists. The <code>import</code> construct yields
179-
the content of the <tt>martinis.libsonnet</tt> file. The <code>+</code> operator is object
180-
concatenation, which combines two objects to form a single object. Note that the Cosmopolitan field
181-
is defined in both files, so the one on the right hand side is used. This means that
182-
<tt>bar_menu.jsonnet</tt> has overridden the recipe from <tt>martinis.libsonnet</tt> with a different
183-
recipe (one that uses Cointreau instead of Triple Sec, among other changes).</p>
179+
allow concurrent modifications by different mixologists.</p>
184180

181+
<p>The <code>import</code> construct yields the content of the <tt>martinis.libsonnet</tt> file
182+
evaluated as a Jsonnet object. The <code>+</code> operator is object concatenation, which combines
183+
two objects to form a single object. Note that the Cosmopolitan field is defined in both files, so
184+
the one on the right hand side is used. This means that <tt>bar_menu.jsonnet</tt> has overridden
185+
the recipe from <tt>martinis.libsonnet</tt> with a different recipe (one that uses Cointreau instead
186+
of Triple Sec, among other changes).</p>
187+
188+
<p>The <code>importstr</code> construct yields a string with the content of
189+
the <tt>bar_menu.6.manhattan_garnish.txt</tt> file without any processing beyond UTF-8 decoding.</p>
190+
191+
<table>
192+
<tr> <td><span class="TableHeader">Input (Jsonnet)</span></td> </tr>
193+
194+
<tr>
195+
<td>
185196
<pre><code>{% include examples/martinis.libsonnet %}</code></pre>
197+
</td>
198+
</tr>
186199

200+
<tr>
201+
<td>
202+
<pre><code>// bar_menu.6.manhattan_garnish.txt
203+
{% include examples/bar_menu.6.manhattan_garnish.txt %}</code></pre>
204+
</td>
205+
</tr>
206+
207+
<tr>
208+
<td>
187209
<pre><code>{% include examples/bar_menu.6.jsonnet %}</code></pre>
210+
</td>
211+
</tr>
212+
213+
<tr><td><span class="TableHeader">Output (JSON)</span></td></tr>
214+
<tr>
215+
<td>
216+
<pre><code>{% include examples/bar_menu.6.jsonnet.golden %}</code></pre>
217+
</td>
218+
</tr>
219+
</table>
188220

189221
<p>By convention, Jsonnet code that is intended only for importing has a <tt>.libsonnet</tt>
190222
extension. This is not enforced, it simply allows you to distinguish between the two kinds of files

examples/BUILD

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ jsonnet_docs_srcs_zip(
1414
"bar_menu.5.jsonnet",
1515
"bar_menu.5.jsonnet.golden",
1616
"bar_menu.6.jsonnet",
17+
"bar_menu.6.manhattan_garnish.txt",
18+
"bar_menu.6.jsonnet.golden",
1719
"bar_menu_utils.libsonnet",
1820
"bar_menu.7.jsonnet",
1921
"bar_menu.7.jsonnet.golden",

examples/bar_menu.6.jsonnet

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ limitations under the License.
2323
{ kind: "Sweet Red Vermouth", qty: 1 },
2424
{ kind: "Angostura", qty: "dash" },
2525
],
26-
garnish: "Maraschino Cherry",
26+
garnish: importstr "bar_menu.6.manhattan_garnish.txt",
2727
served: "Straight Up",
2828
},
2929
Cosmopolitan: {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Maraschino Cherry

0 commit comments

Comments
 (0)