Book Documentation Cap Tutorials

1202 script

In model blocks, you can use special scripting to manipulate data. This '1202 script' has a unique syntax and command set.

To make 1202 script easy to learn and understand, it resembles the PHP language, which is one of the most popular languages on the web.

Syntax

1202 script commands always start with a dollar sign followed directly by the name of the command, for instance the 'trim' command:

$trim

A command is always followed by one or more sets of square brackets or 'arguments':

$trim[ some text ]

In this case, the text between the square brackets is the value that should be trimmed. The result will be the removal of leading and trailing spaces.

Note: The number of arguments is different for each command. You have to define all arguments for the command to work.

In some cases, you'll want to define an empty value for an argument. For instance, the 'date' command is defined as:

$date[input][format]

The first argument is the input value, the second one specifies how the result should be formatted. If you do not supply an 'input', the command will use today's date (this is an example where 1202 script works like PHP):

$date[][%Y-%m-%d] // returns today's date formatted as yyyy-mm-dd

Note: Using quotes around argument values is not required, but in some cases can be necessary. If you have an argument value like '0000-00-00' (an empty date) 1202 script assumes it should calculate its value, as it consists of only numbers and mathematical operators. Use single quotes around the value to prevent this.

Tip: You can nest commands. For example: $if[$date[][%Y] == 2014][The year is 2014]

Variables

You can define variables by using the 'var' command. When a model block is iterated - which happens when multiple content items are 'fed' to it - you can use the 'storevar' command to save variable values for the next iteration.

Once defined, variables are called like this:

{var: test}

Special values

In model blocks, but also in filter groups, you may need a relative date or time. You can use the following 'special values' in model blocks and filter groups:

  • {curdate}: the current date, formatted as yyyy-mm-dd.
  • {curdateutc}: UTC equivalent of {curdate}.
  • {curdatetime}: the current date and time, formatted as yyyy-mm-dd hh:mm:ss.
  • {curdatetimeutc}: UTC equivalent of {curdatetime}.
  • {curday}: the current day of the month (two digits, with leading zeros).
  • {curdayutc}: UTC equivalent of {curday}.
  • {curhour}: the current hour in 24-hour format (with leading zeros).
  • {curhourutc}: UTC equivalent of {curhour}.
  • {curmin}: the current minute (with leading zeros).
  • {curminutc}: UTC equivalent of {curmin}.
  • {curmonth}: the current month (with leading zeros).
  • {curmonthutc}: UTC equivalent of {curmonth}.
  • {cursec}: the current second (with leading zeros).
  • {cursecutc}: UTC equivalent of {cursec}.
  • {curtime}: the current time, formatted as hh:mm:ss.
  • {curtimeutc}: UTC equivalent of {curtime}.
  • {curyear}: the current year, formatted as yyyy.
  • {curyearutc}: UTC equivalent of {curyear}.
  • {customdate}: date, formatted as yyyy-mm-dd, with optional input (defaults to today). The input is defined using a colon and can have relative values, for example: {customdate: next monday}. Note the use of a textual datetime description.
  • {customdateutc}: UTC equivalent of {customdate}.
  • {customdatetime}: same as {customdate}, except the output is formatted as yyyy-mm-dd hh:mm:ss (includes time).
  • {customdatetimeutc}: UTC equivalent of {customdatetime}.

Note: Beware of timezone differences. All dates and times in your 1202 system are saved according to UTC (Coordinated Universal Time). They are shown in your local time (according to the system timezone setting) except when 'UTC' is explicitly stated. When using special values, always consider wether you want local or UTC time.

It is also possible to do calculations with special values. For this, you can use textual datetime descriptions. If you want to get the date of tomorrow:

{curdate + 1 day}

More info on parsing textual datetime descriptions can be found in the PHP documentation.

Commands

This is the current collection of 1202 script commands. We are constantly adding and updating commands, so check back often.

$date

Return a date according to a specified format.

Usage:

$date[input][format]

Example:

$date[2014-09-01][%e %b] // returns '1 sep'

When 'input' is not specified, the current local time is used. If you want UTC, use the 'dateutc' command.

Tip: You can include a timezone with your input, using for instance '2014-09-01 UTC' instead of '2014-09-01'. The 'date' command will then convert it to local time.

More formatting options can be found in the PHP documentation (the 'date' command utilizes the 'strftime' function).

$dateutc

Equivalent to the 'date' command, except it uses UTC when no 'input' is specified.

$if

Conditionally execute a piece of code.

Usage:

$if[condition][code]

Example:

$if[1 == 1][This is true] // returns 'This is true'

You can use the following operators in your condition:

  • == equals
  • != does not equal
  • <> not greater and not smaller than (does not equal)
  • > greater than
  • < smaller than
  • >= greater than or equals
  • <= smaller than or equals

Furthermore, it is possible to combine conditions, using && for 'and', || for 'or', you can do calculations within conditions, and use parentheses to group conditions. For example:

$if[1 == 1 && (1 + 1 == 2 || 2 – 1 == 1)][This is true] // returns 'This is true'

$ifelse

Conditionally execute a piece of code. Also executes a piece of code if the condition evaluates to 'false'.

Usage:

$ifelse[condition][if: code][else: code]

Example:

$ifelse[1 == 2][This is true][This is not true] // returns 'This is not true'

