[Gambas-user] Use gambas WebPages as general-purpose template engine

Martin Fischer martin.fischer6 at web.de
Tue Sep 19 10:49:22 CEST 2023


Hi fellow gambassins,

I created a demo project that uses gambas WebPages as general-purpose
template engine (means: it does not target web-pages, but just
model->string).

I know that there has been interest in adding a template-engine to
gambas (at least, there is a merge request for it). Maybe this would at
least be worth to be included as sample-project or so.

Here is the readme of the sample project:

------------------------------------------
### Use Gambas WebPages as templates

WebPages are designed to be used in the context of web-applications.
As it turns out, they can also be used as general-purpose template engine
(with some limitations that I will explain below).

#### The model

This sample uses a simple demo model consisting of a list of persons
(CPerson).
This model is made globally available in the module 'MainTemplateModel'
for a template
to access and render it (it is not possible to pass a model to a WebPage
as constructor argument).

#### The main template

The template named 'MainTemplate' is responsible for rendering the
model. It uses static text and
gambas code to do this.
Note that it is possible to use all syntactic constructs defined in
     [Gambas WebPage](https://gambaswiki.org/wiki/doc/webpage)
but constructs that do html-escape before printing something might not
be adequate for
your use-case. This means that if you do not want to rendere a html
document with your template,
avoid the `<%=expression%>` syntax because that will apply html excaping
before printing.
Instead use a block of code like so: `<%Print expression;%>`

#### Usage of sub-templates

The MainTemplate delegates the rendering of person birthday to a
sub-template called
'BirthdateTemplate'. It passes the person's birthdate (a Date object) as
argument to this sub-template.

Within the BirthdateTemplate, you can access the arguments value by
accessing the `_Arg` collection
The runtime puts all arguments passed to a sub-page into this
collection, keyed by the name of the
argument (as used in the calling page)

Note that you might also use the bang-syntax `<%!argName%>` in the
sub-template.
The problem is that it is not possible to manipulate the arguments value
with this syntax.

In our example one param named 'birthdate' is passed to the
BirthdateTemplate which is of type Date.
We want to extract only the date-part of it and format it like so:
     `<%Print Format(_Arg["birthdate"], "dd.mm.yyyy");%>`
Is this case you can not use the bang-syntax. Use a code-block with a
Print inside (like above).

Note that there is a problem here: by accessing the `_Arg` collection,
we break encapsulation.
The name of this collection is an implementation detail of the WebPage
machinery. It might change
without notice in future versions of gambas. Unfortunately, it's the
only way to avoid the
very restrictive bang-syntax at the moment.
---------------------------------------------------


I think WebPages are quite usable as general-purpose template engine.
BUT: usage of the _Arg collection in sub-templates might be a
show-stopper (read the last paragraph of the readme again...).

What do you think?
Does all this make sense to you?


Regards
____________________________________
Martin Fischer


More information about the User mailing list