Categories > Your tips & tricks >

Downsize subtemplate file size to increase speed

The forum is closed. Please use Stack Overflow for submitting new questions. Use tags: tinybutstrong , opentbs
By: Sarah
Date: 2013-01-31
Time: 19:57

Downsize subtemplate file size to increase speed

I have an application that uses potentially many subtemplates and my processing time was increasing a lot based on how many sub templates I included, despite having relatively little TBS interaction in the subtemplates. I found file size/length to be a real contributor to this delay. Skrol29 helped my put together a quick php script that I run whenever I edit a subtemplate to cut out the unneeded parts and save a smaller version to the cache, while maintaining my WYSIWYG versions in a source folder.

I did this for OpenTBS with .docx files because they happen to save a lot of extraneous information - I probably would not have noticed a problem with a less junk-filled WYSIWYG system. This version is pretty specific to my use of the drawing canvas and pictures as well as shapes, but the structure could be adapted to cut out whatever extra junk you want. This is also useful if you are using parameter 'getpart' in you file inclusion, if you always include the same "part" and it is much smaller than your overall subtemplate.

$TBS = new clsTinyButStrong;

$LOC_DOCX = 'templates/docx/';
class subTemplate{
    var $filename = '',
        $docx_path = 'templates/docx/',
        $xml_path = 'templates/xml/',
        $TBS = null;
    function __construct(clsTinyButStrong $TBS, $filename){
        $this->TBS = $TBS;
        $f = pathinfo($filename);
        $this->filename = $f['filename'];
    public function load_document(){
        echo 'Loading document.xml from '.$this->filename.'.docx... ';
        return $this;
    public function strip_gfxdata(){
        echo 'Stripping gfxdata... ';
        while ($x = clsTbsXmlLoc::FindStartTagHavingAtt($this->TBS->Source, 'o:gfxdata', 0) ) {
            $x->ReplaceAtt('o:gfxdata', '');
            $this->TBS->Source = str_replace(' o:gfxdata=""', '', $this->TBS->Source);
        return $this;
    public function use_only_block($blockName){
        echo 'Stripping to only '.$blockName.'... ';
        $this->TBS->Source = $this->TBS->getBlockSource($blockName);
        return $this;
    public function save(){
        echo 'Saving <a href="'.$this->xml_path.$this->filename.'.xml">'.$this->filename.'.xml</a>... ';
        $file = fopen($this->xml_path.$this->filename.'.xml', 'w');
        fwrite($file, $this->TBS->Source);
        echo 'Done. <strong>'.number_format(filesize($this->xml_path.$this->filename.'.xml')/1024,1).'KB</strong><br>';

//get list of all subtemplates
function get_templates($dir){
    $templates = array();
    $files = scandir($dir);
    foreach($files as $file){
        $f = pathinfo($file);
        if($f['extension'] == 'docx'){
            $templates[] = $file;
    echo "<strong>".count($templates).' templates to process</strong><br>';
    return $templates;
//process subtemplates and save them to xml folder for use
function make_templates($templates){
    global $TBS;
    echo "<ol>";
    foreach($templates as $template){
        $sub = new subTemplate($TBS, $template);
        echo "<li>";
        echo "</li>";
    echo "</ol>";

$templates = get_templates($LOC_DOCX);


I hope someone finds this useful - it took my execution time from about 15 seconds to 2.