Search code examples
htmlreplacetagssmalltalkpharo

Pharo Smalltalk. copyReplaceAll in html file template to static page file


I have a HTML template file where I've created field tags using my own delimiters. For example:

"This is html with ~~fieldName~~ embedded."

I need to read the HTML template file and replace all of my ~~fieldNames~~ with replacement text, then write out the new static HTML file.

Here's a terse version of what I've tried:

file := StandardFileStream fileNamed: 'inHtmlTemplate.html'. 
aString := file contentsOfEntireFile. 
outHtmlString := aString copyReplaceAll:'~~fieldName~~' 
    with: 'newString' asTokens: false.

The problem is that the method copyReplaceAll:with:asTokens: is implemented in String class, not the concrete subclass ByteString (and the copyReplaceAll:with:asTokens: method answers 'doesNotUnderstand'). The contents of the HTML file, when converted to aString becomes an instance of ByteString.

In summary I need to search-and-replace all my field tags in the HTML template then output the resulting static HTML files.

Using Pharo 4, or a later version of Pharo — how can I get the above to work correctly?


Solution

  • I cannot reproduce your problem in Pharo 6.1. Your example works fine for me. It does not matter if you have an instance of ByteString, since it's a subclass of String and therefore will also understand copyReplaceAll:with:asTokens:. If you get a does not understand error check the top frame in the debugger. Chances are high that you have a typo in your code or an Object not inheriting from String

    Pharo6.1-copyReplaceAll:with:asTokens: