Skip to content
/ JQen Public
forked from HaniAmmar/JQen

A fast and secure template engine for JavaScript

License

Notifications You must be signed in to change notification settings

twoxfh/JQen

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

32 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

JQen

What Is JQen

A JavaScript module that uses Qentem library for fast template rendering.

Features

  • Fast template rendering.
  • Low memory usage.
  • Safe evaluation.
  • Variable replacement with HTML auto-escape.
  • Raw variable replacement without HTML auto-escape.
  • Nested loop (with data grouping and sorting).
  • Nested if condition.
  • Inline if.
  • Math tag.

Usage

Live Test

Templates can be tested live @ JQen Tool

Variable

var Module, data, template = `
<div>{var:v1}</div>
<div>{var:sub-list1[sv1]}</div>
<div>{var:sub-list2[0]}</div>
`;

data = '{"v1":"Qentem","sub-list1":{"sv1":"JQen"},"sub-list2":[77]}';

Module = {
    onRuntimeInitialized: function () {
        document.getElementById("main").innerHTML = Module.JQen_Render(template, data);
    }
};

Math

var Module, data, template = `
<div>0.1+0.2 is: {math: 0.1  +   0.2 }</div>
<div>{var:Equation} = {math:{var:Equation}}; (1+8+1)</div>
<div>6^2 = {math:6^2}</div>
<div>--1 = {math:--1}</div>
<div>{var:one}+{var:three} = {math:{var:one}+{var:three}}</div>
<div>9 % 5 = {math:9 % 5}</div>
`;

data = '{"Equation":"1+4*2+1","one":"1","three":"3"}';

Module = {
    onRuntimeInitialized: function () {
        document.getElementById("main").innerHTML = Module.JQen_Render(template, data);
    }
};

Inline If

var Module, data, template = `
<div>{if case="{var:one} + {var:two} >= {var:three}" true="3" false="not three"}</div>
<div>{if case="{var:one}" true="{var:one}" false="not one"}</div>
{if case="{var:name} == Qentem" true="<div>Qentem!</div>"}
`;

data = '{"one":"1","two":"2","three":"3","name":"Qentem"}';

Module = {
    onRuntimeInitialized: function () {
        document.getElementById("main").innerHTML = Module.JQen_Render(template, data);
    }
};

Loop

var Module, data, template = `
<loop set="object" value="item">
    <div>item[var1] item[var2] item[var3] item[var4]</div>
</loop>
<br />
<loop set="array" value="item">
    <div>item[0] item[1] item[2] item[3]</div>
</loop>
`;

data = `
{
    "object": [
        {
            "var1": "value1",
            "var2": "value2",
            "var3": "value3",
            "var4": "value4"
        },
        {
            "var1": "value5",
            "var2": "value6",
            "var3": "value7",
            "var4": "value8"
        }
    ],
    "array": [
        [
            "value10",
            "value20",
            "value30",
            "value40"
        ],
        [
            "value100",
            "value200",
            "value300",
            "value400"
        ]
    ]
}`;

Module = {
    onRuntimeInitialized: function () {
        document.getElementById("main").innerHTML = Module.JQen_Render(template, data);
    }
};

If Condition

var Module, data, template = `
<if case="{var:0} == 0">
<div>Zero!</div>
</if>

<if case="{var:1} == 0">
Zero!
<else />
<div>Not {var:0} but {var:1}.</div>
</if>

<if case="{var:2} == 0">
Zero!
<elseif case="{var:2} == 2" />
<div>Two!</div>
<else />
Not zero or one.
</if>

<if case="{var:2} == 0">
Zero!
<elseif case="{var:2} == 5" />
Two!
<elseif case="{var:3} == 3" />
<div>{var:3}</div>
<else />
Not zero or one or two.
</if>`;

data = "[0,1,2,3]";

Module = {
    onRuntimeInitialized: function () {
        document.getElementById("main").innerHTML = Module.JQen_Render(template, data)
    }
};

Live Testing

Templates can be tested live @ JQen Tool

Page Example

<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>JQen Page Example</title>
</head>

<body>
    <div id="main"></div>
    <script>
        var Module, data, template = `
        <h2>Students</h2>
        <loop value="department_val">
            <h3>Major: department_val[major]</h3>
            <ul>
            <loop set="department_val[students]" value="student_val">
                <li>
                    <span>Name: student_val[Name]</span>
                    <span>
                        GPA: student_val[GPA]
                        <if case="student_val[GPA] < 2.5"> (Inform adviser!)
                        <elseif case="student_val[GPA] >= 3.5" /> (President\'s List!)
                        <elseif case="student_val[GPA] >= 3.0" /> (Dean\'s List!)
                        </if>
                    </span>
                </li>
            </loop>
            </ul>
        </loop>`;

        data = [
            {
                'major': 'Computer Science',
                'students': [
                    { 'Name': 'Oliver', 'GPA': 3.2 },
                    { 'Name': 'Jonah', 'GPA': 3.8 },
                    { 'Name': 'Jack', 'GPA': 2.8 }
                ]
            },
            {
                'major': 'Math',
                'students': [
                    { 'Name': 'Maxim', 'GPA': 3.0 },
                    { 'Name': 'Cole', 'GPA': 2.5 },
                    { 'Name': 'Claire', 'GPA': 2.4 }
                ]
            }
        ];

        Module = {
            onRuntimeInitialized: function () {
                document.getElementById("main").innerHTML = Module.JQen_Render(template, JSON.stringify(data));
            }
        };
    </script>
    <script src="JQen.js"></script>
</body>

</html>

Reference

Syntax @ Qentem-Engine/Template.md.

Build WASM

First: Install Emscripten

Then:

git submodule update --init

mkdir Build
em++ -Os -fno-exceptions -msimd128 -D QENTEM_MSIMD128=1 --bind -I ./qentem/Include ./Source/QLib.cpp -o ./Build/JQen.js

Note: A compiled WASM file is @ releases

License

MIT License

Copyright (c) 2020 Hani Ammar

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

About

A fast and secure template engine for JavaScript

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • C++ 100.0%