TinyButStrong - the PHP Template Engine
Categories > Your tips & tricks >

Use a batch file to simplify OpenTBS templating

The forum is closed. Please use Stack Overflow for submitting new questions. Use tags: tinybutstrong , opentbs

By: Sarah
Date: 2013-01-03
Time: 01:29

Use a batch file to simplify OpenTBS templating

This is for Windows users working with Word templates (who have or can get 7-zip or another command line zip interface).

If you have a complicated or fragile template like I do (saving my .docx in Word would destroy almost all of my TBS tags), you may find yourself repeatedly changing the file extension of your .docx to .zip, going in, copying out the document.xml, editing it, saving it, copying it back into the .zip archive, and changing the extension back to .docx. This is error prone and slow and annoying but as I have gotten steady sicker of it, I have decided on some things that make it easier.

First, I keep a perfect copy of my .docx that has been changed to .zip and unzipped as a folder. This speeds it up and allows you to edit document.xml easily without risking overwriting another document.xml accidentally. I keep this folder with my OriginalFileName.docx. so it looks like this:
MyProject
  OriginalFileName
    _rels
    docProps
    word
      document.xml
      <etc>
OriginalFileName.docx

Then I wrote a batch file that does the annoying stuff for me. I just edit MyProject/OriginalFileName/word/document.xml and run copy-document-xml.bat which (for me) looks like:
M:
cd <path to MyProject>\MyProject
copy OriginalFileName\word\document.xml word/document.xml
rename OriginalFileName.docx OriginalFileName.zip
"C:\Program Files (x86)\7-Zip\7z.exe" a OriginalFileName.zip word\document.xml
rename OriginalFileName.zip OriginalFileName.docx

This changes to the drive letter I want and then changes directories until it arrives in MyProject location (this may not be necessary for you, I am working from a network drive and can't accept UNC paths as a cmd starting point...). You can take out the first two lines if when your bat runs, it starts in the current folder.
Then it copies the document.xml you edited to the right level of hierarchy to fit into your docx.
It renames the .docx to .zip so you can interact with it with 7-zip.
Next you will need to include the path to your command line zip interface (you can see mine in the quotes). If you use 7-zip, the 'a' means add to archive. If you use something else, you will need to look at their documentation.
Finally it renames the zip archive (your template in disguise) back to its original name and extension.

If you upload your template somewhere, you could add an ftp command to this file and really reduce friction.

Most people probably won't need this, but I made it earlier today and it has already saved me a ton of time and annoyance (as well as probably prevented some errors!) so I thought it might be worth sharing.

Thanks for reading, hope it helps.
By: Skrol29
Date: 2013-01-07
Time: 10:30

Re: Use a batch file to simplify OpenTBS templating

Thanks for sharing this tip.

By the way, OpenTBS is based on a PHP class that is able to manage ZIP archive un full PHP.
http://www.tinybutstrong.com/tools.php

This can give ideas to build your batch above in PHP (if have it in PHP may have any utility, which is not sure).