0

I use TBS and OpenTBS for parsing office templates in a PHP script. Files of type rtf or txt are processed with TBS and those of types docx or pptx with OpenTBS.

The merged files are used in an office context for letters, submissions and the lot, so I want tags that were not merged in the first place, for instance due to missing data, to be removed before the files are produced. A solution that covers both TBS and OpenTBS would be ideal.

At this time I use fields like [onload.nonexistant;ifempty=’’] for TBS but the lengthy syntax is kind of awkward, error prone and doesn't work for OpenTBS.

If an empty default value doesn't work, maybe there is some kind of cleanup operation for remaining fields?

Skrol29
  • 5,402
  • 1
  • 20
  • 25
patmin
  • 117
  • 1
  • 10
  • Did you set the `noerr`option to true ? I'm surprised you get no error with a field sucha as `[onload.nonexistant;ifempty=’’]` when variable `nonexistant` is not set. – Skrol29 Mar 15 '18 at 23:17
  • 'noerr' is set to true but the ifemtpy clause does not hit, even when I use a word instead of empty tags. I looked at the field in a hex editor and the field definition seems right. – patmin Mar 16 '18 at 07:50
  • As it turns out, the `ifempty` clause only gets active if the variable is set to an empty string. – patmin Mar 16 '18 at 20:58

2 Answers2

1

After much poking around in the TBS documentation I finally resolved to write a simple plugin. It catches TBS' output event and removes any unmerged fields. Here is the code:

<?php

define('TBS_CLEANER','clsTbs_Cleaner');

class clsTbs_Cleaner {

    function OnInstall() {
        $this->Version = '0.0.1';
        return array('AfterShow');
    }

    function AfterShow(&$Render) {
        $this->TBS->Source = preg_replace('/\[(onload|onshow)\..*\]/', '', $this->TBS->Source);
        return true;
    }
}

The plugin can be used by including $TBS->PlugIn(TBS_INSTALL,TBS_CLEANER); in the processing PHP file, right after $TBS = new clsTinyButStrong;.

Please feel free to build upon this approach. Note that the given search filter may need some improvement, though. Maybe a recursive filter for balanced square bracket expressions?

patmin
  • 117
  • 1
  • 10
1

Actually there is an inconsistent TBS behaviour (with version 3.10.1 and before) when you merges a field such as [onload.nonexistant;ifempty=display_me], assuming that you have no variable $nonexistant in the global PHP scope nor in the $TBS->VarRef scope.

  • By default such a field displays a TBS error message in the template : « item 'nonexistant' is not an existing key in the array ».
  • If you add parameter noerr inside the fields, then it is correctly merged and is replaced with 'display_me'.
  • But if you don't use parameter noerr and set option 'noerr' to true, then no error is displayed but the field is not merged at all.

The expected behaviour in the last point is that the field should be replaced with 'display_me'. Just like it is when you are using MergeField().

So this is a kind of bug which will be fixed in the next TBS version.

You can patch the current TBS version (3.10.1) by replacing all

if (isset($Loc->PrmLst['noerr'])) {

with

if ( $this->NoErr || isset($Loc->PrmLst['noerr']) ) {

in function meth_Merge_AutoVar(). They are 3 of them.

The other solution in your case is simply to add parameter 'noerr' in all the fields you'd like to fix.

Skrol29
  • 5,402
  • 1
  • 20
  • 25
  • Splendid! The `ifempty` part is not even required if the target string is empty, which makes it a lot more elegant for my needs. – patmin Mar 19 '18 at 07:27