Piranya Script (Programming Language)

Piranya Script is a lightweight programming language designed for embedding logic within HTML. Below is an overview of its key features and syntax.


Syntax Overview

Script Blocks

Script blocks are delimited by {% to start and %} to end:

{% ... %}

Writing Values

To output values, you use a "binding":

{%binding 'hello world'%}

Variables

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;

Control Structures

Conditional Statements

You can create conditional statements in two ways:

  1. Simple if block:

    {%if something%}
        {%binding 'Condition met'%}
    {%endif%}
    
  2. Braced if block:

    {%
    if (something) {
        binding "...";
    }
    %}
    

Foreach Loops

To iterate over a collection:

{%
foreach product in products {
    binding product.Title;
}
%}

Parallel Foreach Loops

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.


Usage in HTML

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%}

Loading data

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.


Dictionary/Map

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

Defining Functions

Functions are defined using the function keyword. Example:

{%
var myFunction = function (parameter1, parameter2) {
    return parameter1 ~ " " ~ parameter2;
};
%}

Invoking Functions

You can invoke a function like this:

{%var myVariable = myFunction(a, b)%}

Functions can also contain bindings if needed.