The same options and operators are available as for the 'if' command.

$import

Import a model block into another model block.

Usage:

$import[model block id][query string]

Example:

$import[20][doc-id=1&system-id=2] // returns the processed model block with id '20'

Note: Importing only works with model blocks that are requested remotely. They will not work within model block fields (specified in containers).

$int

Return the integer value of a value.

Usage:

$int[input]

Example:

$int[12.902] // returns '12'

$iter

Iterate over a field. This is useful when a field can have multiple values, which applies to fields from linked items.

Usage:

$iter[field][code]

Example:

$iter[{3-120: Text: Image: id}][{3-120: Text: Image: id} ] // returns a list of image id's

The 'code' is executed for each iteration.

$len

Return the length (in characters) for a value.

Usage:

$len[input]

Example:

$len[one] // returns '3'

$lower

Return a value in lowercase.

Usage:

$lower[input]

Example:

$lower[Make This Lowercase] // returns 'make this lowercase'

$numformat

Return a value according as a number according to a specific format.

When not provided, the number of decimals is 0, the decimal separator ',', and the thousands separator '.'.

Usage:

$numformat[input][decimals][decimal separator][thousands separator]

Example:

$numformat[1202.1202][2][,][.] // returns '1.202,12'

$pos

Return the first character's position of a value ('needle') within another value ('haystack').

Usage:

$pos[input][search]

Example:

$pos[test][this is a test] // returns '10'

When the input value was not found, the command will return '0'.

Tip: Use the 'pos' command to find out if a value occurs within an input value by checking if the result is equal or not equal to '0'.

$rand

Return a random (numeric) value.

Usage:

$rand[min, max]

Example:

$rand[0, 10] // returns a number between 0 and 10

$replace

Replace a value within a value.

Usage:

$replace[search][replace][input]

Example:

$replace[this][that][I want this] // returns 'I want that'

$rem

Comment or 'remark' for documentation purposes. These are stripped when output and therefore will only be visible within your 1202 system.

Usage:

$rem[value]

Example:

$rem[this section of the script does some calculations] // returns ''

$round

Return a float rounded to the given number of decimals (defaults to '0').

Rounds up, away from zero, when it is half-way there, eg. 1.5 makes 2 and -1.5 makes -2.

Usage:

$round[input][decimals]

Example:

$round[12.02][1] // returns '12.0'

$storevar

Store a value to be used in a next iteration of the same model block. When content filters are linked to model blocks, it can 'feed' more than one item to a block. The block will then be iterated, with all values and variables cleared for each iteration. The 'storevar' command can save a variable between these iterations.

Usage:

$storevar[var]

Example:

$storevar[{var: test}] // has no return value, stores the variable named 'test'

Note: You first have to set a variable with the 'var' command to be able to store it.

$stripnum

Return a value with all digits (0-9) stripped.

Usage:

$stripnum[input]

Example:

$stripnum[input with 123 digits in it] // returns 'input with digits in it'

$striptags

Return a value with all tags stripped.

Usage:

$striptags[input]

Example:

$striptags[input with a <strong>tag</strong>] // returns 'input with a tag'

Info: This command strips HTML and PHP tags.

$substr

Return part of a value, specified by a start position and length.

Usage:

$substr[input][start, length]

Example:

$substr[one two three][4, 3] // returns 'two'

The 'start' and 'length' values can be negative, counting from the end of the string instead of the beginning. For examples, see the PHP substr documentation.

$trim

Strip whitespace from the beginning and end of a value and return it.

Usage:

$trim[input]

Example:

$trim[  too much space  ] // returns 'too much space'

$ucfirst

Return a value with the first character in uppercase.

Usage:

$ucfirst[input]

Example:

$ucfirst[first] // returns 'First'

$upper

Return a value in uppercase.

Usage:

$upper[input]

Example:

$upper[Make This Uppercase] // returns 'MAKE THIS UPPERCASE'

$urldecode

Return a decoded URL-encoded value. You can use this in conjunction with the 'urlencode' command.

Usage:

$urldecode[input]

Example:

$urldecode[a+simple+%26+short+test] // returns 'a simple & short test'

$urlencode

Return a URL-encoded value. You can use this in conjunction with the 'urldecode' command.

Usage:

$urlencode[input]

Example:

$urlencode[a simple & short test] // returns 'a+simple+%26+short+test'

$urlfriendly

Return a 'friendly' URL that is easy to read. It avoids 'clutter' from URL-encoding by replacing spaces for hyphens and simplifying accented letters and special characters.

Usage:

$urlfriendly[input]

Example:

$urlfriendly[al-ḥāsib al-miṣrī or the Egyptian calculator] // returns 'al-hasib-al-misri-or-the-egyptian-calculator'

$var

Set a variable to use it later on in the same model block.

Usage:

$var[name][value]

Example:

$var[test][some value] // sets the variable 'test' with the value 'some value'

You can access the set variable as a type of special field. The naming of these fields follows the pattern:

{var: name}

In the example above the variable is named 'test' and can thus be retrieved with:

{var: test}

Info: You can set the value of a variable with a string or number but also by using other variables or commands. This offers great opportunities for manipulating data.

Tip: If you want to save a variable to be used in later iterations of the same model block, use the 'storevar' command.