MMBase tips

Compilation of my most favorite escapers

tip #15 / Mon 28 Jun 2010 / André / 0 comments

The taglib has several useful escapers for formatting text. These are some of my favorite and most used.

The 'escaper' attribute on mmbase tags that write values and the <mm:escaper /> itself have some great and useful ways to format text, these are the ones I used most. Most of the documentation of the escapers can  -besides <mm:escaper /> -  be found in the taglib documentation of <mm:content />.

Substring a sentence to 32 characters and append ... as ellipsis.

<!-- define an escaper -->
<mm:escaper type="substring" id="wrapper">
  <mm:param name="from" value="0" />
  <mm:param name="to" value="32" />
  <mm:param name="ellipsis" value="..." />

<!-- use it on a field named 'intro' -->
<mm:field name="intro" escape="wrapper" />

Wrap the intro of an article in paragraph tags when needed and append a class named 'intro'.

<mm:field name="intro" escape="paragraph(intro)" />

Sometimes an editor for a contains html-editor which write <p> ... intro here ... </p>, when it does this escaper will omit adding the paragraph tags. The result:

<p class="intro">Hi, I'm the intro.</p>

Another one I use often is the 'links' escaper:

<mm:field name="text" escape="links" />

Any link within the text, like for example www.toly.nl or written like http://www.toly.nl/, are automagically recognized and are wrapped in a link tag. Most escapers, when it makes sense of course, can be combined this one and the previous example.

<mm:field name="text" escape="paragraph,links" />

There is also an escaper that removes tags in stead of adding them.

<!-- define the escape with id 'stripper' -->
<mm:escaper type="tagstripper" id="stripper">
  <mm:param name="escapeamps" value="false" />

<!-- strip all html-tags from field 'intro' -->
<mm:field name="intro" escape="stripper" />

I will keep updating this page with more examples, partly as a reference for myself.

Escape and un-escape an url

MMBase its <mm:link /> and <mm:url /> tag escape url's by default. Consider for example:

<mm:import id="first">first value</mm:import>

<mm:link page="http://www.toly.nl/" referids="first">

Which will print "http://www.toly.nl/?first=first+value".
The <mm:import externid=".." /> tag escapes it automatically back for you when you import the value into another page:

<mm:import externid="first" />
<mm:write referid="first" />

But when that's not possible you need to define an escaper first and invert it before use.

<mm:escaper referid="url" inverse="true" id="undo_url_escaping" />
<mm:write referid="first" id="undo_url_escaping" />

That prints "first value" in stead of "first+value".

Encrypt and decrypt a string

This example uses encryption. This escape works both ways, first create an escaper to encrypt strings.

<mm:escaper id="encrypter" type="encryption">
  <mm:param name="algorithm" value="Blowfish" />
  <mm:param name="format" value="hex" />
  <mm:param name="direction" value="encrypt" />

And an escaper to decrypt them again.

<mm:escaper id="decrypter" type="encryption">
  <mm:param name="algorithm" value="Blowfish" />
  <mm:param name="format" value="hex" />
  <mm:param name="direction" value="decrypt" />

It can be used like this.

<mm:import id="str" reset="true">pindakaas!</mm:import>
<mm:import id="encrypted_str" reset="true"><mm:write referid="str" escape="encrypter" /></mm:import>
encrypted: <mm:write referid="encrypted_str" />
decrypted: <mm:write referid="encrypted_str" escape="decrypter" />

Prints for example "encrypted: eeaa7a3568ab38de680bd9c63dc10527 decrypted: pindakaas!". Other encryption algorithms include 'AES' and 'DES', the formats can be 'hex' and 'base64', see the <mm:escaper /> docs.


RSS feed

Tweet this article

Most mentioned links:

MMBase documentation
MMBase Taglib reference
The reference for all tags mm