GOLF(2gg)

πŸ“¦ version 601.4.41, in ubuntu questing

πŸ’– GOLF(2gg) β€” write-string πŸ’–

okay bestie, welcome to the 2gg manual, section Development. sit down, hydrate, and let's talk about building strings. 🫢


✨ NAME ✨

write-string β€” and it lives in the strings family πŸŽ€


🌸 PURPOSE 🌸

Create complex strings. that's it. that's the whole vibe. no cap. πŸ’…


πŸ¦‹ SYNTAX πŸ¦‹

write-string <string>

<any code>

end-write-string [ notrim ]

πŸ‘‘ DESCRIPTION πŸ‘‘

So here's the tea: output of any Golf code can be written into <string> with write-string. And in between write-string and end-write-string you can write <any Golf code>. fr fr β€” like, you can use database queries, conditional statements etc., just as you would for any other Golf code. it's giving ✨unlimited power✨.

πŸ’« SHORTCUT CODE πŸ’«

not me about to save you keystrokes πŸ₯° β€” 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 directories):

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

lowkey iconic. πŸ”₯

🧚 TRIMMING 🧚

Just like with all other Golf code, every line is trimmed both on left and write (yes bestie, on write, we're keeping it exactly as written 😭), so this:

(( mystr
@Some string
))

is the same as:

(( mystr
        @Some string <whitespaces>
))

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:

(( mystr
        @My string
        @
        @
))

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.

⚠️ reminder bestie: "notrim" means you keep the trailing whitespace. that's a choice and we respect it. 🫑

🌈 EXAMPLES 🌈

πŸ“ Simple

A simple example:

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

The output is

Hello world (and have a nice day too!)

ate. πŸ’…

πŸ’œ Using code inside

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

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

If selector variable is "simple", as in URL

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

the result is

Hello world (and have a nice day too!)

If selector variable is "database", as in URL

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

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

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

If selector variable is anything else, as in URL

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

the result is

No message

three different vibes, one handler. we love a versatile queen. πŸ‘‘

πŸŽ€ Using call-handlers calls inside

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

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

The "other-service" may be:

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

The output:

Result from other-service is Hello from other service

πŸ¦‹ Nesting

An example to nest write-strings:

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

The result is

Hi!
Hi Again!

strings inside strings, it's giving inception but make it cute 🧚✨


⭐ SEE ALSO ⭐

  • Strings

concatenate-strings copy-string count-substring delete-string lower-string new-string read-split replace-string set-string split-string string-length trim-string upper-string write-string

See all documentation πŸ“š


okay bestie that's the whole page!! no notes, 10/10, go build something gorgeous with it πŸ’–πŸŒΈπŸ¦‹

this masterpiece of a page is officially brought to you by version $VERSION, dated $DATE, and it's the GOLF(2gg) manual, Development section. periodt. πŸ’…βœ¨

πŸ“¦ where this page comes from