Search

I had an idea for an extension based on what I’ve done for my URL Router extension that would basically allow a generic regex search/replace on the final output of a page.

This could be used, for example, for putting developer-centric comments in templates and have them visible when logged in as admin but stripped for regular users.

This would be one simple use, but I am sure there would be others, and the flexibility of regex lends itself to infinite possibilities.

Any takers?

I think XSLT is a much more powerful output control method. It would be a breeze to implement the thing you mentioned with XSLT.

Also, doing that in the XSLT keeps everything in one place.

I hope I didn’t misunderstand what you were trying to tell. :-)

Phoque - far be it from me to taint the clean separation of data/presentation!!

I’d be interested to know how you would apply such a manipulation rule across an entire site. e.g. stripping every HTML comment that began with the word “DEV” inside, or changing every URL that began with a certain domain to some other domain (rather than go through every reference by hand to update).

Anyway just a thought, I’m a relative newcomer to XSLT so I have a lot to learn.

I’d be interested to know how you would apply such a manipulation rule across an entire site.

<xsl:template match="/">
    <xsl:apply-templates select="data/test/entry/body/*" />
</xsl:template>

<xsl:template match="p[starts-with('DEV')]" priority="1">
    <xsl:if test="/data/event/user-is-logged-in/blabla">
        <p class="dev"><xsl:value-of select="substring-after(., 'DEV')" /></p>      
    </xsl:if>
</xsl:template>

<xsl:template match="body//*">
    <xsl:element name="{name()}">
        <xsl:apply-templates select="* | @* | text()"/>
    </xsl:element>
</xsl:template>

<xsl:template match="body//@*">
    <xsl:attribute name="{name(.)}">
        <xsl:value-of select="."/>
    </xsl:attribute>
</xsl:template>

Nils - Could you break down exactly what that does?

Rob,

Nils more or less beat me to it, but the ideal way to handle this would be in your XSl templates. What Nils has above is the Ninja technique of applying templates to your elements rather than just copying them. This would allow you to handle your URL rewrite by creating a template for the href attribute that would replace the URL you want to replace.

Regarding the comments, I’m not sure about doing it in PHP, but you might be able to use match='comment()' to write a similar template for your HTML comments. That is assuming that your comments are in your source XML, and you want to output them. By default, comments you have in your templates shouldn’t be output anyway, so any dev notation should be kept serverside anyway.

Another less ideal approach is to use EXSL for multi-pass transformations. That involves putting the result of your first pass into an xslt variable, then applying your second pass templates to that variable, wrapped in a exsl:node-set() function.

Nils - Could you break down exactly what that does?

As Andrew said, I’m using Allen’s HTML ninja technique. I couldn’t live without it.

I can see the benefit and reasoning behind the techniques displayed above, and from a developer’s perspective it absolutely makes sense to use them.

However, I was looking at this more from an extension point of view, something that is a way to do search/replace wrapped in a simple interface. Another example application might be a blacklist word filter, whereby a list of words could be replaced or removed (e.g. bad language).

Anyway I was just thinking out loud really!! Thanks for the pointers that will help further my XSLT.

Allen’s HTML ninja technique. I couldn’t live without it.

Same here. The Ninja technique opens the doors.

@rob

If you want to go the php/extension route, i think you’ll do your filtering on the FrontendOutputPostGenerate delegate’s $Output. Take a look in class.frontendpage.php

If you want to go the php/extension route, i think you’ll do your filtering on the FrontendOutputPostGenerate delegate’s $Output. Take a look in class.frontendpage.php

Exactly, I’d looked in the delegate list and it seemed it should be straightforward to apply regex serach/replace. Perhaps I’ll try a blacklisted words module as a test.

Create an account or sign in to comment.

Symphony • Open Source XSLT CMS

Server Requirements

  • PHP 5.3-5.6 or 7.0-7.3
  • PHP's LibXML module, with the XSLT extension enabled (--with-xsl)
  • MySQL 5.5 or above
  • An Apache or Litespeed webserver
  • Apache's mod_rewrite module or equivalent

Compatible Hosts

Sign in

Login details