{% extends "reactive_example_base.html" %} {% load reactive %} {% block title %}Basic Example{% endblock %} {% block content %}

Loading page, and no JavaScript errors so far...

{% with test_template_var="success" initial_first_float=1.5 initial_second_float=2.3 %} {% #block %} {% #block %} Test1 {% #/def text_dsadf=test_template_var %} {% #/def test_var = 'test' %} {% #/def test_array= ['str1', 'str2', 'str3'] %} {%#%} {%/%} Test2 {% #element span %} {% #/def nested_var ='nested' %} {% for element in example_list %} {{ element }} {% #/print test_var %} {% endfor %} {% #/print 'Test3' %} {% #/print nested_var %} {% /element %} {% #/def test_bool=True %} {% #element span %} {% #/print test_bool %} {% /element %} {% #element span %} {% #if test_bool %} Test4 {% #/print test_var %} {% /if %} {% /element %} {% if True %} {% #element span %} {% #/print test_array %} {% #for element in test_array %} {% #/def element2=2 %} {% #/def element3 =element %} {% #/print element2 %} {% #/print element3 %} {% /for %} {% /element %} {% endif %} {% comment %} Comment test {% endcomment %} {%#%} {%/%}
Fixed array:
{% #element section %} Dynamic array: (contains {% #/print len(test_array) %} elements) {% /element %} {% #element section %} Even None is supported: {% #/def test_none=None %} {% #/def test_null=null %} {% /element %} {% #/def number=3 %} {% #/def greeting='Hi' %} {% #/def sum_result=2+number %} {% #element p %} You may also use plus operator, with a number that is updating every second, (with the present function to avoid self referencing, see in the next paragraph):
2+{% #/print number %}={% #/print sum_result %}
"{% #/print greeting %}"+" "+"World"={% #/print greeting+" "+"World" %} {% /element %} {% #element p %} Using the function present(), we can print the values without trigerring rerendering:
The original equation was: 2+{% #/print present(number) %}={% #/print present(sum_result) %}
{% /element %} {% #/def result_choice='number_sum' %} {% #/def some_result=(result_choice==='number_sum')?(2+number):greeting+" "+"World" %}
Using the set tag (and the present function to avoid self referencing), we may chose which result to show, with keeping tracking of the result without setting it repeatedly:


{%#%} Result: {% #/print some_result %} {%/%} {% #script %} function setRadioChoice(choice) { {% #set result_choice %} choice {% /set %} } function update_number() { {% #/set number=present(number+1) %} setTimeout(update_number, 1000); } setTimeout(update_number, 1000); setTimeout( function() { {% #set greeting %}'Hello'{% /set %} }, 1500); const result_choice_sum = document.getElementById('result_choice_sum'); const result_choice_concat = document.getElementById('result_choice_concat'); if (result_choice_sum.checked) { setRadioChoice(result_choice_sum.value); } else if (result_choice_concat.checked) { setRadioChoice(result_choice_concat.value); } result_choice_sum.onchange = function () { setRadioChoice('number_sum'); }; result_choice_concat.onchange = function () { setRadioChoice('str_concat'); }; {% /script %}
{% #/def first_float=initial_first_float %} {% #/def second_float=initial_second_float %} {% #/def float_operation='add' %}
You may {%#%}{% #if float_operation==='minus' %}a decimals number{% :else %}two decimals numbers{% /if %}{%/%} below:
{%#%} - {%/%} {%#%} {% #/print float_operation==='minus'?'':(matchKeyVal(float_operation,float_binary_operation_symbols)) %} {%/%} {%#%} {%/%} = {%#%}{% #/print float_operation==='add'?(first_float + second_float):(float_operation==='substruct'?(first_float - second_float):(float_operation==='multiply'?(first_float * second_float):(float_operation==='divide'?(first_float / second_float):(float_operation==='minus'?(-first_float):'Error')))) %}{%/%}
{%#%} {%/%}
{% #/def test_json={v1: '2', 'v7': '3', nested: {A: 1, B: 2}, arr: [{a:1, b:2}, {c: 3}]} %} {% #element section style="padding-top: 15px;" %} You can also use JSON/dictionary values:

{% #/print test_json %}

{% /element %} {%#%} {%/%} {% #/def people=[{first: 'John', last: 'Smith', age: 40},{first: 'Kristen', last: 'Perkins', age: 27}] %} {% #element section %} You may even render a table using an array of dictionary: {% #for person in people %} {% #element tr %} {% /element %} {% /for %}
First Name Last Name Age
{% #/print person.first %} {% #/print person.last %} {% #/print person.age %}
{% /element %} {%#%} {%/%} {% #element section %} You can have a tag inside another tag: Test var array is: {% #/print test_array %} {% #element div style="color: red;" %} Test var value is: {% #/print test_var %} {% /element %} {% /element %} {% #/def nested_array=[[1,2,3],[11,12,13],[21,22,23]] %} {% #element div style="padding-top: 5px;" %} And also a for inside another for loop: {% /element %} {%#%} {%/%} {% #block %} {% #/def rand_color='rgb(0,0,255)' %} {% #/def changing_color=True %}
{% #script %} document.getElementById('toggle_color_button').onclick = function () { {% #set changing_color %} !{% #/get changing_color %} {% /set %} }; {% /script %}
{% # %}
{% #if changing_color %} This is text change its color randomally using reactive tag attribute. {% /if %} {% #if !changing_color %} This text stopped changing color! {% /if %}
{% / %} {%#%} {%/%} {% /block %} {% #element div style="padding-top: 5px;" %} You can also have a keyed for loop: (notice the HTML escaping and the internal string escaping in the source code) {% #for element in test_array by element %} {% #element p %} {% #/print element %} {% /element %} {% /for %} {% /element %} {% #/def interactive_people_array=[{first:'John',last:'Smith',age: 40}, {first:'Can\'t fool you',last:'',age: 1}] %} {% #/def disable_insertion=False %} {% #element div style="padding-top: 5px;" %} Or even a more advanced one: (make sure that first names are unique, or else you'll have an error) {%#%}
{%/%} {%#%}
New person: {% comment%}An example for self enclosed tags{% endcomment %} {% #/element input/ type = "text" id="first_name" %} {% #/element input type ="text" id= "last_name" / %} {%#%}{%/%} {%#%}{%/%}
{%/%} {%#%} {%/%} {%#%}
{% #if len(interactive_people_array)===0 %} People list is empty! {% :elif len(interactive_people_array)===1 %} Exactly one person in the list: {% :else %} People list: {% /if %}
{%/%} {% #if len(interactive_people_array)>0 %} {% #for person in interactive_people_array by person.first %} {% #element tr %} {% #element td %}{% #/print person.first %}{% /element %} {% #element td %}{% #/print person.last %}{% /element %} {% #element td %}{% #/print person.age %}{% /element %} {%#%} {%/%} {% /element %} {% /for %}
First Name Last Name Age Notes Actions
{%#%}{%/%} {%#%}{%/%} {%#%}{%/%} {%#%}{%/%}
{% /if %} {% /element %} {% /block %} {% /block %} {% endwith %} {% endblock %}