addValue problems

Nov 1, 2012 at 10:02 PM
Edited Nov 1, 2012 at 10:02 PM

Hello,  I apologize if this has been answered before.

 

I created a template file a la the Template.php example and it works 'kinda'  Some of the values are replaced as they should be but others not.  For example,

 

a table:

Col 1 Col 2

${year} ${lastyear}

${reports} ${lastreports}

the year and lastreports substitutions work fine. But not the others.  I can't see any diff. bet. these.  Anyone else have a problem like this?  Solution?


Thanks.

 

Nov 8, 2012 at 11:08 PM

Hi There,

I've discovered this, seems to happen only when the templates are made in Word 2010.

I'm using a template with replacement ${client_name} - sometimes Word 2010 will input it as that, Open Office always will.

The client name wasn't replaced, so I put a echo _documentXML in Template::save, and ${client_name} was output as:

${</w:t></w:r><w:bookmarkStart w:id="0" w:name="_GoBack"”) used on a non-void HTML element. Ignoring the slash and treating as a start tag.">/><w:r><w:t>client_name}

So that's never going to be matched properly.

another part in the same document is:

${
</w:t></w:r><w:proofErr w:type="spellStart"”) used on a non-void HTML element. Ignoring the slash and treating as a start tag.">/><w:proofErr w:type="gramStart"<span title="Self-closing syntax (“/>”) used on a non-void HTML element. Ignoring the slash and treating as a start tag.">/><w:r><w:t>}

so you can't even reliably filter all the other tags out.

Would be nice if there was a solution, perhaps something that cleans all that extra crap out
Nov 9, 2012 at 2:33 AM

ok, my solution was to remove any non-letters, I tried changing the replace to _client_code_ but it started adding xml around all the underscores, so I just have tsclientcode and replace that, works great and is workable for what I'm using it for.

Nov 9, 2012 at 1:21 PM

Thanks for your input stunz.  I want to understand your solution better, however.  Are you saying that simply changing the template var names to eliminate non-letters cured the problem, i.e.  ${client_name} => ${clientname}  

OR -- Are you saying that you edited the XML produced?  I am not trying to be dense here but I'm a bit new to all this.

Thanks again for your input to a vexing problem. 

Nov 9, 2012 at 7:32 PM

Hi there,

no I had to remove the ${'s altogether as Word 2010 was putting xml in there, i.e. ${client_name} => tsclientname - ts is abbreviation of the app I'm using it in, and prevents real words being replaced prefixing with that.

So I edited Template::setValue and removed pretty much everything but the last line

Nov 12, 2012 at 9:49 PM

Oh.  That makes sense. I'm going to give it a try and see what happens. Thanks again.  It would be a great feature if I could get it to work :)

Nov 13, 2012 at 12:41 PM
winettsg wrote:

Oh.  That makes sense. I'm going to give it a try and see what happens. Thanks again.  It would be a great feature if I could get it to work :)

Just had to give you a big THANKS.  I just updated the Template->setValue() code as you suggested and it works like a charm.  This is a HUGE help to me.

Nov 20, 2012 at 10:28 PM

If this can help you i would be glad.

I cracked a little bit the function, using some regular expressions in order to:
1. Find the regular expressions commencing with '${' and ending with '}' (in my case i dropped the $ sign cause i needed to use the $ sign in my word file)
2. With only that string, find and eliminate all the opening tags
3. find and eliminate the closing tags in that string
4. replace the old string (with garbage code) with the cleaned string
5. output the _documentXML

this is the piece of code I implemented if anyone is interested

public function setValue($search, $replace) {
$pattern = '|\{([^\}]+)\}|U'; //if you need the $, use: '|\$\{([^\}]+)\}|U''
preg_match_all($pattern, $this->_documentXML, $matches);
$openedTagPattern= '/<[^>]+>/';
$closedTagPattern= '/<\/[^>]+>/';
foreach ($matches[0] as $value) {
$modified= preg_replace($openedTagPattern, '', $value);
$modified= preg_replace($closedTagPattern, '', $modified);
$this->_documentXML = str_replace($value, $modified, $this->_documentXML);
}

if(substr($search, 0, 1) !== '{' && substr($search, -1) !== '}') { //change to: substr($search, 0, 2) !== '${' if you need the $ character
$search = '{'.$search.'}'; //change to '${'.$search.'}' if $ needed
}

if(!is_array($replace)) {
$replace = utf8_encode($replace);
}

$this->_documentXML = str_replace($search, $replace, $this->_documentXML);
}

Nov 21, 2012 at 12:17 PM

That's an interesting approach.  I may try adding it to the class as a different function (e.g. NewSetValue) and give it a whirl.

 

Thanks.