Tools helper

Tools is a special helper that has more powerful access to the contents of the context.

Generates a full path link to the source static file. Makes sure the link does not break if the site is moved to a custom domain or if the link contains custom path parameters.

This is a replacement for the older staticFileLink and takes care multiple shortcoming of the original.

<script src="{{ .Tools.StaticLink " AAA-Etag-js" }}"></script>

IsPath

Checks if we are currently navigating to the given custom path. Counts all 3 custom paths.

{{ .Tools.IsPath "path1" }}
{{ .Tools.IsPath "path1/path2" }}
{{ .Tools.IsPath "path1/path2/path3" }}

Also used for automatic sourcemap importing for sub-paths.

GetNavPath

Generates a valid link path that composes of the current client code, language and the given path value. Counts in possible navigation differences from custom domains.

{{ .Tools.GetNavPath "da-csv-export" }}

This would generate the correct paths according to the current handler

/104146/en/da-csv-export <!-- regular -->
/public/104146/en/da-csv-export?some=value <!-- public -->
/en/da-csv-export?some=value <!-- custom domain -->
Info

Optional language parameter is available from version 1.215+

{{ .Tools.GetNavPath "da-csv-export" "fi" }}
/104146/fi/da-csv-export <!-- regular -->
/public/104146/fi/da-csv-export?some=value <!-- public -->
/fi/da-csv-export?some=value <!-- custom domain -->

Browser

A separate function to parse data from the user agent header to specific data points. Can be used to determine if dealing with a mobile device or attempt to identify the platform used.

Browser

{{ .Browser.Name }}
{{ .Browser.Version }}
{{ .Browser.ShortVersion }}

Device

{{ .Browser.Device.IsConsole }}
{{ .Browser.Device.IsMobile }}
{{ .Browser.Device.IsTablet }}

Platform

{{ .Browser.Platform.Name }}
{{ .Browser.Platform.Version }}

Bot

{{ .Browser.Bot.Name }}
{{ .Browser.Bot.IsBot }}
{{ .Browser.Bot.Why }}

ExecTemplate

Executes template that can be assigned to the variable and could be passed to other data points (e.g. email body).

First of all we need to have template with payload:

{{ define "pg-in-build-template-parser-partial" }}

<h2>Hello, {{ .Session.ClientCode }}</h2>

<div align="left">
    Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut
    labore et dolore magna aliqua.
</div>
<div align="right">
    Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut
    labore et dolore magna aliqua.
</div>
<div align="center">
    Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut
    labore et dolore magna aliqua.
</div>
<div align="justify">
    Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut
    labore et dolore magna aliqua.
</div>

<h2>Using API calls inside executed template</h2>

{{ $warehouses := .Data.AccountAdminApi.Api.Requests.getWarehouses.Response.Get "data.warehouses" }}

<ul>
    {{ range $w := $warehouses.Array }}
    <li>{{ $w.Get "id" }}</li>
    {{ end }}
</ul>

{{ end }}

Then we can execute that template on other page and use resulting content wherever needed:

{{ template "pg-layout" . }}

{{ define "body" }}

<input type="hidden" name="AccountAdminApi.Api.Get.getWarehouses" value="v1/warehouse" data-preset-val="v1/warehouse" />
<input type="hidden" name="ErplyApi.Api.Post.getProducts" value="getProducts" data-preset-val="getProducts" />
<input type="hidden" name="ErplyApi.Api.PostParam.getProducts.getStockInfo" value="1" data-preset-val="1" />

<h1>Executing template from page...</h1>

<!-- We can pass any data to the function, like we do with regular partials inside templates -->
{{ $result := .Tools.ExecTemplate "pg-in-build-template-parser-partial" (.) }}
<pre>{{ $result }}</pre>

And result would be:

<h2>Hello, 104670</h2>

<div align="left">
    Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut
    labore et dolore magna aliqua.
</div>
<div align="right">
    Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut
    labore et dolore magna aliqua.
</div>
<div align="center">
    Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut
    labore et dolore magna aliqua.
</div>
<div align="justify">
    Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut
    labore et dolore magna aliqua.
</div>

<h2>Using API calls inside executed template</h2>

<ul>
    <li>1</li>
    <li>3</li>
    <li>4</li>
    <li>10</li>
    <li>11</li>
    <li>12</li>
    <li>13</li>
</ul>

CSP

Used to set server generated nonce to script and styles. When added correctly then the console error messages should no longer appear.

Link sources

<link rel="stylesheet" {{ .Tools.CSP }} href="{{ .Tools.StaticLink "Test-css" }}">
<script {{ .Tools.CSP }} src="{{ .Tools.StaticLink "add-billing-statement-js" }}"></script>

Inline

<head>
    <style {{ .Tools.CSP }}>
        .color-red {
            color: red;
        }
    </style>
</head>

<script {{ .Tools.CSP }}>
    console.log("test");
</script>

B2bAuthDomain (b2b specific)

While using Automat API to make b2b authentication calls, we need to pass the domain which is available only in the template (and we don’t have connection with template in the API calls). To get the current domain we can use .Tools.B2bAuthDomain and pass it to the necessary field.

<input type="hidden" name="AutomatApi.Api.Post.register" value="v1/b2b/register-user">
<input type="hidden" name="AutomatApi.Api.Json.register.string.domain" value="{{ .Tools.B2bAuthDomain }}">

IsModuleConfigured

Used to check if a specific module is currently enabled.

Note that this check is always checking against the current application (from the currently rendered template), so if the uuid being checked is a module for another app then it will always return false for it.

As a parameter give the uuid of the module you want to check. Returns a boolean value.

{{ .Tools.IsModuleConfigured "0cea1a50-5462-4fbc-9160-20ba082ec2be" }}

We can then generate some logic based on its result

{{ if .Tools.IsModuleConfigured "0cea1a50-5462-4fbc-9160-20ba082ec2be" }}
    <p>Extra features enabled</p>
{{ else }}
    <p>Extra features disabled</p>
{{ end }}

GetEnabledModules

Can be used to read a list of currently enabled modules.

Note that this check is always checking against the current application (from the currently rendered template), so it will not return uuid’s for modules that have been enabled for other applications.

Returns a comma separated list(string) of uuid’s.

{{ .Tools.GetEnabledModules }}
<!-- = uuid1,uuid2 -->

We can explode the result with split when we need to create a list out of it.

<ul>
{{ range (split .Tools.GetEnabledModules ",") }}
    <li>{{ . }}</li>
{{ end }}
</ul>