Tustena Liquid il markup facile per le stampe personalizzate
Liquid è un linquaggio per la definizione di markup facile per le configurazione di stampe personalizzate. Pur essendo di recente concezzione si può ben considerare uno standard indistriale percui abbiamo deciso di essere tra i primi ad implementarlo in Tustena CRM.
Ci sono due tipi di markup in Liquid: Output e Tag.
Per esempio le seguenti righe:
The word "tobi" in uppercase: {{ 'tobi' | upcase }}
The word "tobi" has {{ 'tobi' | size }} letters!
Change "Hello world" to "Hi world": {{ 'Hello world' | replace: 'Hello', 'Hi' }}
The date today is {{ now | date: 'yyyyMMdd' }}
daranno come risultato:
The word "tobi" in uppercase: TOBI
The word "tobi" has 4 letters!
Change "Hello world!" to "Hi world!": Hi world
The date today is (la data corrente)
I tag disponibili sono:
assign - assegna un valore ad una variabilecapture - cattura il testo contenuto nel blocco e lo assegna ad una variabilecase - è lo standard case ... whencycle - è utilizzato per ciclare dei valori all'interno di un loop (utile per alternare i colori ad ogni riga in una tabella)for - il classico ciclo forif - la codizione if/elseunless - la codizione inversa di if/elseinclude - include un altro templatecomment - non stampa il contenutoifchanged - è utilizzato all'interno di un loop per stampare solo se il dato è cambiatoliteral - ???block - ???tablerow - ???extends - ???
Capture
Capture inserisce il risultato in una variabile senza stamparlo.
{% capture heading %}
Tustena is the best!
{% endcapture %}
...
<h1>{{ heading }}</h1>Capture è utile per salvare il contenuto e utilizzarlo in seguito all'interno del template.
Assign
Assign assegna un valore ad una variabile.
{% assign foo = 'monkey' %}
{{ foo }}
Puoi utilizzare il valore in seguito nel template
Cycle
Cycle is usually used within a loop to alternate between values, like colors or DOM classes.
{% for item in items %}
<div class="{% cycle 'red', 'green', 'blue' %}"> {{ item }} </div>
{% end %}il risultato è:
Item one
Item two
Item three
Item four
Item five
Case
If you need more than one condition you can use the Case Statement
{% case template %}
{% when 'index' %}
Welcome
{% when 'product' %}
{{ product.vendor | link_to_vendor }} / {{ product.title }}
{% else %}
{{ page_title }}
{% endcase %}For
"For" iterates over an array or collection.
Several useful variables are available to you within the loop.
Basic usage:
{% for item in collection %}
{{ forloop.index }}: {{ item.name }}
{% endfor %}Advanced usage:
{% for item in collection %}
<div {% if forloop.first %}class="first"{% endif %}>
Item {{ forloop.index }}: {{ item.name }}
</div>
{% endfor %}You can also define a limit and offset much like SQL. Remember
that offset starts at 0 for the first item.
{% for item in collection limit:5 offset:10 %}
{{ item.name }}
{% end %}To reverse the for loop simply use
{% for item in collection reversed %}Available variables:
forloop.name 'item-collection'forloop.length Length of the loopforloop.index The current item's position in the collection; forloop.index starts at 1. This is helpful for non-programmers who start believe the first item in an array is 1, not 0.forloop.index0 The current item's position in the collection where the first item is 0forloop.rindex Number of items remaining in the loop (length - index) where 1 is the last item.forloop.rindex0 Number of items remaining in the loop where 0 is the last item.forloop.first Returns true if the item is the first item.forloop.last Returns true if the item is the last item.
Uso di limit:
# array = [1,2,3,4,5,6]
{% for item in array limit:2 offset:2 %}
{{ item }}
{% endfor %}
# results in 3,4
Unless
Unless is a conditional just like 'if' but works on the inverse logic.
{% unless x < 0 %} x is greater than zero {% end %}If
If e' un blocco condizionale
{% if user.admin %}
Admin user!
{% else %}
Not admin user
{% endif %}
There are {% if count < 5 %} less {% else %} more {% endif %} items than you need.IfChanged
The ifchanged block tag is used within a loop. It checks its own rendered contents against its previous state and only displays its content if the value has changed.
For example, in order to show the date only when a post is published on a new day (always displaying the time):
{% ifchanged %}
<div class="date">{{ article.published_at | date: '%B %d' }}</div>
{% endifchanged %}
<div class="time">{{ article.published_at | date: '%I:%M %p' }}</div>
Tables
{% tablerow item in items cols: 3 limit: 12 %}
{{ item.variable }}
{% endtablerow %}tablerowloop.length length of the entire for looptablerowloop.index index of the current iterationtablerowloop.index0 index of the current iteration (zero based)tablerowloop.rindex how many items are still left?tablerowloop.rindex0 how many items are still left? (zero based)tablerowloop.first is this the first iteration?tablerowloop.last is this the last iteration?tablerowloop.col index of column in the current rowtablerowloop.col0 index of column in the current row (zero based)tablerowloop.col_first is this the first column in the row?tablerowloop.col_last is this the last column in the row?
{% tablerow item in items cols: 3 %}
{% if col_first %}
First column: {{ item.variable }}
{% else %}
Different column: {{ item.variable }}
{% endif %}
{% endtablerow %}I filtri implementati sono:
date - formatta la data con la sintassi .Netcapitalize - Capitalizza tutte la paroledowncase - converte il testo in minuscoloupcase - converte il testo in maiuscolofirst - restituisce il primo valore di un arraylast - restituisce l'ultimo valore di un arrayjoin - unisce gli elementi di un array intramezzando un caratteresort - ordina gli elementi di un arraymap - ???size - restituisce la dimensione di un array di stringheescape - l'escape di una stringescape_once - esegue l'escape di una string html senza modificare gli elementi già convertiti.strip_html - rimuove i tag html da una stringastrip_newlines - rimuove gli 'a capo' (\n) da una stringanewline_to_br - sostituisce gli 'a capo' (\n) con un break html
replace - sostituisce ogni occorrenzareplace_first - sostituisce la prima occorrenzaremove - rimuove ogni occorrenzaremove_first - rimuove la prima occorrenzatruncate - tronca la stringa a n caratteritruncatewords - tronca la stringa a n paroleprepend - prepone alla stringa il valoreappend - accoda alla stringa il valoreminus - sottrae il valoreplus - somma il valoretimes - moltiplica il valoredivided_by - divide il valore
Estensioni custom di Tustena:
sum - somma il valore considerando le come numeri e restituendo un valore decimalemultiply - moltiplica il valore considerando le come numeri e restituendo un valore decimaledivide - divide il valore considerando le come numeri e restituendo un valore decimalesubtract - sottrae il valore considerando le come numeri e restituendo un valore decimaleformat_as_money -empty_cell -default -resolve_name -split -format_date -is_date -