File: write-string.2gg

package info (click to toggle)
golf 601.4.41-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,824 kB
  • sloc: ansic: 20,020; sh: 1,171; makefile: 292
file content (302 lines) | stat: -rw-r--r-- 4,429 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
.TH GOLF 2gg $VERSION $DATE Development Tools
.SH NAME
write-string \-  (strings)
.SH PURPOSE
Create complex strings.

.SH SYNTAX

.RS 4
.EX

write-string <string>

<any code>

end-write-string [ notrim ]

.EE
.RE

.SH DESCRIPTION
Output of any Golf code can be written into <string> with write-string. In between write-string and end-write-string you can write <any Golf code>. For instance you can use database queries, conditional statements etc., just as you would for any other Golf code. 

.LP
.B SHORTCUT CODE
.LP

Note that instead of write-string you can also use a shortcut "(("  (and instead of end-write-string you can use "))"  ), for example here a string "fname" holds a full path of a file named "config-install.golf" under the application home directory (see \fBdirectories\fP):

.RS 4
.EX

get-app directory to home_dir
(( fname
@<<print-out home_dir>>/config-install.golf
))

.EE
.RE


.LP
.B TRIMMING
.LP

Just like with all other Golf code, every line is trimmed both on left and write, so this:

.RS 4
.EX

(( mystr
@Some string
))

.EE
.RE

is the same as:

.RS 4
.EX

(( mystr
        @Some string <whitespaces>
))

.EE
.RE

write-string (or "((") statement must always be on a line by itself (and so does end-write-string, or "))" statement). The string being built starts with the line following write-string, and ends with the line immediately prior to end-write-string. 

All trailing empty lines are removed, for example:

.RS 4
.EX

(( mystr
        @My string
        @
        @
))

.EE
.RE

the above string would have two trailing empty lines, however they will be removed. If you want to skip trimming the trailing whitespaces, use "notrim" clause in end-write-string.

.SH EXAMPLES

.B - Simple

A simple example:

.RS 4
.EX

set-string my_str="world"
set-string my_str1="and have a nice day too!"

write-string result_str
@Hello <<print-out my_str>> (<<print-out my_str1>>)
end-write-string

print-out result_str

.EE
.RE

The output is 

.RS 4
.EX

Hello world (and have a nice day too!)

.EE
.RE



.B - Using code inside

Here is using Golf code inside write-string, including database query and conditional statements to produce different strings at run-time:

.RS 4
.EX

get-param selector

set-string my_str="world"

write-string result_str
    if-true selector equal "simple"
        @Hello <<print-out my_string>> (and have a nice day too!)
    else-if selector equal "database"
        run-query @db="select name from employee" output name
            @Hello <<print-out name>>
            @<br/>
        end-query
    else-if
        @No message
    end-if
end-write-string

print-out result_str

.EE
.RE

If selector variable is "simple", as in URL 

.RS 4
.EX

https://mysite.com/<app name>/some-service?selector=simple

.EE
.RE

the result is 

.RS 4
.EX

Hello world (and have a nice day too!)

.EE
.RE

If selector variable is "database", as in URL 

.RS 4
.EX

https://mysite.com/<app name>/some-service?selector=database

.EE
.RE

the result may be (assuming "Linda" and "John" are the two employees selected):

.RS 4
.EX

Hello Linda
<br/>
Hello John
<br/>

.EE
.RE

If selector variable is anything else, as in URL 

.RS 4
.EX

https://mysite.com/<app name>/some-service?selector=something_else

.EE
.RE

the result is 

.RS 4
.EX

No message

.EE
.RE


.B - Using call-handlers calls inside

The following uses a call-handler inside write-string:

.RS 4
.EX

set-string result_str=""
write-string result_str
    @<<print-out "Result from other-service">> is <<call-handler "/other-service">>
end-write-string
print-out result_str

.EE
.RE

The "other-service" may be:

.RS 4
.EX

begin-handler /other-service public
    @"Hello from other service"
end-handler

.EE
.RE

The output:

.RS 4
.EX

Result from other-service is Hello from other service

.EE
.RE



.B - Nesting

An example to nest write-strings:

.RS 4
.EX

write-string str1
    @Hi!
    write-string str2
        @Hi Again!
   end-write-string
   print-out str2
end-write-string
print-out str1

.EE
.RE

The result is 

.RS 4
.EX

Hi!
Hi Again!

.EE
.RE

.SH SEE ALSO
 Strings

\fBconcatenate-strings\fP  
\fBcopy-string\fP  
\fBcount-substring\fP  
\fBdelete-string\fP  
\fBlower-string\fP  
\fBnew-string\fP  
\fBread-split\fP  
\fBreplace-string\fP  
\fBset-string\fP  
\fBsplit-string\fP  
\fBstring-length\fP  
\fBtrim-string\fP  
\fBupper-string\fP  
\fBwrite-string\fP   
See all 
\fBdocumentation\fP