Benvenuto Ospite, sei in: Login
RSS RSS

Navigazione (Tech)





Ricerca wiki
»
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.

  • Output - stampa il testo inserito applicando eventuali filtri ed è incluso tra due graffe consecutive
    {{ testo da stampare }}
  • Tag - esegue una funzione e stampa il risultato. E' incluso in tra il simbolo graffa seguito dal percento e viceversa
    {% comando da eseguire %}


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 variabile
  • capture - cattura il testo contenuto nel blocco e lo assegna ad una variabile
  • case - è lo standard case ... when
  • cycle - è 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 for
  • if - la codizione if/else
  • unless - la codizione inversa di if/else
  • include - include un altro template
  • comment - non stampa il contenuto
  • ifchanged - è utilizzato all'interno di un loop per stampare solo se il dato è cambiato
  • literal - ???
  • 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 loop
  • forloop.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 0
  • forloop.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 &lt; 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 &lt; 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 loop
  • tablerowloop.index index of the current iteration
  • tablerowloop.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 row
  • tablerowloop.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 .Net
  • capitalize - Capitalizza tutte la parole
  • downcase - converte il testo in minuscolo
  • upcase - converte il testo in maiuscolo
  • first - restituisce il primo valore di un array
  • last - restituisce l'ultimo valore di un array
  • join - unisce gli elementi di un array intramezzando un carattere
  • sort - ordina gli elementi di un array
  • map - ???
  • size - restituisce la dimensione di un array di stringhe
  • escape - l'escape di una string
  • escape_once - esegue l'escape di una string html senza modificare gli elementi già convertiti.
  • strip_html - rimuove i tag html da una stringa
  • strip_newlines - rimuove gli 'a capo' (\n) da una stringa
  • newline_to_br - sostituisce gli 'a capo' (\n) con un break html
  • replace - sostituisce ogni occorrenza
  • replace_first - sostituisce la prima occorrenza
  • remove - rimuove ogni occorrenza
  • remove_first - rimuove la prima occorrenza
  • truncate - tronca la stringa a n caratteri
  • truncatewords - tronca la stringa a n parole
  • prepend - prepone alla stringa il valore
  • append - accoda alla stringa il valore
  • minus - sottrae il valore
  • plus - somma il valore
  • times - moltiplica il valore
  • divided_by - divide il valore

Estensioni custom di Tustena:
  • sum - somma il valore considerando le come numeri e restituendo un valore decimale
  • multiply - moltiplica il valore considerando le come numeri e restituendo un valore decimale
  • divide - divide il valore considerando le come numeri e restituendo un valore decimale
  • subtract - sottrae il valore considerando le come numeri e restituendo un valore decimale
  • format_as_money -
  • empty_cell -
  • default -
  • resolve_name -
  • split -
  • format_date -
  • is_date -