3 users online. Create an account or sign in to join them.Users
Replace text with data from context
This is an open discussion with 5 replies, filed under General.
Search
If I understand correctly that your content is stored in your db without the abbr tag and you want to insert the abbr tag before the content is displayed on a page, then you can do this:
In a custom data source intercept the content (after it is extracted from the db but before it is displayed), insert (search and replace) the abbr tag and then send the content on to be displayed. There are many examples of custom data sources on this forum, but I can show you one if you decide to go this way and need help.
@wisolman Thanks for the response. I tried digging in to do what you suggested and got stumped, so I would very much appreciate and example.
No need for a custom DS, you could also do this in XSLT (original by Dave Pawson):
<?xml version='1.0' encoding='utf-8'?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="*" mode="html">
<xsl:element name="{name()}">
<xsl:apply-templates select="* | @* | text()" mode="html"/>
</xsl:element>
</xsl:template>
<xsl:template match="@*" mode="html">
<xsl:attribute name="{name()}">
<xsl:value-of select="."/>
</xsl:attribute>
</xsl:template>
<xsl:template match="text()" mode="html">
<xsl:call-template name="replace-acronyms">
<xsl:with-param name="text" select="." />
</xsl:call-template>
</xsl:template>
<xsl:template name="replace-acronyms">
<xsl:param name="acronyms" select="/data/acronyms/entry" />
<xsl:param name="text" />
<xsl:choose>
<xsl:when test="not($acronyms)">
<xsl:value-of select="$text" />
</xsl:when>
<xsl:when test="not(string($text))" />
<xsl:otherwise>
<xsl:variable name="acronym" select="$acronyms[1]/acronym" />
<xsl:variable name="explanation" select="$acronyms[1]/explanation" />
<xsl:choose>
<xsl:when test="contains($text, $acronym)">
<xsl:variable name="before" select="substring-before($text, $acronym)" />
<xsl:variable name="after" select="substring-after($text, $acronym)" />
<xsl:call-template name="replace-acronyms">
<xsl:with-param name="text" select="$before" />
<xsl:with-param name="acronyms" select="$acronyms[position() > 1]" />
</xsl:call-template>
<acronym title="{$acronyms[1]/explanation}">
<xsl:value-of select="$acronym" />
</acronym>
<xsl:call-template name="replace-acronyms">
<xsl:with-param name="text" select="$after" />
<xsl:with-param name="acronyms" select="$acronyms" />
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="replace-acronyms">
<xsl:with-param name="text" select="$text" />
<xsl:with-param name="acronyms" select="$acronyms[position() > 1]" />
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
The stylesheet assumes that your acronyms are in /data/acronyms
All you have to do in your pages is start the ninja HTML modifier by replacing
<xsl:copy-of select="text/*" />
with
<xsl:apply-templates select="text/*" mode="html" />
The only problem I’ve stumbled across is that this utility will also replace substrings of words, i.e. “abbr” in “abbreviation” will get a <acronym /> tag as well…
While phoque’s XSLT approach looks kind of complicated it might be your best bet. The custom data source approach is also complicated by the fact that you are working with two different sections. A DS extracts from only one section, so you would have to query the db to get data from the other section. This can be done, of course, but the solution gets quite lengthy.
@phoque - Your last statement implies that thadd is searching on “abbr.” My understanding is that he is searching on other terms and inserting abbr tags. But no matter what he is searching on there is a possibility for ambiguity.
Thanks guys, this looks really promising and I’ll give it a stab this weekend. I appreciate all the help!
Create an account or sign in to comment.
I have an idea to improve the usability of my site, but I’m at a loss for how to implement it and would appreciate any ideas the community has to offer.
I have two contexts: Page Content and Glossary. The Glossary contains a list of terms and definitions that’s used in my FAQ and the Page Content contains the markdown that goes on the page. What I’d like to do is search through the Page Content when it’s output and replace any of my terms with an HTML abbr tag.
For example, let’s say my page content is “I need this information ASAP please.” and I have “ASAP” in my glossary defined as “As Soon As Possible”. I’d like to replace this in output as:
“I need this information ASAP please.”
Does anyone have any ideas how to approach this? Thanks!