Skip to content

Commit 046c860

Browse files
committed
Document block constructs in HackBuilder
1 parent 4683e00 commit 046c860

3 files changed

Lines changed: 91 additions & 1 deletion

File tree

docs/_data/nav_docs.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
- id: overview-config-and-factory
55
- id: overview-hack-builder
66
- id: overview-definitions
7+
- title: Hack Builder
8+
items:
9+
- id: hack-builder-blocks
710

811
# n title:, 1 items: per title:, n id: per items:
912
# - title: A

docs/_docs/hack-builder/blocks.md

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
---
2+
docid: hack-builder-blocks
3+
title: Blocks
4+
layout: docs
5+
permalink: /docs/hack-builder/blocks/
6+
---
7+
8+
All forms of blocks can be created with the fundamental constructs - eg an `if`
9+
block can be created with:
10+
11+
``` php
12+
<?hh
13+
$builder
14+
->addLine('if ($condition) {')
15+
->indent()
16+
/* do other stuff */
17+
->unindent()
18+
->ensureNewLine()
19+
->addLine('}');
20+
```
21+
22+
This is verbose and hard to scan through; methods are provided that help with both
23+
for common structures.
24+
25+
If Statements
26+
-------------
27+
28+
``` php
29+
<?hh
30+
$builder
31+
->startIfBlock($condition)
32+
/* do stuff */
33+
->addElseIfBlock($condition)
34+
/* do other stuff */
35+
->addElseBlock()
36+
/* do weird stuff */
37+
->endIfBlock();
38+
```
39+
40+
Switch Statements
41+
-----------------
42+
43+
``` php
44+
<?hh
45+
$builder
46+
->startSwitch('$var')
47+
->addCase('foo')
48+
/* do stuff */
49+
->breakCase()
50+
->addCase('bar')
51+
/* do stuff */
52+
->returnCase('$x', HackBuilderValues::literal())
53+
->addDefault()
54+
->breakCase()
55+
->endSwitch();
56+
```
57+
58+
For-Each Loops
59+
--------------
60+
61+
``` php
62+
<?hh
63+
$builder
64+
->startForeachLoop('$list', '$key', '$value')
65+
/* do stuff */
66+
->endForeachLoop();
67+
```
68+
69+
The second parameter can be null.
70+
71+
Try-Catch Blocks
72+
----------------
73+
74+
``` php
75+
<?hh
76+
$builder
77+
->startTryBlock()
78+
/* do stuff */
79+
->addCatchBlock('SomeException', '$e')
80+
/* do stuff */
81+
->addFinallyBlock()
82+
/* do stuff */
83+
->endTryBlock();
84+
```

docs/_docs/overview/hack-builder.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ As readable code is a goal of Hack Codegen, several whitespace helpers are provi
4545
- `->addMultilineCall($call, $args)`: either renders the call all on one line, or with
4646
one argument on one line, depending on if it can fit within the maximum line length
4747

48+
Finally, you can get the generated code as a string by calling `->getCode()`.
49+
4850
Values
4951
------
5052

@@ -76,10 +78,11 @@ Blocks
7678
Helpers are provided for common block constructs - eg:
7779

7880
``` php
81+
<?hh
7982
$builder
8083
->startIf('$condition')
8184
->addLine('doStuff();')
8285
->endIf()
8386
```
8487

85-
See the `HackBuilder` source for a complete list.
88+
See [the reference documentation](/hack-codegen/docs/hack-builder/blocks/) for details.

0 commit comments

Comments
 (0)