How to use Plugin CopyText in IBM WCM 8.5 and 9

carita-feliz

The problem

Being able to process text using client code is useful for many reasons, for example: sometimes you want to cut text or extract specific patterns, etc. However, we usually have a issues because of special characters like line breaks (\n), let’s go back for a moment to remember how a well-formatted JavaScript variable looks:

var articleDesc = 'Description read from IBM WCM';

As you see, previous variable is well-defined, everything looks good; but what happen if the “value” is something like this:

// Notice the line is broken because of a invisible "\n"
var articleDesc = 'Description read from IBM WCM
';

Previous variable definition is going to fail because \n character is not valid! you could answer, well lets’ use template strings… but those ones are not compatible with our “favorite” browser: Internet Explorer, so avoid the temptation.

The solution

Ok, now the question is: is there any feature of IBM WCM to clean those line breaks? Sure, there is, just add the CopyText plugin as in following block which is not intended for this kind of tasks but it works smoothly:

var articleDesc = '[Plugin:CopyText format="trim" escape="json" text="SOMETHING_THAT_CAN_HAVE_BREAK_LINES"][/Plugin:CopyText]';

For example, let’s imagine we are reading the description of pieces of content:

var articleDesc = '[Plugin:CopyText format="trim" escape="json" text="[Property field='description' type='content' context='current']"][/Plugin:CopyText]';

After applying the plugin, no more break lines will make our front-end to be broken 🙂

var articleDesc = 'Description read from IBM WCM';

That’s it for today, be happy with your code, see you soon!

Añade un Comentario

Leave a Reply

Your email address will not be published. Required fields are marked *