Difference between revisions of "EPScript/Functions"

From EPrints Documentation
Jump to: navigation, search
(Switched back to basic navigatiom template)
(Added some more functions)
Line 4: Line 4:
  
 
== Calling Functions ==
 
== Calling Functions ==
 
 
Functions can be called in two ways:
 
Functions can be called in two ways:
 
+
{{codesample|<pre>
{{codesample|
+
<when test="is_set( creators )">
<pre>
+
</pre>}}
<when test="is_set( creators )">
 
</pre>
 
}}
 
 
 
 
or
 
or
 +
{{codesample|<pre>
 +
<when test="creators.is_set">
 +
</pre>}}
 +
These are interchangable, but it may be beneficial to use a specific form in some cases.
  
{{codesample|
 
<pre>
 
<when test="creators.is_set">
 
</pre>
 
}}
 
 
These are interchangable, but it may be beneficial to use a specific form in some cases.
 
  
 
== Generic Functions ==
 
== Generic Functions ==
  
 
=== is_set ===
 
=== is_set ===
 
 
Returns true if the parameter is set, based on the following criteria:
 
Returns true if the parameter is set, based on the following criteria:
 
* If the parameter is a string, it is set if it is not empty.
 
* If the parameter is a string, it is set if it is not empty.
 
* If the parameter is a list or a complex structure, it is set if at least one value is set.
 
* If the parameter is a list or a complex structure, it is set if at least one value is set.
 +
{{codesample|<pre>
 +
<when test="is_set( creators )">
 +
  ...
 +
</when>
 +
</pre>}}
 +
 +
=== yesno ===
 +
Returns a <tt>yes</tt> or <tt>no</tt> string depending on whether the expression is true.
 +
{{codesample|<pre>
 +
<print expr="yesno(is_set('abstract'))" />
 +
</pre>}}
  
{{codesample|
 
<pre>
 
<when test="is_set( creators )">
 
...
 
</when>
 
</pre>
 
}}
 
  
 
== List Functions ==
 
== List Functions ==
  
 
=== length ===
 
=== length ===
 +
Returns the number of items in the list.
 +
{{codesample|<pre>
 +
<if test="length(editors) gt 1">s</if>
 +
</pre>}}
  
Returns the number of items in the list.
+
=== one_of ===
 +
Returns a boolean depenbding on whether a variable is one of a number of values.
 +
{{codesample|<pre>
 +
<when test="type.one_of('article','conference_item','book')">
 +
  ...
 +
</when>
 +
</pre>}}
  
{{codesample|
 
<pre>
 
<if test="length(editors) gt 1">s</if>
 
</pre>
 
}}
 
  
 
== String Functions ==
 
== String Functions ==
  
=== one_of ===
+
=== reverse ===
 +
Returns the reverse of a string (i.e. 'abc' becomes 'cba').
 +
{{codesample|<pre>
 +
<when test="type.reverse = 'tnetap'">
 +
  ...
 +
</when>
 +
</pre>}}
 +
 
 +
=== join ===
 +
Returns the concatentation of strings with a particular joining character.
 +
{{codesample|<pre>
 +
<when test="$list.join(':') = 'a:b:c'">
 +
  ...
 +
</when>
 +
</pre>}}
 +
 
 +
=== substr ===
 +
Returns the substring of a string
 +
{{codesample|<pre>
 +
<when test="title.substr(0,3) = 'the'">
 +
  ...
 +
</when>
 +
</pre>}}
 +
 
 +
=== contains ===
 +
Returns a boolean depending whether the string contains a particular substring.
 +
{{codesample|<pre>
 +
<when test="title.contains('the')">
 +
  ...
 +
</when>
 +
</pre>}}
 +
 
 +
=== as_string ===
 +
Converts variable into a string.  Can be useful in phrases or for defining class or id attributes for HTML elements
 +
{{codesample|<pre>
 +
<phrase ref="licenses_description_{license.as_string()}" />.
 +
</pre>}}
 +
 
 +
=== strlen ===
 +
Gets the integer length of a string.
 +
{{codesample|<pre>
 +
<when test="strlen(title) > 50">
 +
  ...
 +
</when>
 +
</pre>}}
 +
 
 +
