EXAMPLES
This section contains examples of how to use the GoERP template engine. The examples are divided into different categories based on the use case.
This section contains examples of how to use the GoERP template engine. The examples are divided into different categories based on the use case.
This example demonstrates how to create a 2-stage automation process.
data
to the
next stage.{{- $warehouses := .Data.AccountAdminApi.Api.Requests.getWarehouses.Response.Get "data.warehouses" -}}
{{- $wIds := mkStringArray }}
{
"postOperations": [
{{- range $i, $warehouse := $warehouses.Array -}}
{{- $wId := ($warehouse.Get "id").String -}}
{{if $i}},{{end}}{
"ErplyApi.Api.Post.getProductStock{{$i}}": "getProductStock",
"ErplyApi.Api.PostParam.getProductStock{{$i}}.warehouseID": "{{ $wId }}",
"ErplyApi.Api.PostParam.getProductStock{{$i}}.getSuggestedPurchasePrice": "1"
}
{{- $wIds = addToStringArray $wIds $wId -}}
{{ end }}
],
"stageTo": {
"name": "pg-aut2-automation",
"data": {"wids": {{ $wIds | toJson }}}
},
"enabled": true
}
{{- $wids := (.Data.Automation.Request.Get "wids").Array -}}
{{- if .Data.Automation.ParentRequests.ErplyApi -}}
{
"postOperations": [
{{- range $i, $wid := $wids -}}
{{- $whStockResponse := index $.Data.Automation.ParentRequests.ErplyApi (printf "getProductStock%d" $i) -}}
{{- $whStockWithNegativeAmounts := ($whStockResponse.Response.Get "records.#(amountInStock<0)#").Array -}}
{{- if not $whStockWithNegativeAmounts -}}
{{- continue -}}
{{- end -}}
{{if $i}},{{end}}{
"ErplyApi.Api.Post.newRegistraton{{$i}}": "saveInventoryRegistration",
"ErplyApi.Api.PostParam.newRegistraton{{$i}}.warehouseID": "{{ $wid }}",
{{- range $ii, $el := $whStockWithNegativeAmounts -}}
{{- $rowIndex := toInt (add $ii 1) -}}
{{- $rowAmount := ($el.Get "amountInStock").String -}}{{if $ii}},{{end}}
"ErplyApi.Api.PostParam.newRegistraton{{$i}}.productID{{$rowIndex}}": "{{$el.Get "productID"}}",
"ErplyApi.Api.PostParam.newRegistraton{{$i}}.amount{{$rowIndex}}": "{{ replace $rowAmount "-" "" 1 }}",
"ErplyApi.Api.PostParam.newRegistraton{{$i}}.price{{$rowIndex}}": "{{$el.Get "suggestedPurchasePrice"}}"
{{- end -}}
}
{{- end -}}
],
"enabled": true
}
{{- end -}}
There is no way to match multiple values in one run, but you can use the pipe |
operator to run
multiple searches in a row.
friends.#(first!=Dale)#|#(first!=Roger)#.last
// Result: ["Murphy","Smith"]
In template code:
{{ $res.Get "friends.#(first!=Dale)#|#(first!=Roger)#.last" }}
// With more pipes:
{{ $res.Get "friends.#(first!=Dale)#|#(first!=Roger)#.last|@unique|@commaSepStr" }}
// Result: "Murphy,Smith"
While chaining requests:
<input
type="hidden"
name="CustomApi.Api.Query.req2.<-names"
data-preset-val="req1.Response.friends.#(first!=Dale)#|#(first!=Roger)#.last|@unique|@commaSepStr">
{
"name": {
"first": "Tom",
"last": "Anderson"
},
"age": 37,
"children": [
"Sara",
"Alex",
"Jack"
],
"fav.movie": "Deer Hunter",
"friends": [
{
"first": "Dale",
"last": "Murphy",
"age": 44,
"nets": [
"ig",
"fb",
"tw"
]
},
{
"first": "Roger",
"last": "Craig",
"age": 68,
"nets": [
"fb",
"tw"
]
},
{
"first": "Jane",
"last": "Murphy",
"age": 47,
"nets": [
"fb",
"ig"
]
},
{
"first": "John",
"last": "Smith",
"age": 49,
"nets": [
"tw"
]
}
]
}
The regular way exposes all fields to the template - this means anyone can alter the field contents before its being sent. This allows the area to be used for possible phishing attacks.
The best way to hide the fields is to move all functional fields away from the template (in this case into an automation) These automation fields are never exposed to the public and cannot be intercepted.
The following example contains 3 templates:
In this example we only render the form when nothing is sent, we also never expose the automation trigger to the template, thus a user cannot even alter its execution.
We use the input here to trigger the automation, and the only value that is being passed to the e-mailer.
<h1>Secure emailer</h1>
<p>Email from public/b2b without exposing the fields for editing</p>
<!-- Optional, we render the form only once and display a message once its already sent -->
{{ if .Data.Parameters.inputEmailField }}
{{ .Tools.AutomationEvent "check-emailer-automation" (jsonSet `{}` "email" .Data.Parameters.inputEmailField) }}
<h2>Thanks!</h2>
{{ else }}
<h2>Send me something</h2>
<form method="post">
<label for="email">Add your email</label>
<input type="string" id="email" name="inputEmailField">
<button type="submit">Send</button>
</form>
{{ end }}
{{ .Data.Parameters.inputEmailField }}
We read the email from the passed data and use it as the email, rest is hardcoded into the automation. Here we also encode an optional template to the email body.
{
"postOperations": [
{
"EMSApi.SendEmailInput.To": "{{ .Data.Automation.Request.Get "email" }}",
"EMSApi.SendEmailInput.Subject": "Secure emailer demo",
"EMSApi.SendEmailInput.ContentType": "html",
"EMSApi.SendEmailInput.Content": "<encode>{{ .Tools.ExecTemplate "check-done-page" }}</encode>",
"EMSApi.SendEmailInput.IsEncoded": "true"
}
],
"enabled": true
}
The content that will be in the email.
<h1>Thanks! email received</h1>