Hi,
I have a document that has 15 TVs and I would like to get only 10 of them.
Until know I have 2 solutions:
- One that gets all of them
- One that gets each one individually
One that gets all of them
<?php
$id = $modx->resource->get('id');
$collection = $modx->getCollection('modResource', array('id' =>$id));
foreach ($collection as $resourceId => $resource) {
$tvs = array();
$templateVars = &$resource->getMany('TemplateVars');
foreach ($templateVars as $tvId => $templateVar) {
$tvs[$tvPrefix . $templateVar->get('name')] = !empty($processTVs) ? $templateVar->renderOutput($resource->get('id')) : $templateVar->get('value');
}
}
print_r($tvs);
?>
This is a copy/paste from getResources. It works but note that even if I have one document I have no idea how to do it without a loop. I tried getOne but I think it’s for something else.
Can anyone point to the some resources to learn how to do it myself?
One that gets each one individually
<?php
$id = $modx->resource->get('id');
$tv1 = $modx->getObject('modTemplateVar', array('name' =>'TV1'));
$tv2 = $modx->getObject('modTemplateVar', array('name' =>'TV2'));
....and so on
echo $tv1->getValue($id);
echo $tv1->getValue($id);
echo $tv1->getValue($id);
....and so on
?>
This solution takes each TV individually.
At this point I’m not sure which solution is the best. I guess I’m looking for an API that gets only what I need and some pointers on what solution is faster/recommended.
Thank you