Quote from: dogas at Mar 03, 2006, 09:59 PM
Can you elaborate a bit more? I admit.. on fridays we have a few beers in the office and I just can’t make heads or tails out of what this means.
That’s aone of the problem, if you type it without thinking, basically, I didn’t get my message accross
Btw, if you have both document assigned to the same template, it means that you will have the same template variables, correct?
Now, lets call it doc1 and doc2. If you use GetDoc snippet, from doc1 to fetch content data from doc2, it will fetch the whole raw data from doc2 content area and it will be outputed by the GetDoc snippet on doc1.
Basically, in doc1, the raw data had been combined together with doc2, so now MODx parser will parsed the raw data and outputed the result to your screen, which means, every tv calling on doc1 and doc2 will be replaced with the value of the corresponding tv on doc1. So now it’s all depend on the needs. If you want the tv to be replaced by the tv value on doc1, then your snippet will not be needed. BUT..., if you want your doc2 tv tag to be replaced with the tv value on doc2, instead of doc1, then it’s time to use your snippet to achieve that.
Hope it does make sense with my limitation of ability to explain things in a clear manner and with the right english grammar and vocab
Heheheee...
Anyway, to solve your snippet as well, we can write it like this. It will avoid any missing id parameter when calling the snippet, to output nothing, instead of giving out error message. I also added the [*#tv_name*] fix and avoiding multiple calls to database.
// $id = document id to be fetched
$content = '';
if(isset($id) && is_int((int) $id)) {
$tblContent= $modx->db->config['table_prefix'] . 'site_content';
$tblTemplate= $modx->db->config['table_prefix'] . 'site_templates';
$query = "select template.content from $tblContent content, $tblTemplate template where content.id=$id and template.id = content.template";
if (!$rs= $modx->dbQuery($query)) {
return '';
}
$row= $modx->fetchRow($rs);
$content = $row['content'];
preg_match_all('~\[\*(.*?)\*\]~',$content,$matches);
$cnt = count($matches[1]);
$value = $modx->getTemplateVarOutput($matches[1], $id, "1");
for ($i=0; $i<$cnt; $i++) {
$v = $value[$matches[1][$i]];
$content = str_replace("[*".$matches[1][$i]."*]",$v,$content);
$content = str_replace("[*#".$matches[1][$i]."*]",$v,$content);
}
}
return $content;
Hope it works, I haven’t tested this out yet, but if it works, please update your first post with this code. Btw, if you can add more features to it, feel free to add it, and make it as your first snippet contribution to the community
You can make it to accept another parameter, which will determine which field on the document that will be fetched, instead of the default content field. Good job btw dogas!
Btw, have you tested the code yet? It seems to me that the fetchRow has to be $modx->db->fetchRow.
It also seems to me that the $content will not receiving any content data at all. Hemm... anyway, please take care of that and post your contribution when you’re done