GOLF(2gg)

NAME
write-string - (strings)

PURPOSE
Create complex strings.

SYNTAX

        write-string <string>

        <any code>

        end-write-string [ notrim ]

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.

SHORTCUT CODE

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 directories):

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

TRIMMING

Just like with all other Golf code, every line is trimmed both on left and write, 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.

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!)

- 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

- 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!

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