DEV Community

Sempare Limited
Sempare Limited

Posted on

Eliminating whitespace in Sempare Template Engine for Delphi templates

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 illustrate how whitespace (space and newlines) can be eliminated from templates managed by the Sempare Template Engine.

Typically, when using templates, there may be more whitespace and newlines in a template that you really want. Let's explore a few options.

<% ignorews %>
  no more spaces
<% end %>
Enter fullscreen mode Exit fullscreen mode

This template will result in:

nomorespaces
Enter fullscreen mode Exit fullscreen mode

Similarly, there is:

<% ignorenl %>
this.
is.
a.
test.
<% end %>
Enter fullscreen mode Exit fullscreen mode

This will result in:

this.is.a.test.
Enter fullscreen mode Exit fullscreen mode

These are a bit crude.

When it comes to the normal statements, we have a better alternative that you can simply add a - immediately after the script open tag.

    <%- if true %>
    this will be displayed
    <% else %>
    this won't
    <% end %>
Enter fullscreen mode Exit fullscreen mode

This will result in:

    this will be displayed
Enter fullscreen mode Exit fullscreen mode

The behaviour of the <%- is as follows:

  • the whitesapce before the '<%-' will be removed
  • the whitespace including the newline after true '%>' will be removed
  • the whitespace before and after, including newline around <% else %> will be removed
  • the whitespace before and after, including newline around <% end %> will be removed

Nested statements will need to have the hint applied as well.

    <ul>
    <%- for x := 1 to 3 %>
       <%- y := x + 1 %>
       <li><%- y %></li>
    <% end %>
    </ul>
Enter fullscreen mode Exit fullscreen mode

Without the hints, you will see output like:

    <ul>


      <li>2</li>

      <li>3</li>

      <li>4</li>


    </ul>
Enter fullscreen mode Exit fullscreen mode

With the hint, we will get a cleaner output:

    <ul>
      <li>2</li>
      <li>3</li>
      <li>4</li>
    </ul>
Enter fullscreen mode Exit fullscreen mode

Conclusion

In summary, the template engine provides a few alternatives to help minimise the whitespace generated by templates.

The Sempare Template Engine is very flexible, configurable and extensible, allowing for a good seperation of concerns between your business logic that will access data, and the templating engine that will render/present your data as required.

The template engine is available on https://github.com/sempare/sempare-delphi-template-engine and via Embarcadero GetIt.

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)