Menu Close

Articles

Level: String Templates

A template is based text that contains special notation to be replaced with meaningful content. This can be created as separate files or embedded into code. Templates can be used for simple things like print a message or generate complex content.

String format

We use the hashtag character “#” to create a placeholder format into a string. We use string format operator “<-” to replace placeholders with numeric or string values. This operator can use a variable to resolve a template.  It can be used with single variables or with tuples.

print ('the number has value #n and is #s' <- (10,'even'))

⇒ the number has value 10 and is even

String placeholders

  • #s is used for strings
  • #n is used for numbers
  • #d is used for date
  • #t is used for time
  • #h is used for control

Using placeholder (#) into a string

When placeholders are presented into the string template the members of the collection are used to fill-in the places with values: In the following example we have 5 placeholders. Last placeholder is used to insert to CR.

output ('#s is #n and #s is #n.' <- ('one',1,'two',2));

=> one is 1 two is 2.

If the format string # is followed by round parentheses #(…). the string is called a template. We can use inside parentheses several other symbols to be more specific about conversion. This can be used to align strings and numbers.

#(x, ap:m.d).

Details:

  • x is one of {s,n,f,t}
  • a is alignment one of {<,>,^},
  • p is the padding character: {‘_’,0}
  • m is the length
  • d is number of decimals

Alignment symbol “a” can be one of:

> is used to align to the right
< is used to align to the left
^ is used to align to center

Format examples:

'#(s, >_:10)'   ; right align string fill with spaces to 10 characters
'#(n, >0:10.2)' ; right align fill with 0 up to 10 digits and use 2 decimals

Unicode Text

Text is new type available in Level2 to define a large string. Text can contain multiple lines of symbols separated with end of line character. A text use Unicode symbols and is optimized for faster search of internal words and symbols. Text can be modified while strings are immutable.

Text Literal

A text literal is not enclosed in double quotes: Instead we use three dots “…” to start a text block. A text literal can be defined on multiple lines and will preserve all content including the end of line and the string indentation. This kind of text can also contains single quotes or double quoted strings.

-- declaration example of a text literal
let
  my_text: Text;
do
 my_text= 
 ... 
 "Opportunity is missed by most people 
  because it is dressed in overalls 
  and looks like work." 
 Thomas Edison 
 ...; 
 print(my_text);
end do;

Output:

"Opportunity is missed by most people 
 because it is dressed in overalls 
 and looks like work."
Thomas Edison 

Text concatenation

Unicode strings and text can be concatenated using the string concatenation operator: “&”. This operator is also used as format operator and template engine. A normal string can be implicit converted into a text. However a text can’t be converted into a string without force casting string(<text_variable>). This may produce good string or gibberish depending on text_variable content.

Template placeholders

Inside a text we can use template literals using format operator #(…). We can use following notations to create a template:

#(key_value, format).

How to use a template?

  • Create a map collection with elements
  • Create the template text using template format
  • Use the format build in function to replace placeholders with values

Example:

procedure test is
  template: Text;
  my_map: Map of String;
begin
  my_map = {"key1":"test","key2":"works!" }; 
  template:"Hey look at this #(key1). it #(key2).";
  print(template.format(my_map));
end procedure;

=> Hey look at this test it works!

Built IN text functions:

  • format(str text, dic dictionary) => text;
  • replace(str text, target string, arrow string) => text;
  • find(str text, patern string) => integer;
  • count(str text, patern string) => integer;
  • length(str text) => integer;

Note: The text also support escape sequences like a normal string. However in a text literal we do not have to escape the quote symbols: (‘) and (“). However we have to escape three dots: “…” inside text to avoid early termination by mistake.

HTML Data Type

It is a goal of Level to have HTML data type. This data type may contain text and template placeholders.

HTML Literals

HTML may be a text on multiple lines. The indentation may be corrected a little bit by the compiler. We can subtract the unnecessary indentation. We define a new operator “…” that will be at the beginning and at the end of the HTML text.

Example:

procedure html(s string, a,b integer, c,d real) is
  my_html:Text;
begin
  my_html=
  ...
  <!DOCTYPE html>
  <html>
    <body>
      <h1>My First Heading</h1>
      <p>My first paragraph.</p>
    </body>
  </html>
  ...;
end procedure;

Usability

Templates greatly increase the productivity. If templates are external files we can load these files into memory and apply formatting. This can generate dynamic html pages or reports that are very appreciated techniques in software industry.

Read next: Level: Pointers & References