This content originally appeared on DEV Community and was authored by Sempare Limited
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 %>
This template will result in:
nomorespaces
Similarly, there is:
<% ignorenl %>
this.
is.
a.
test.
<% end %>
This will result in:
this.is.a.test.
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 %>
This will result in:
this will be displayed
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>
Without the hints, you will see output like:
<ul>
<li>2</li>
<li>3</li>
<li>4</li>
</ul>
With the hint, we will get a cleaner output:
<ul>
<li>2</li>
<li>3</li>
<li>4</li>
</ul>
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.
This content originally appeared on DEV Community and was authored by Sempare Limited
Sempare Limited | Sciencx (2024-10-24T18:19:24+00:00) Eliminating whitespace in Sempare Template Engine for Delphi templates. Retrieved from https://www.scien.cx/2024/10/24/eliminating-whitespace-in-sempare-template-engine-for-delphi-templates/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.