Hello
I need to render a chunk with php, using a template’s TVs. (This is going in an iframe.) A similar situation would be needed to supply a chunk through AJAX. What would be the best way to go about this. Presumably there are modx helper functions for chunk parsing..?
Thanks!
It’s not clear what you want. What’s in the TVs and how will you use them in the snippet?
You can send the content of the TVs to your chunk by using the TV tags as parameters in the snippet tag:
[!SnippetName? &tv1=`[*Tv1Name*]` &tv2=`[*Tv2Name*]` !]
If the TVs contain chunk names, you can get the content of the chunks like this:
$chunk1 = $modx->getChunk($tv1);
You can parse them yourself, or use $modx->parseChunk();
Then return the output.
parseChunk looks like what I need, awesome! What I want to do is use all the TVs from a particular page (document/resource) in parsing this chunk. Is there a good way of doing that?
Thanks
Quote from: pehrlich at Feb 22, 2010, 03:05 PM
parseChunk looks like what I need, awesome! What I want to do is use all the TVs from a particular page (document/resource) in parsing this chunk. Is there a good way of doing that?
Thanks
The easiest way is to just put tags in the chunk for the TVs.
[*tv1*] in the chunk will be replaced with the value of the current page’s tv1 Template Variable.
You can hide empty ones, if necessary, with PHX (or do other transformations on them).
but see, there is no current page. A custom .php script is parsing the chunk, and the args for replacement need to be supplied. (my bad for not explaining this clearly earlier!)
I could just write a query that would return an associative array of TVs for the desired page (which I know through GET), but if MODx has a built in ’correct’ way of doing this, I should use that.
Ah, then you want this, or one of the related functions listed on this page:
http://wiki.modxcms.com/index.php/API:getTemplateVar
Thanks for sharing that.
I’m sure people will find it useful.
FYI, if you were running the code in a snippet within MODx, you could replace "echo" with "return" at the end. It makes things a little cleaner.
Also, inside MODx, the $modx object will already be available and you are already connected to the DB so you wouldn’t have to include the class file and instantiate it or call connect().
right but how would you connect to a snippet with a HTTP request (ie, AJAX)?
Quote from: pehrlich at Feb 22, 2010, 08:27 PM
right but how would you connect to a snippet with a HTTP request (ie, AJAX)?
Put the snippet tag in the requested page? I’m probably misunderstanding you.