Sublime Forum

How can I accomplish (templating issue)

#1

I frequently come across this need.
I have a block of text. In that text I have some string. Something like:

var aLink = doc.getSomething("xx", "Link");
callFunction();
write(aLink);

Also I have an array (lines of text)

Link
Image
GoldFish
Car

I want to fill in the template with the array.
3 lines of code, 4 items in the array. So I want 12 lines

var aLink = doc.getSomething("xx", "Link");
callFunction();
write(aLink);
var aImage = doc.getSomething("xx", "Image");
callFunction();
write(aImage);
var aGoldFish = doc.getSomething("xx", "GoldFish");
callFunction();
write(aGoldFish);

...snip...

Is there anyway to do this?

0 Likes

#2

One possible way to do this is via a custom plugin of some sort, which would be custom Python code to do exactly what you would like.

You can also do this out of the box easily using built in Sublime functionality:

  1. The “template” content can be duplicated as many times as you like via simple copy/paste; ensure that the place where the new text is going to be injected is text that is distinct (in your example, the word Link appears in the target location and nowhere else, for example.
  2. For the content to be inserted, select all of the text and split the selection into lines (Selection > Split into Lines from the menu to see the key), and then do a copy to put the lines into the clipboard
  3. In the original content, select all instances of the word Link (in this example) and then do a paste; the number of selections here needs to match the number of lines from the other side

This method won’t work if the text to be inserted is more than one line; it may also be a little cumbersome if there are dozens of things to be inserted. In either of those cases, the plugin is probably the way to go.

0 Likes