-
- 186 Posts
I looked an searched, but couldn’t find an answer to my question:
How can I get an array of all TV-content present on a page?
something like
"$contentarray = $modx->getTemplateVarOutput(*,$docID);"
As far as I can see I always have to know the name of the TV to ask for its content.
But if I do not know the names and just want the rendered output of all TVs for a page?
any clues?
Thanks!
-
- 788 Posts
Have a look at the (prefix)
site_tmplvar_contentvalues table in the database, I’m pretty sure this is where the information you seek is stored.
You could create a custom query to grab only the TV content for the doc which would be something like the code below:
Note this example only counts the number of TVs for the document which has this snippet, but you can modify as needed:
$output ="";
$sql = "SELECT * FROM ".$modx->dbConfig['dbase'].".".$modx->dbConfig['table_prefix']."site_tmplvar_contentvalues WHERE contentid='".$modx->documentObject['id']."'";
$rs = @$modx->dbQuery($sql);
$limit = @$modx->recordCount($rs);
if($limit > 0 ){
$output .="There are ".$limit." TVs for this document";
}else{
$output .="No TVs found";
}
return $output;
-
- 186 Posts
Thanks breezer! So, pulling it directly from the DB would be the way?
I thought that a cominbation of getTemplateVars to get an array of TVnames and then cycling it using getTemplateVarOutput could do this job. Seemed a bit too complicated.
Ok, then, at least I have an option now, thanks again!
Definitely NOT the recommended approach. Pulling things directly from the database is bypassing the API and asking for future upgrade issues. $modx->getTemplateVarOutput(’*’, $docId) will retrieve an array of the TV records from the db for you, returning the the data as an array of arrays, keyed with the tv names applicable to that document. Each TV array includes all the data necessary to process it. The only problem is, you then have to process that TV data as the parser would to render it properly, especially if using @ bindings or widgets. This is being addressed by the new object-oriented API, so that you can simply get the template vars for a doc and simply be able to render them with another API call the same way the parser would.
Until then, you’ll need to look at the document.parser.class.inc.php and duplicate the TV rendering logic.