MODX Cloud

The Most Productive MODX Learning Playground Ever

Claim Free Lab Account

Accessing Template Variable Values via the API

Skip to end of metadata
Go to start of metadata


Accessing Template Variable Values via the API

Like just about everything in the MODX GUI, you can access Template Variables and their values via the MODX API. This relies on the xPDO method getObject and related functions, but we demonstrate some examples here because it relates directly to Template Variables.

getTVValue

string|null getTVValue (str|integer $tv_name OR ID of TV)

See core/model/modx/modresource.class.php

getTVValue Usage

Let's say we have a TV named 'bio', and we're going to retrieve page id 123 that uses this TV. Here's what our Snippet might look like:

$page = $modx->getObject('modResource', 123);
return $page->getTVValue('bio');

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.

boolean setTVValue (str|integer $tv_name OR ID of TV, string $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

Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.