Framework  3.9
APITemplate Class Reference

APITemplate provides a simple template mechanism for transforming APIHelper output to meet 3rd-party standards. More...

+ Inheritance diagram for APITemplate:
+ Collaboration diagram for APITemplate:

Public Member Functions

 __construct ($file, $mimeType="text/xml")
 Creates a new APITemplate. More...
 
 format ($items)
 Override this function to provide your format definition. More...
 
- Public Member Functions inherited from APIFormatter
 getClass ()
 

Public Attributes

 $file
 
 $mimeType
 
- Public Attributes inherited from APIFormatter
 $parent
 

Detailed Description

APITemplate provides a simple template mechanism for transforming APIHelper output to meet 3rd-party standards.

The format is simple. {classname} starts the substitution block {/classname} ends the substitution block (where classname is the name of the DataItemClass. Within the substitution block all DataItem::format() behaviors are available, including method and helper function calls.

The MIME type of the output defaults to XML as this is the most common interchange format. However, you can easily override this when creating your template instance.

Author
andy

Definition at line 175 of file api_helper.inc.

Constructor & Destructor Documentation

◆ __construct()

APITemplate::__construct (   $file,
  $mimeType = "text/xml" 
)

Creates a new APITemplate.

Parameters
string$filethe full path to the file containing the template*
string$mimeTypethe MIME type to be returned in the HTTP headers (default MIME type is text/xml)

Definition at line 186 of file api_helper.inc.

187  {
188  $this->file = $file;
189  $this->mimeType = $mimeType;
190  }

Member Function Documentation

◆ format()

APITemplate::format (   $items)

Override this function to provide your format definition.

The results should be sent to the output buffer, via 'echo' or equivalent.

Parameters
array$itemsthe list of DataItems to be formatted

Reimplemented from APIFormatter.

Definition at line 192 of file api_helper.inc.

193  {
194  $class = $this->getClass();
195  $template = file_get_contents($this->file);
196 
197  $expr = "\{".$class."\}(.*?)\{\/".$class."\}";
198 
199  $matches = array();
200 
201  if (!preg_match("/$expr/si", $template, $matches))
202  {
203  throw new FakoliException("Badly formatted template $file - $expr<br><pre>$template<pre>");
204  }
205 
206  $format = $matches[1];
207 
208  $result = formatItems($items, $format);
209 
210  $out = str_replace($matches[0], $result, $template);
211 
212  header("Content-Type: $this->mimeType");
213  echo $out;
214  }
formatItems($items, $template, $separator="")
Format a list of DataItems using the specified templated.
Definition: data_item.inc:2176

Member Data Documentation

◆ $file

APITemplate::$file

Definition at line 177 of file api_helper.inc.

◆ $mimeType

APITemplate::$mimeType

Definition at line 178 of file api_helper.inc.


The documentation for this class was generated from the following file: