Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
Displaying the content of a file in an article. Is it possible?
Hi all,
I’ve been putting a shiny new website together that’s going to feature quite a few pieces of code in the articles. Initially I was just going to do this by using bc..
to wrap it all up in <pre>
and <code>
tags, but then I hit upon the idea of simply uploading the file in the TXP interface and grabbing it from the article somehow, and using a custom form to wrap it. That way I could update the file and the article would update too.
Problem is, I’m not sure how to go about it. Ideally I’d like to just type in <txp:show_my_file id="1" form="fileshow" />
or similar, which would then display the content of the file, but searching the docs and the plugins site hasn’t given me a solution.
Can anyone out there help? I know this runs the potential risk of blurting out the code of a ZIP file or similar, but as the only files going in via TXP will be text, I don’t think I’ll mess up that often. :)
Cheers!
Andy.Offline
Re: Displaying the content of a file in an article. Is it possible?
Answering my own question!
In case it’s handy to anyone else, I solved this one with a bit of PHP in the ‘files’ form. Mine now looks like this:
<txp:php>
$filemime = array("pl","sh");
$filename = file_download_name();
$fileext = substr(strrchr($filename, '.'), 1);
if (in_array("$fileext", $filemime)) {
$filecontents = file_get_contents("files/$filename");
echo '<pre><code>'.$filecontents.'</code></pre>';
}
</txp:php>
<p>
<txp:file_download_link><txp:file_download_name /></txp:file_download_link> [<txp:file_download_size format="auto" decimals="2" />] (<txp:file_download_downloads />)
</p>
This checks the file extension of the given file (anything that follows the last ‘.’ in the name) and compares it to the content of the array (in this case containing ‘pl’ for perl files and ‘sh’ for shell scripts). If it matches an item in the array, the content of the file is echoed out.
I suppose this could be done more neatly by checking the Textpattern category of the file and only outputting if it’s a member of ‘scripts’ or something, but I’ve only just thought of that and this is likely to work for me for the time being. :)
Offline
Re: Displaying the content of a file in an article. Is it possible?
Duh. You probably want to throw a $filecontents = htmlspecialchars("$filecontents");
in there too, otherwise you’re not going to be terribly W3C compliant. :)
Offline
Re: Displaying the content of a file in an article. Is it possible?
Check out the new plugin jea_pygments_txp. It’s meant for syntax highlighting but it works with external files.
Piwik Dashboard, Google Analytics Dashboard, Minibar, Article Image Colorpicker, Admin Datepicker, Admin Google Map, Admin Colorpicker
Offline
Re: Displaying the content of a file in an article. Is it possible?
Thanks, MattD – that plugin is really nice.
Offline