Thank you for your replies. Following Bruno17's advice I have stripped out all the conditional modifiers from the chunk and replaced them with logic and lexicon strings. This has resulted in speed improvement. But there is still huge difference between using getChunk and straight html in logic.
getChunk takes 2.5 to 3 seconds whereas the html and variables takes 890 milliseconds.
@Bruno17 - I'm not sure what I should be caching. The snippet I am using is called via ajax and returns JSON to a javascript function to plot the markers on a map. Should I be caching the info window HTML? Any clues would be appreciated.
@goldsky - I have a custom class using the following getChunk method. I have also included the processor code below this (it includes both ways of creating the html for the info window, one is commented out).
@BobRay - thanks. I have moved the logic to the code below.
public function getChunk($name, $properties = array()) {
$chunk = null;
if (!isset($this->chunks[$name])) {
$chunk = $this->_getTplChunk($name);
if (empty($chunk)) {
$chunk = $this->modx->getObject('modChunk',array('name' => $name));
if ($chunk == false) return false;
}
$this->chunks[$name] = $chunk->getContent();
} else {
$o = $this->chunks[$name];
$chunk = $this->modx->newObject('modChunk');
$chunk->setContent($o);
}
$chunk->setCacheable(false);
return $chunk->process($properties);
}
private function _getTplChunk($name, $postfix = '.chunk.tpl') {
$chunk = false;
$f = $this->config['chunks_path'].strtolower($name).$postfix;
if (file_exists($f)) {
$o = file_get_contents($f);
$chunk = $this->modx->newObject('modChunk');
$chunk->set('name',$name);
$chunk->setContent($o);
}
return $chunk;
}
<?php
require_once $modx->getOption('propertymanager.core_path',null,$modx->getOption('core_path').'components/propertymanager/').'model/propertymanager/propertymanager.class.php';
require_once $modx->getOption('googlemap.core_path',null,$modx->getOption('core_path').'components/googlemap/').'model/googlemap/googlemap.class.php';
$transaction_id = NULL;
$parent_id = NULL;
if (isset($_GET['transaction_id'])) {
$transaction_id = filter_input(INPUT_GET, 'transaction_id', FILTER_SANITIZE_STRING);
$parent_id = filter_input(INPUT_GET, 'parent_id', FILTER_SANITIZE_STRING);
}
$property_manager = new PropertyManager($modx, $transaction_id);
$criteria = $property_manager->getCriteria();
$properties = $modx->getIterator('pmProperty', $criteria);
$map = new GoogleMap($modx);
$map->map_options->setScrollwheel(FALSE);
$map->map_options->setCenter(53.9609795,-2.0119441);
if ($area = $property_manager->getArea()) {
$map->map_options->setCenter($area['latitude'], $area['longitude']);
$map->map_options->setZoom(10);
}
$map->setLexicon($modx->lexicon->fetch('pm.',false));
foreach ($properties as $property) {
$content = NULL;
$placeholders = $property->toArray();
$placeholders['sub_type'] = strtolower($property->getOne('SubType')->get('name'));
// set price text
$placeholders['price_qualifier'] = ($placeholders['price_qualifier_id'] > 0) ? $property->getOne('PriceQualifier')->get('name') : NULL;
$placeholders['price'] = number_format($placeholders['price'], 0, '.', ',');
$placeholders['rent_frequency'] = ($transaction_id == 2) ? $modx->lexicon('pm.pcm') : NULL;
if ($placeholders['price_qualifier_id'] == 1) {
$placeholders['price_text'] = $placeholders['price_qualifier'];
} else {
$placeholders['price_text'] = sprintf($modx->lexicon('pm.price_text'), $placeholders['price_qualifier'], $placeholders['price'], $placeholders['rent_frequency']);
}
// set status text
if ($placeholders['status_id'] > 0) {
$placeholders['status'] = sprintf($modx->lexicon('pm.status_text'), $property->getOne('Status')->get('name'));
}
// set bedroom text
if ($placeholders['bedrooms'] > 0) {
$lexicon_string = ($transaction_id == 1) ? $modx->lexicon('pm.description_text_sales'): $modx->lexicon('pm.description_text_lettings');
$placeholders['description_text'] = sprintf($lexicon_string, $placeholders['bedrooms'], $placeholders['sub_type']);
} else {
$lexicon_string = ($transaction_id == 1) ? $modx->lexicon('pm.description_text_sales_no_beds'): $modx->lexicon('pm.description_text_lettings_no_beds');
$placeholders['description_text'] = sprintf($lexicon_string, ucfirst($placeholders['sub_type']));
}
$placeholders['property_url'] = $modx->makeUrl($parent_id) . 'property-' . $placeholders['id'] . '.html';
if ($image_array = $property_manager->getMainImage($placeholders['id'])) {
$placeholders['image_url'] = $image_array['url'];
$placeholders['images_title'] = $placeholders['display_address'];
} else {
$placeholders['image_url'] = $property_manager->config['assets_url'] . 'images/awaiting-image.jpg';
$placeholders['images_title'] = NULL;
}
$content = $property_manager->getChunk('infobox', $placeholders);
/*
if ($image_array = $property_manager->getMainImage($placeholders['id'])) {
$placeholders['image_url'] = $property_manager->config['phpthumb'] . '?src=' . $property_manager->config['base_url'] . $image_array['url'] . '&f=jpg&w=116&h=87&zc=1';
$placeholders['images_title'] = $placeholders['display_address'];
} else {
$placeholders['image_url'] = $property_manager->config['phpthumb'] . '?src=' . $property_manager->config['assets_url'] . 'images/awaiting-image.jpg' . '&f=jpg&w=116&h=87&zc=1';
$placeholders['images_title'] = NULL;
}
$content = '<div class="infobox-property-container">
<div class="infobox-property clear-fix">
<div class="infobox-image"><img src="' . $placeholders['image_url'] . '" width="116" height="87">
<p class="infobox-more-details"><a href="' . $placeholders['property_url'] . '">More Details</a></p>
</div>
<div class="infobox-text">
<p class="infobox-address">' . $placeholders['display_address'] . '</p>
<p class="infobox-price">' . $placeholders['price_text'] . '</p>' .
$placeholders['status'] .
'<p class="infobox-bedrooms">' . $placeholders['description_text'] . '</p>
</div>
</div>
</div>';
*/
$marker = new GoogleMapMarkerOptions();
$marker->setPosition($placeholders['latitude'],$placeholders['longitude']);
$marker->setTitle($placeholders['display_address']);
$marker->setContent($content);
$marker->setIcon('icon');
$marker->setShadow('shadow');
$map->setMarker($marker);
}
$image = new GoogleMapMarkerImage();
$image->setUrl($property_manager->config['assets_url'] . 'icons/marker.png');
$image->setSize(21, 24);
$image->setOrigin(0, 0);
$image->setAnchor(10, 24);
$image_2 = new GoogleMapMarkerImage();
$image_2->setUrl($property_manager->config['assets_url'] . 'icons/marker.png');
$image_2->setSize(29, 24);
$image_2->setOrigin(23, 0);
$image_2->setAnchor(10, 24);
$map->setMarkerImage('icon', $image);
$map->setMarkerImage('shadow', $image_2);
$clusterer_options = new GoogleMapMarkerClustererOptions($modx);
$clusterer_options->setGridSize(10);
$clusterer_options->setMaxZoom(14);
$clusterer_options->setAverageCenter(TRUE);
$clusterer_options->setZoomOnClick(FALSE);
$clusterer_options->setMinimumClusterSize(2);
$clusterer_options->setTitle('Properties');
$style = new GoogleMapMarkerClustererIconStyle($modx);
$style->setUrl($property_manager->config['assets_url'] . 'icons/clustermarker.png');
$style->setWidth(21);
$style->setHeight(24);
$style->setAnchor(0, 0);
$style->setAnchorIcon(24, 10);
$style->setBackgroundPosition('0 0');
$style->setTextSize(1);
$map->setClustererOptions($clusterer_options);
$map->setClustererIconStyle($style);
$info_box = new GoogleMapInfoBox();
$info_box->setAlignBottom(TRUE);
$info_box->setBoxClass('infobox');
$info_box->setCloseBoxMargin('5px 5px 5px 0');
//$info_box->setInfoBoxClearance(3,3);
//$info_box->setPixelOffset(10,10);
$map->setInfoBox($info_box);
return json_encode($map);