[TxMt] Re: Questions about Templates

Dru Kepple dru at summitprojects.com
Wed Jul 8 16:24:41 UTC 2009


> From: Neil <kngspook at gmail.com>
> 
> 3. As I make these Templates, I'm finding a lot of common text between them,
> and when I want to tweak it across the board...well, it's like programming
> without functions. What, if anything, can I do to reduce redundancy in my
> templates?

I'm sure this is possible, and I've fiddled about with doing something
similar, but haven't put too much effort into this.  You can execute Ruby
from within snippets, and in theory you could put your repository of
reusable snippet text within a file or a preference or something, and just
put the text in through Ruby.

So, I just threw something together, and got this working.

1. In your bundle on the hard drive, create a folder called "Support"
(right-click the bundle in ~/Library/Application Support/TextMate/Bundles/,
choose show package contents)

2. In this folder, create a few ruby files that will contain your reusable
snippet text.

3. Each file might look like this:

print 'Here is some reusable text.'

4. In your snippet, add this "tab stop" to load the file and insert the
text:

This is my snippet... ${1:`#!/usr/bin/env ruby
    require ENV['TM_BUNDLE_SUPPORT'] + '/SnippetRepo'
    `}$0 

Starting with the ${1: is the embedded ruby that executes; I don't know if
you know ruby or not, but all it does is load and execute that file you
created in the Support folder.  Again, not sure how much you know, but
TM_BUNDLE_SUPPORT provide the path to the bundle's Support folder.  You have
to manually create the Support folder (as far as I know), and it has to be
at the root level of the bundle, spelled right, etc.  But then
TM_BUNDLE_SUPPORT provides easy access to files that your bundle will use,
but isn't directly a Snippet, or Command, or whatever.

The disadvantage with this technique is that, as far as I know, ruby can
only be executed from the tab stops, so when the snippet expands and you tab
through the other stops, this boiler plate text will get highlighted, as
well, and potentially altered.

Also, I'm not sure how to be more intelligent about the text inserted this
way; I'm sure it's possible, but I don't know if the snippet can communicate
with the script you run or vice versa.  You can, however, run ruby as if it
were a command, so you should have access to all of those shell variables
that you can use in commands, like TM_FILENAME and the like.

I hope that helps; please post any further advancements you make in this
area.  I'm interested, as well!

+dru




More information about the textmate mailing list