Write string
Purpose: Create complex strings.
write-string <string>
<any code>
end-write-string [ notrim ]
Copied!
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.
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
))
Copied!
Just like with all other Gliimly code, every line is trimmed both on left and write, so this:
(( mystr
@Some string
))
Copied!
is the same as:
(( mystr
@Some string <whitespaces>
))
Copied!
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
@
@
))
Copied!
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.
- 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
Copied!
The output is
Hello world (and have a nice day too!)
Copied!
- 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
Copied!
If selector variable is "simple", as in URL
https://mysite.com/<app name>/some-service?selector=simple
Copied!
the result is
Hello world (and have a nice day too!)
Copied!
If selector variable is "database", as in URL
https://mysite.com/<app name>/some-service?selector=database
Copied!
the result may be (assuming "Linda" and "John" are the two employees selected):
Hello Linda
<br/>
Hello John
<br/>
Copied!
If selector variable is anything else, as in URL
https://mysite.com/<app name>/some-service?selector=something_else
Copied!
the result is
No message
Copied!
- Using call-handlers calls inside
The following uses a call-handler inside write-string:
set-string result_str=""
write-string result_str
@<<p-out "Result from other-service">> is <<call-handler "/other-service">>
end-write-string
p-out result_str
Copied!
The "other-service" may be:
begin-handler /other-service public
@"Hello from other service"
end-handler
Copied!
The output:
Result from other-service is Hello from other service
Copied!
- 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
Copied!
The result is
Hi!
Hi Again!
Copied!
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.