Accessing TV Values
Last updated Oct 15th, 2026 | Page history | Improve this page | Report an issue
Support the team building MODX with a monthly donation.
The budget raised through OpenCollective is transparent, including payouts, and any contributor can apply to be paid for their work on MODX.
Backers
Budget
$194 per month—let's make that $500!
Learn moreRead and write TV values through the resource API; the examples below rely on the xPDO method getObject and related functions.
getTVValue¶
string|null getTVValue (str|integer $tv_name OR ID of TV)
See core/src/Revolution/modResource.php (methods getTVValue() and setTVValue())
getTVValue Usage¶
Read the TV bio from resource 123:
$page = $modx->getObject('modResource', 123);
return $page->getTVValue('bio');
getTVValue reads the resource cache when a cached value exists. Saving a resource normally clears that cache, but setTVValue below does not, so a value it just wrote may stay invisible to getTVValue. When you need the current data, bypass the cache and read the TV value record with getObject:
$tvr = $modx->getObject('modTemplateVarResource', array(
'tmplvarid' => $tvId,
'contentid' => $resourceId
));
if ($tvr) {
return $tvr->get('value');
}
else {
$tv = $modx->getObject('modTemplateVar', $tvId);
if ($tv) return $tv->get('default_text');
}
return '';
setTVValue¶
Use setTVValue to save a new value to a TV. Unlike some other xPDO API methods, this method stores values to the database immediately, so you do not need to invoke a separate call to a save() method. This method does not clear the resource cache.
boolean setTVValue (str|integer $tv_name OR ID of TV, string $value)
If the new value equals the TV’s default_text, setTVValue does not write a row — it removes the existing modTemplateVarResource record instead, so the TV falls back to its default. An immediate getTVValue after setTVValue may still return a cached value.
setTVValue Usage¶
$page = $modx->getObject('modResource', 123);
if (!$page->setTVValue('bio', 'This is my new bio...')) {
$modx->log(xPDO::LOG_LEVEL_ERROR, 'There was a problem saving your TV...');
}
See Also¶
- Creating a Template Variable
- Bindings
- Template Variable Input Types
- Template Variable Output Types
- Adding a Custom TV Type - MODX 2.2
- Creating a multi-select box for related pages in your template
Support the team building MODX with a monthly donation.
The budget raised through OpenCollective is transparent, including payouts, and any contributor can apply to be paid for their work on MODX.
Backers
Budget
$194 per month—let's make that $500!
Learn more










