I manage snippets of code on my site with a MIGX TV where I simply paste in the code, give it a reference and also choose the syntax highlighting for it.
Then where I want the code snippet to popup I just place a snippet call:
[[source?reference=`reference I gave the code`]]
That snippet takes the MIGX data, finds the right code snippet and processes it to prevent tags/html from being executed. It then wraps it in the proper markup for syntaxhighlighting or quotes.
It works pretty good, I still need to blog about it..
Here's the code for the snippet if it's any help, my MIGX TV is "mh.sources":
$tv = $modx->resource->getTVValue('mh.sources');
$data = $modx->fromJSON($tv);
if (!is_array($data)) return;
foreach ($data as $row) {
if ($row['reference'] == $reference) {
$content = $row['content'];
if ($row['syntaxhighlighting']) {
$content = str_replace(array('[',']'),array('[',']'),htmlentities($content, ENT_QUOTES, "UTF-8"));
if ($row['syntaxhighlighting'] == 'quote') {
$content = '<blockquote>' . trim(nl2br($content)) . '</blockquote>';
} else {
$content = '<div style="width: 98%"><pre class="brush: '.$row['syntaxhighlighting'].'">'.$content.'</pre></div>';
}
}
return $content;
}
}
return 'Reference '.$reference.' not found.';
Ugh, the forum keeps eating up stuff.
Line 8 above should be
$content = str_replace(array('[',']'),array('& #91;','& #93;'),htmlentities($content, ENT_QUOTES, "UTF-8"));
except with no space between the &'s and #91/93.
[ed. note: markh last edited this post 12 years, 2 months ago.]