== Mathematical Functions ==
 +
 
 +
=== add ===
 +
Returns the addition of one number to another.
 +
{{codesample|<pre>
 +
<when test="pages + 4 > 10">
 +
  ...
 +
</when>
 +
</pre>}}
 +
 
 +
=== subtract ===
 +
Returns the substraction of one number from another.
 +
{{codesample|<pre>
 +
<when test="pages - 4 lt 10">
 +
  ...
 +
</when>
 +
</pre>}}
 +
 
 +
=== multiply ===
 +
Returns the multiplication of one number by another.
 +
{{codesample|<pre>
 +
<when test="pages * 4 gt 10">
 +
  ...
 +
</when>
 +
</pre>}}
 +
 
 +
=== divide ===
 +
Returns the division of one number by another.
 +
{{codesample|<pre>
 +
<when test="pages / 4 lt 10">
 +
  ...
 +
</when>
 +
</pre>}}
 +
 
 +
=== mod ===
 +
Returns the modulus (i.e. remainder) of a number from a base (e.g <tt>5 % 3</tt> equals 2.
 +
{{codesample|<pre>
 +
<when test="pages % 2 = 0">
 +
  ...
 +
</when>
 +
</pre>}}
 +
 
 +
=== uminus ===
 +
Returns the negative value of an integer (e.g. 1 becomes -1).  This is a unary rather than binary operator like substract.
 +
{{codesample|<pre>
 +
<when test="-rating gt 5">
 +
  ...
 +
</when>
 +
</pre>}}
 +
 
 +
 
 +
== Rendering Functions ==
 +
 
 +
=== citation ===
 +
Renders the XHTML citation for a data object with a specified style.  Encapsulated within a link to
 +
{{codesample|<pre>
 +
<print expr="$item.citation('default')" />
 +
</pre>}}
 +
 
 +
=== citation_link ===
 +
Renders the XHTML citation for a data object with a specified style, where the title is encompassed by a link to the data object.
 +
{{codesample|<pre>
 +
<print expr="$item.citation_link('default')" />
 +
</pre>}}
 +
 
 +
=== embed_video ===
 +
Renders an XHTML object to render an HTML5 video preview for a document that is a video or audio file.
 +
{{codesample|<pre>
 +
<print expr="$doc.embed_video" />
 +
</pre>}}
 +
 
 +
 
 +
== Data Object Functions ==
 +
 
 +
=== property ===
 +
Get the property of a data object
 +
{{codesample|<pre>
 +
<print expr="$item.property('note')">
 +
</pre>}}
 +
 
 +
=== main_item_property ===
 +
Get the property of parent data object.
 +
{{codesample|<pre>
 +
<print expr="$doc.main_item_property('note')" />
 +
</pre>}}
 +
 
 +
=== as_item ===
 +
Treats a variable as an item so other data object functions can be applied to it.
 +
{{codesample|<pre>
 +
  <when test="$doc.property('eprintid').as_item().property('type') = 'thesis'">
 +
</pre>}}
 +
 
 +
=== dataset ===
 +
Returns the string for the dataset of the data object.
 +
{{codesample|<pre>
 +
  <when test="$item.dataset() = 'document'">
 +
</pre>}}
 +
 
 +
=== related_objects ===
 +
Returns an array of objects related to the current data object through a particular property.
 +
{{codesample|<pre>
 +
<foreach expr="$doc.related_objects('http://eprints.org/relation/hasVersion')" iterator="rel">
 +
</pre>}}
  
Returns true if the string is in the list of strings provided.
+
=== url ===
 +
Returns the URL for a particular data object.
 +
{{codesample|<pre>
 +
<a href="{$doc.url()}">Download</a>
 +
</pre>}}
  
{{codesample|
 
<pre>
 
<when test="type.one_of( 'book','book_section' )">
 
...
 
</when>
 
</pre>
 
}}
 
  
=== reverse ===
+
== Date Functions ==
  
Returns the reverse of a string (i.e. 'abc' becomes 'cba').
+
=== today ===
 +
Returns today's date
 +
{{codesample|<pre>
 +
<print expr="today()" />
 +
</pre>}}
  
{{codesample|
+
=== datemath ===
<pre>
+
Returns a date relative to the date provided, (e.g. two years ago).
<when test="type.reverse = 'tnetap'">
+
{{codesample|<pre>
...
+
<when test="date lt today().datemath( -2,'year' )">
</when>
+
</pre>}}
</pre>
 
}}
 

Revision as of 18:09, 20 February 2022

EPScript

Functions

Calling Functions

Functions can be called in two ways:

 <when test="is_set( creators )">

or

 <when test="creators.is_set">

These are interchangable, but it may be beneficial to use a specific form in some cases.


Generic Functions

is_set

Returns true if the parameter is set, based on the following criteria:

  • If the parameter is a string, it is set if it is not empty.
  • If the parameter is a list or a complex structure, it is set if at least one value is set.
 <when test="is_set( creators )">
   ...
 </when>

yesno

Returns a yes or no string depending on whether the expression is true.

 <print expr="yesno(is_set('abstract'))" />


List Functions

length

Returns the number of items in the list.

 <if test="length(editors) gt 1">s</if>

one_of

Returns a boolean depenbding on whether a variable is one of a number of values.

 <when test="type.one_of('article','conference_item','book')">
   ...
 </when>


String Functions

reverse

Returns the reverse of a string (i.e. 'abc' becomes 'cba').

 <when test="type.reverse = 'tnetap'">
   ...
 </when>

join

Returns the concatentation of strings with a particular joining character.

 <when test="$list.join(':') = 'a:b:c'">
   ...
 </when>

substr

Returns the substring of a string

 <when test="title.substr(0,3) = 'the'">
   ...
 </when>

contains

Returns a boolean depending whether the string contains a particular substring.

 <when test="title.contains('the')">
   ...
 </when>

as_string

Converts variable into a string. Can be useful in phrases or for defining class or id attributes for HTML elements

 <phrase ref="licenses_description_{license.as_string()}" />.

strlen

Gets the integer length of a string.

 <when test="strlen(title) > 50">
   ...
 </when>

Mathematical Functions

add

Returns the addition of one number to another.

 <when test="pages + 4 > 10">
   ...
 </when>

subtract

Returns the substraction of one number from another.

 <when test="pages - 4 lt 10">
   ...
 </when>

multiply

Returns the multiplication of one number by another.

 <when test="pages * 4 gt 10">
   ...
 </when>

divide

Returns the division of one number by another.

 <when test="pages / 4 lt 10">
   ...
 </when>

mod

Returns the modulus (i.e. remainder) of a number from a base (e.g 5 % 3 equals 2.

 <when test="pages % 2 = 0">
   ...
 </when>

uminus

Returns the negative value of an integer (e.g. 1 becomes -1). This is a unary rather than binary operator like substract.

 <when test="-rating gt 5">
   ...
 </when>


Rendering Functions

citation

Renders the XHTML citation for a data object with a specified style. Encapsulated within a link to

 <print expr="$item.citation('default')" />

citation_link

Renders the XHTML citation for a data object with a specified style, where the title is encompassed by a link to the data object.

 <print expr="$item.citation_link('default')" />

embed_video

Renders an XHTML object to render an HTML5 video preview for a document that is a video or audio file.

 <print expr="$doc.embed_video" />


Data Object Functions

property

Get the property of a data object

 <print expr="$item.property('note')">

main_item_property

Get the property of parent data object.

 <print expr="$doc.main_item_property('note')" />

as_item

Treats a variable as an item so other data object functions can be applied to it.

  <when test="$doc.property('eprintid').as_item().property('type') = 'thesis'">

dataset

Returns the string for the dataset of the data object.

  <when test="$item.dataset() = 'document'">

related_objects

Returns an array of objects related to the current data object through a particular property.

 <foreach expr="$doc.related_objects('http://eprints.org/relation/hasVersion')" iterator="rel">

url

Returns the URL for a particular data object.

 <a href="{$doc.url()}">Download</a>


Date Functions

today

Returns today's date

 <print expr="today()" />

datemath

Returns a date relative to the date provided, (e.g. two years ago).

 <when test="date lt today().datemath( -2,'year' )">