Разработка кастомного сниппета (Snippet) MODX
Сниппет MODX — PHP-код, возвращающий строку для вывода в шаблоне. Это основной инструмент динамической функциональности: списки товаров, фильтры, интеграции с API, вычисления. Сниппет получает параметры из шаблона и возвращает HTML.
Структура сниппета
<?php
// Сниппет ProductList
// Вызов: [[!ProductList? &category=`5` &limit=`12` &sort=`price`]]
// Получить параметры (с default значениями)
$categoryId = (int)($scriptProperties['category'] ?? 0);
$limit = (int)($scriptProperties['limit'] ?? 10);
$offset = (int)($scriptProperties['offset'] ?? 0);
$sortField = $scriptProperties['sort'] ?? 'menuindex';
$sortDir = $scriptProperties['sortdir'] ?? 'ASC';
$tpl = $scriptProperties['tpl'] ?? 'productCard';
// Запрос к ресурсам через MODX API
$c = $modx->newQuery('modResource');
$c->where([
'parent' => $categoryId,
'published' => 1,
'deleted' => 0,
'class_key' => 'modDocument',
]);
// Получить TV значения через Join
$c->innerJoin('modTemplateVarResource', 'TVPrice', [
'TVPrice.tmplvarid' => $modx->getObject('modTemplateVar', ['name' => 'price'])->id,
'TVPrice.contentid = modResource.id',
]);
$c->select('modResource.*, TVPrice.value AS price');
$c->sortby($sortField, $sortDir);
$c->limit($limit, $offset);
$resources = $modx->getCollection('modResource', $c);
if (empty($resources)) return '';
$output = '';
foreach ($resources as $resource) {
$data = array_merge($resource->toArray(), [
'price' => $resource->get('price'),
'link' => $modx->makeUrl($resource->id, '', '', 'full'),
'image' => $resource->getTVValue('product_image'),
]);
// Чанк для вывода карточки
$output .= $modx->getChunk($tpl, $data);
}
return $output;
Сниппет с pdoTools (рекомендуется)
pdoTools предоставляет pdoFetch — быстрый и правильный способ делать запросы:
<?php
// Сниппет ProductSearch с pdoTools
if (!$modx->loadClass('pdoFetch', MODX_CORE_PATH . 'components/pdotools/model/pdotools/', false, true)) {
return 'pdoTools не установлен';
}
$pdoFetch = new pdoFetch($modx, $scriptProperties);
$pdoFetch->addWhere([
'modResource.parent' => (int)($scriptProperties['category'] ?? 0),
'modResource.published' => 1,
]);
// TV join
$pdoFetch->addTVs('price,product_image,short_description');
$result = $pdoFetch->run();
return $result;
Сниппет с кэшированием
<?php
// Кэшировать результат на 30 минут
$cacheKey = 'products_' . md5(json_encode($scriptProperties));
$cacheOptions = [xPDO::OPT_CACHE_KEY => 'default', xPDO::OPT_CACHE_EXPIRES => 1800];
$cached = $modx->cacheManager->get($cacheKey, $cacheOptions);
if ($cached !== null) return $cached;
// ... запрос ...
$output = generateOutput($resources);
$modx->cacheManager->set($cacheKey, $output, 1800, $cacheOptions);
return $output;
Сниппет для интеграции с внешним API
<?php
// Сниппет WeatherWidget — погода из OpenWeatherMap
$city = $scriptProperties['city'] ?? 'Moscow';
$apiKey = $modx->getOption('weather_api_key');
$tpl = $scriptProperties['tpl'] ?? 'weatherWidget';
$cacheKey = 'weather_' . $city;
$cached = $modx->cacheManager->get($cacheKey, [xPDO::OPT_CACHE_EXPIRES => 1800]);
if ($cached !== null) {
return $modx->getChunk($tpl, $cached);
}
$url = "https://api.openweathermap.org/data/2.5/weather?q={$city}&appid={$apiKey}&units=metric&lang=ru";
$response = file_get_contents($url);
if (!$response) return '';
$data = json_decode($response, true);
if (!$data || $data['cod'] !== 200) return '';
$weather = [
'city' => $data['name'],
'temp' => round($data['main']['temp']),
'feels_like' => round($data['main']['feels_like']),
'description' => $data['weather'][0]['description'],
'icon' => "https://openweathermap.org/img/wn/{$data['weather'][0]['icon']}@2x.png",
'humidity' => $data['main']['humidity'],
];
$modx->cacheManager->set($cacheKey, $weather, 1800);
return $modx->getChunk($tpl, $weather);
Передача параметров в сниппет
[[!ProductList?
&category=`[[*id]]`
&limit=`12`
&tpl=`productCardTpl`
&sort=`price`
&sortdir=`ASC`
]]
! перед именем — некэшированный вызов (динамический контент).
Без ! — кэшированный (статический блок, одинаковый для всех).
Сроки
Разработка одного сниппета с запросом к ресурсам, кэшированием и чанком вывода — 0.5–2 дня. Сложный сниппет с фильтрами, пагинацией и интеграцией с API — 3–5 дней.







