Piranya Script is a lightweight programming language designed for embedding logic within HTML. Below is an overview of its key features and syntax.
Script blocks are delimited by {%
to start and %}
to end:
{% ... %}
To output values, you use a "binding":
{%binding 'hello world'%}
Variables are set using the var
keyword. Note the var
keyword is required, even when overriding the value later.
var a = 1;
binding a;
var a = 2;
binding a;
You can create conditional statements in two ways:
Simple if
block:
{%if something%}
{%binding 'Condition met'%}
{%endif%}
Braced if
block:
{%
if (something) {
binding "...";
}
%}
To iterate over a collection:
{%
foreach product in products {
binding product.Title;
}
%}
To run loops in parallel, use parallelforeach
:
{%
parallelforeach product in products {
binding product.Title;
}
%}
Variables in the outer scope (from outside the loop) are locked when read or modified during parallel execution.
Piranya Script is often embedded in HTML. For example:
{%foreach post in posts%}
<div class="post">
<p class="headline">{%binding post.Headline%}</p>
<p class="content">{%binding post.Content%}</p>
</div>
{%endforeach%}
Data can be queried using QueryProvider
. For example:
{%var products = QueryProvider.Query type="Product" category_id=5 cache=true%}
This fetches entities of type Product
with a specific filter (category_id=5
) and caching enabled. The result is stored in the variable products
.
Other key-value parameters can be added as filters in the query.
If you need to create a dictionary containing key-value pairs, you can use SimpleParameterCollection
{%
var map = SimpleParameterCollection;
var map["a"] = 1;
binding map["a"];
%}
Functions are defined using the function
keyword. Example:
{%
var myFunction = function (parameter1, parameter2) {
return parameter1 ~ " " ~ parameter2;
};
%}
You can invoke a function like this:
{%var myVariable = myFunction(a, b)%}
Functions can also contain bindings if needed.