Write string

Purpose: Create complex strings.

 write-string <string>

 <any code>

 end-write-string [ notrim ]

Output of any Gliimly code can be written into <string> with write-string. In between write-string and end-write-string you can write <any Gliimly code>. For instance you can use database queries, conditional statements etc., just as you would for any other Gliimly 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.gliim" under the application home directory (see directories):
 get-app directory to home_dir
 (( fname
 @<<p-out home_dir>>/config-install.gliim
 ))

Trimming
Just like with all other Gliimly 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 <<p-out my_str>> (<<p-out my_str1>>)
 end-write-string

 p-out result_str

The output is
Hello world (and have a nice day too!)


- Using code inside
Here is using Gliimly 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 <<p-out my_string>> (and have a nice day too!)
     else-if selector equal "database"
         run-query @db="select name from employee" output name
             @Hello <<p-out name>>
             @<br/>
         end-query
     else-if
         @No message
     end-if
 end-write-string

 p-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 sub-handlers calls inside
The following uses a sub-handler inside write-string:
 set-string result_str=""
 write-string result_str
     @<<p-out "Result from other-service">> is <<sub-handler "/other-service">>
 end-write-string
 p-out result_str

The "other-service" may be:
 begin-handler /other-service
     @"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
    p-out str2
 end-write-string
 p-out str1

The result is
Hi!
Hi Again!

See also
Strings
copy-string  
count-substring  
delete-string  
lower-string  
read-split  
replace-string  
set-string  
split-string  
string-length  
trim-string  
upper-string  
write-string  
See all
documentation


Copyright (c) 2019-2024 Gliim LLC. All contents on this web site is "AS IS" without warranties or guarantees of any kind.