Core/getElementsByTemplate

From PunchCMS
Jump to: navigation, search

PCMS_Client->getElementsByTemplate(templateApiName, [getFirst], [recursive], [random])

Overview

Get a collection of elements with a specific template api name from the root folder.

Arguments

templateApiName [string]
Template API name of the elements you are looking for.

getFirst [boolean, optional]
Indicate if you want to get only the first found Element or a collection of Elements. Default is FALSE.
This is deprecated. You should use the getElementByTemplate() method instead.

recursive [boolean, optional]
Indicate if you want to search trough the tree or only on the first level. Setting this to TRUE can be very time consuming. Default is FALSE.

random [boolean, optional]
Indicate if you want to randomize the returned collection. Default is FALSE.

Returns

If Elements are found matching the criteria this method returns a valid Elements object. Otherwise an empty collection. That way you can test if you get a valid return using the count() method.

If the getFirst argument was set to TRUE and an Element was found it will return a valid Element object. Otherwise NULL.

Example

$objCms = PCMS_Client::getInstance();
 
// Get a collection of Elements with template API name 'Item'.
$objElements = $objCms->getElementsByTemplate("Item");
foreach ($objElements as $objElement) {
    // Loop through the collection.
}