Alternative method for opening Smarty templates with HTML syntax highlighting in gedit

December 16th, 2008 – 11:41 am
Filed as: code, ubuntu

Here is an update to a previous post regarding how to get gedit to recognise Smarty template *.tpl files. I’ve found that gedit prefers to use the mime type to decide what type of file it is and therefore what syntax highlighting to apply.

After a little investigation I found that most of my .tpl files were being identified as text/plain in Nautilus (found by right-clicking on the file and selecting properties, on the basic tab the type field will tell you the mime type nautilus is seeing it as)

My new solution to this problem is to convince nautilus (and gedit) that the *.tpl files are in fact HTML files and therefore syntax highlight them in the same way, and here’s how to do it:

  1. Make a backup of your freedesktop.org.xml file with the following commands:

    cd /usr/share/mime/packages
    sudo cp freedesktop.org.xml freedesktop.org.xml.backup

  2. Edit the freedesktop.org.xml file as a super user:

    sudo gedit /usr/share/mime/packages/freedesktop.org.xml

  3. Search the file for “*.php” (without the quotes). You should find a section with:

    <glob pattern="*.php"/>
    <glob pattern="*.php3"/>
    <glob pattern="*.php4"/>

  4. Add a new glob entry for tpl files by adding the line:

    <glob pattern="*.tpl"/>

  5. Save and close the file then run the following command which should be fairly self-explanatory:

    sudo update-mime-database /usr/share/mime

  6. Nautilus and gedit should now recognise .tpl files as html files and syntax highlight as such

The only drawback with this option (and I consider it a rather minor inconvenience) is that double clicking your tpl files will try and open them in Firefox by default now, you can change this to open in gedit by default but then all your .html and .htm files will open in gedit by default too.

You choose which you prefer, I personally have no problem leaving them to open in Firefox by default since I rarely double click tpl files in nautilus, I usually use the file browser plugin in gedit.

Thanks to a suggestion by Sudhanshu I’ve edited the post to add the tpl entry to the php section of the freedesktop.org.xml file instead of the html section. The benefit of this is that you can now double click tpl files and have them open in the same application as php files (usually gedit) while html files will still open in your browser.

As always comments are appreciated on whether this was helpful or if you’ve found a better way.

8 Comments

» Leave a comment now

» RSS feed for comments on this post
» TrackBack URI

  1. 1

    [...] Update: I have posted an alternative way to do the same thing this post describes, the alternative way should be more reliable. See it here http://www.c3b.co.uk/2008/12/16/alternative-method-for-opening-smarty-templates-with-html-syntax-hig... [...]

    Pingback made by c3b» » Smarty template syntax highlighting with gedit on December 16, 2008 @ 12:13 pm

  2. 2

    Your new solution works!
    Now Gedit is highlighting .tpl files automatically
    Thanks a lot!

    Comment made by PHP coder on December 19, 2008 @ 8:18 pm

  3. 3

    Glad I could help, thanks for the comment :)

    Comment made by admin on December 20, 2008 @ 12:11 am

  4. 4

    Great. Thanks a lot. I was looking for this long time ago.

    Comment made by Bolorino on December 25, 2008 @ 11:25 am

  5. 5

    This works, but to avoid in Firefox, I put the extension under the php section.

    Comment made by Sudhanshu on January 4, 2009 @ 12:13 am

  6. 6

    @Sudhanshu That’s a good idea, I’ve now done this myself as it seems like a more elegant solution. I should’ve thought of it myself. I’m also going to update the post to make the suggestion

    Thanks

    Comment made by waylon on January 4, 2009 @ 12:36 pm

  7. 7

    Thanks for the great how-to. It was driving me crazy to have to select the highlighting syntax manually whenever I was working with tpl files.

    Comment made by Scott on April 11, 2009 @ 12:14 am

  8. 8

    [...] article is from http://www.c3b.co.uk/?p=32. I just rewrite this article here for my studying. Thanks Waylon for such a good article. Miss [...]

    Pingback made by Smarty template file with HTML Syntax Highlight in Gedit | Just For Fun With Us on September 15, 2009 @ 1:56 am


Leave a Comment

  1. XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>