Sublime Forum

[Solved] Template processing, text expansion?

#1

Is there any way in Sublime Text to place a directive in one file, that when opened by Sublime Text will cause the directive to be replaced by the text in another file that was referred to in the directive? Such as–

[code]
== insert text of file-a here ==

Lots of HTML or other code

== insert text of file-b here ==

More text or code, etc.[/code]
This type of template-like file, containing directives or placeholders that Sublime Text would replace with the actual text from the files referred to in the directives, would make it easy to update a collection of webpages that contain many sections of usually static text or HTML code that are identical in all or most of the pages of the website, but which might be changed from time to time, by inserting the text from the files containing those sections (like upper-navigation.html, header.html, footer.html, left-sidebar-bottom.html) while each such template-like file would contain in plain text form (in addition to the directives to include text from other files) the content that is unique to that particular file, such as the main content of an HTML webpage.

In this way, if one changes any of the parts or pieces that are usually assembled to create a particular webpage, one could merely open the template-like file that contains the “include” directives, to generate a new version of the page that contains any changes made to any of its usually-static parts which are stored in individual files.

This would be much simpler than copying new header or navigation HTML text and then pasting it into each of dozens or perhaps hundreds of pages in a fairly static website that doesn’t rely on PHP and a database.

0 Likes

#2

[size=150]Template Toolkit[/size]

I thought I would answer my own question, for others who might be interested.

Most websites use a dynamic content compilation technique, where PHP scripts on the server gather text, images and other items from an SQL database and then combine them in a certain order to produce a final (and hopefully valid) HTML page to send to the viewer’s web browser.

One of my websites is mostly static, so I simply used a text editor to write the HTML for each page; no PHP nor database is needed. However, the main content text of the pages changes from time to time, as well as some content or the way certain other parts of the page (like the header, the navigation toolbar or footer) are written. While the main content is unique to each webpage, because each page deals with different subject matter, the other parts of the page (the navigation links, footer, header, etc.) are identical on each of the many pages of the website. If I want to change something in one of those areas, I have to copy-and-paste the newly-edited header, for example, into each and every one of the webpages of the entire site, which can become tedious.

That’s why I was asking about a templating system, where a file can contain directives (like a simplified macro language) that would be processed when the file is opened, to substitute or expand, include or import text from other files or snippets (and perhaps do a little programming logic). That way the page would be dynamically generated: the main content unique to the page would be in the file itself, and the header, footer, left sidebar text, right sidebar text, navigation toolbar, etc., would all be copied into the file by the template processing engine that would read the directives in the file and do what the directives requested (such as insert the text contained in the external file header.txt into the current document).

I found such a template processing program, Template Toolkit, written in the Perl programming language but with its own mini-language of directives that do not require any knowledge of Perl. Template Toolkit even has a package for the editor TextMate which also works with Sublime Text 2.

To homepage of Template Toolkit 2

0 Likes