Template Api

Last Updated: 2023-10-28 04:05:45
The template API allows you to import data in template files using Maple syntax.  It does not require any configuration or authentication and is readily available.  There is a five include limit per request meaning up to five Maple { api } statements can be used per page load.
    
The template API can be expanded similar to the HTTP API where it's dependent on plugins installed, additional installations will expand the API endpoints available.  It's also important to note that the template API endpoints can be shared between plugins.
    
The template API only supports inline variables and does not allow for parameters to be passed using the GET method.  Review the HTTP API documentation for details on inline variables.
The API syntax has two key points of interest which are the API endpoint and variable name. The API endpoint is a URL pointing to the API call you want to make.  This endpoint is defined without a protocol or host name and always starts with /api followed by an optional plugin path.  For example, /api/blog

The variable name is the name you choose to assign the dataset returned.  This name can then be used to access data and output containing values. Another key point to mention is a variable name can only contain alphanumeric values and underscores.  For example, my_variable_7
     
The full syntax can be defined as { api [ API ENDPOINT ] as [ VARIABLE ] }.  An example of its implementation would be { api /path/to/endpoint as myVariable }

Once you've imported the data needed for your template, it's important to know how to implement the returned dataset.  There are two commonly used maple constructs which are outputting a standard variable and/or the more commonly task of cycling through a list of records and outputting it's data.

Standard variables are defined as { [ VARIABLE ] }.  Let's assume that the following API returns only one variable { api /path/to/endpoint as myVariable }.  Accessing this variables value is written as { myVariable } so your full template may look something like this.
   
{ api /path/to/endpoint as myVariable }
<div>{ myVariable }</div>

The second construct are loops which allow you to cycle through record-sets and access specific values of a collection.  A collection is a variable containing multiple values usually related to one another.
    
For example, a variable representing an account may have first and last name variables.  These values can be accessed like so myAccount['FirstName'] or myAccount['LastName'].  You can use single, double or no quotes so myAccount['FirstName'] is the same as myAccount["FirstName"] and myAccount[FirstName].
    
Note: Maple is transitioning to dot notation for collections meaning the previous examples can be written as myAccount.FirstName and myAccount.LastName respectively.

Loop syntax can be defined as { [ VARIABLE ] in [ COLLECTION ] }[ CONTENTS ]{ loop }. An example of its implementation may look something like this.
    
{ api /path/to/endpoint as blogArticles }
{ article in blogArticles }
<a href="/path/to/page/{ article[Id] }">{ article[Title] }</a>
{ loop }
    
You can read more about Maple and it's syntax in the documentation. Below is a list of the template api
endpoints available.




Was this page helpful?