File Type Templates Module
Modules
It is a little bit hard to explain what this module is meant for. Perhaps I start by telling the story of how this module came about.
Once upon a time a client asked for a solution to publish their own uploaded FLV videos in news entries. They are no HTML-experts so it was supposed to be an easy solution.
I added a custom File-Upload field to the weblog for them and put a conditional into the entry-template that would embed a FLV-Player if a file was present.
The next day they concretized their requirements. What they actually meant was that they wanted to be able to post an arbitrary number of video clips per entry and position them wherever in the body text they saw fit, just like they do with images now.
This gave me quite a headache at first but on second thought, unknowingly, they actually provided a good approach in their request: Just like with images.
When you upload a browser-supported image type and choose Embedded in Entry, Expression Engine provides the necessary HTML-code to display it, namely an <img>-tag.
When you upload anything else and choose Embedded in Entry, Expression Engine provides just a link to the file.
Hence I quickly wrote a plugin that checks the text for the existence of any links to FLV-files and replaces them with the code needed to embed a flash-player which loads the file. I told them to just upload the FLV-files and place the resulting link-codes where the videos are supposed to appear inside the text and they lived happily ever after.
I actually liked the basic idea behind this little proprietary plugin so I carried it a bit further. The result is a module that lets you define your own template for any non-image filetype which will then replace links to files of that type in your entries. In these templates you can use 4 variables for filename, complete URL to the file, filesize (not available for remote files) and linktext to reformat the output. Optionally you can also define templates per filetype per weblog.
Usage example A:
The download-links on this site are formatted using the File Type Templates Module.
The actual code in the entry looks like this:
<a href="{filedir_3}ft_templates.zip">ft_templates.zip</a>
I created a FT template for .zip-files in this weblog that looks like this:
<a href="{file}" style="text-decoration:none;">
<div style="border:1px dotted silver; padding:4px;">
<img src="http://gadgets.silenz.org/images/zip.gif" style="border:0px; float:left;" />
Download: {filename}<br />
{filesize}
</div>
Hence any links to .zip-files automatically render:
Download: ft_templates.zip
6.19 KB
Usage example B:
The second entry on the Testsite is a plain weblog entry with links to three uploaded .flv- and one .mp3-files.
I created a template for FLV:
<div style="text-align:center;">
<br />
<object width="320" height="240" id="{filename}">
<param name="movie" value="http://gadgets.silenz.org/downloads/player.swf?movie={file}&autoload=on&autorewind=on">
<embed src="http://gadgets.silenz.org/downloads/player.swf?movie={file}&autoload=on&autorewind=on" width="320" height="240" type="application/x-shockwave-flash" />
</object>
<br />
<strong>{linktext}</strong>
</div>
and one for MP3:
<object align="middle" height="15" width="400">
<param name="movie" value="http://gadgets.silenz.org/downloads/xspf_slim.swf?song_url={file}&song_title={linktext}">
<param name="quality" value="high">
<embed src="http://gadgets.silenz.org/downloads/xspf_slim.swf?song_url={file}&song_title={linktext}" quality="high" type="application/x-shockwave-flash" align="middle" height="15" width="400">
</object>
The same entry with the FTT module applied.
Installation:
- Copy the folder named ft_templates into the modules folder inside your system-directory.
- Copy the file named lang.ft_templates.php into the /language/english folder inside your system-directory.
- Navigate to Modules in your CP and find the module named File Type Templates. Click Install.
- Now you have access to the Settings of your newly installed module.
Control Panel Backend:
The interface should be pretty self-explaining.
In the row titled “Add new template” you can ... add a new template.
- In the first column you choose the file type the template applies to.
- In the second you may choose a weblog.
If you do so the template is only valid for entries of this weblog.
If you leave it blank, you create a general template that is valid for all weblogs.
If you, for one file type, create a general template and a weblog specific template the specific template will supersede the general one in the corresponding weblog while the general one is valid for all other weblogs. - The third column holds the actual template that will replace all links to files of the chosen file type. There are four variables you can use inside the template. They are explained right under the field so I won’t elaborate on them here.
Existing templates will show up in the rows above and can be either edited or deleted.
The Tag:
There is just one tag: {exp:ft_templates}{/exp:ft_templates}.
It is supposed to be placed inside an {exp:weblog:entries}-tag-pair and should enclose any fields that may contain links to be replaced.
{exp:weblog:entries}
{title}
{exp:ft_templates weblog_id="{weblog_id}"}
{body}
{another_field}
{/exp:ft_templates}
{/exp:weblog:entries}
The parameter weblog_id="{weblog_id}” is optional and can be omitted if no weblog-specific file type templates have been created for the weblogs involved.





