DEV Community

Sempare Limited
Sempare Limited

Posted on

Using loops in the Sempare Template Engine for Delphi

The Sempare Template Engine (available at https://github.com/sempare/sempare-delphi-template-engine and GetIt) is a versatile templating system designed specifically for Delphi developers to streamline the creation and management of dynamic HTML, text, or any other text-based output formats. Whether you are building web applications, reports, or email templates, the Sempare Template Engine offers a powerful yet straightforward solution that integrates with minimal boilerplate into Delphi projects. The template engine has been around since 2019 and has many features for you to explore.

In this tutorial we will review some of the looping statements, namely for and while.

Basic for loops

The basics, as we do in pascal, we can loop over a fixed range:

   <table>
   <% for i := 1 to 10 %>
     <tr><td><% i %></td><tr>
   <% end %>
   </table>
Enter fullscreen mode Exit fullscreen mode

This will produce the output:

  <table>
     <tr><td>1</td></tr>
     <tr><td>2</td></tr>
     ... 3 to 8 ommitted ...
     <tr><td>9</td></tr>
     <tr><td>10</td></tr>
  </ul>
Enter fullscreen mode Exit fullscreen mode

Lets make it more interesting, and use variables:

   <% start_idx := 1; end_idx := 10 %>
   <table>
   <% for i := start_idx to end_idx %>
     <tr><td><% i %></td></tr>
   <% end %>
   </table>
Enter fullscreen mode Exit fullscreen mode

Let us assume the variables are driven by user input, rather than being set in the template. Now we need to also cater for the scenario that no values are present. So we could devise a script like:

   <% if (start_idx >= 0) and (end_idx > 0) %>
     <table>
     <% for i := start_idx to end_idx %>
       <tr><td><% i %></td></tr>
     <% end %>
     </table>
   <% else %>
     <p>There are no values</p>
   <% end %>
Enter fullscreen mode Exit fullscreen mode

This could be translated to the following using extension to the for loop grammar:

     <% for i := start_idx to end_idx %>
         <tr><td><% i %></td></tr>
       <% onbegin %>
         <table>
       <% onend %>
         </table>
       <% onempty %>
        <p>There are no values</p>
     <% end %>
Enter fullscreen mode Exit fullscreen mode

Enumerable for loops

For loops are flexible and can enumerate over different structures such as arrays, lists, dictionaries, JSON arrays, and can be extended to enumerate over custom types as well.

When it comes to enumeration, there are also two variations of the for loop, namely 'for in' and 'for of'. Lets have a quick look at some examples:

<% arr := [ 5, 4, 3, 2, 1 ] %>
<% for i in arr ; i ; betweenitems %>, <% end %>
Enter fullscreen mode Exit fullscreen mode

This will output:

0, 1, 2, 3, 4, 5
Enter fullscreen mode Exit fullscreen mode

This is very similar to JavaScript behaviour.

<% arr := [ 5, 4, 3, 2, 1 ] %>
<% for i of arr ; i ; betweenitems %>, <% end %>
Enter fullscreen mode Exit fullscreen mode

This will yield:

5, 4, 3, 2, 1
Enter fullscreen mode Exit fullscreen mode

Looping over dictionaries

Assume we have the following:

type
   TPerson = record
     Name: string;
   end;
var LUsers : TDictionary<string, TPerson>;
var LTemplate:string := '...'; // listed below
//
writeln(Template.Eval(LTemplate, LUsers));
Enter fullscreen mode Exit fullscreen mode

the template:

<% for email in _ %>
  Email: <% email %> Name: <% _[email].Name %> 
<% end %>
Enter fullscreen mode Exit fullscreen mode

The magic _ references the TDictionary passed to the template.

Consider we may only be interested in the names, we could change the template to:

<% for person of _ %>
  Name: <% person.Name %> 
<% end %>
Enter fullscreen mode Exit fullscreen mode

More documentation is available at: https://github.com/sempare/sempare-delphi-template-engine/blob/main/docs/statements.md#for

While loops

A while statement is a loop based on a condition being true.

   <% i := 0; while i < 10; i; betweenitems %>, <% end %>
Enter fullscreen mode Exit fullscreen mode

Breaking out of a loop

As in Pascal, we can use the break statement to break out of a for or while loop.

   <% for i := 1 to 10 %>
      <% if i = 5 ; break ; end %>
      <% i %>
   <% end %>
Enter fullscreen mode Exit fullscreen mode

The above will show the values 1 to 4.

More documentation is available at: https://github.com/sempare/sempare-delphi-template-engine/blob/main/docs/statements.md#while

Skipping iterations

Similar to the above, we can use the continue statement to skip iterations in for or while loops.

   <% for i := 1 to 10 %>
      <% if i mod 2 == 0 ; continue ; end %>
      <% i %>
   <% end %>
Enter fullscreen mode Exit fullscreen mode

The above will show all the odd numbers.

Conclusion

The Sempare Template Engine offers a lot of rich functionality, allowing you to manipulate text output to fit your needs.

Sponsorship Required

Please help us maintain the project by supporting Sempare via GitHub sponsors (https://github.com/sponsors/sempare) or via our payment link (https://buy.stripe.com/aEU7t61N88pffQIdQQ). Sponsors can obtain access to our integrated IDE wizard for RAD Studio.

Top comments (0)