Search

I just stopped by the irc channel to bug the developers and friends about a curious question. I was wondering if there's a way to evaluate PHP variables inside text areas.

Alistair, of course, had a good answer: that I needed to create a filter that would evaluate the string.

Seeing as that seemed too difficult (haha), I decided to be less cryptic. The reason I needed those variables was that I was linking files and images through the article entry (text area). For example:

<a href="/files/some_file.pdf">My pdf</a>

The issue is that in the development environment, I don't have the web site in the document root of the server. As such, I would get broken anchors or images.

An alternative solution is to do some Ninja XSLT:

<xsl:template match="img[starts-with(@src, '/')]" priority="1">
    <xsl:element name="img">
        <xsl:apply-templates select="@*[name() != 'src']"/>
        <xsl:attribute name="src">
            <xsl:value-of select="concat($root, @src)" />
        </xsl:attribute>
        <xsl:apply-templates select="* | text()" />
    </xsl:element>
</xsl:template>

<xsl:template match="a[starts-with(@href, '/')]" priority="1">
    <xsl:element name="a">
        <xsl:apply-templates select="@*[name() != 'href']"/>
        <xsl:attribute name="href">
            <xsl:value-of select="concat($root, @href)" />
        </xsl:attribute>
        <xsl:apply-templates select="* | text()" />
    </xsl:element>
</xsl:template>

Basically, this looks for all anchors and images that href or src, respectively, begin with a "/" and replaces their respected attribute values with the $root variable available to all XSLT stylesheets.

Hope it helps someone else. If someone, cough Allen, has a better solution, it'd be great if I could see it.

I'll have to take a closer look at your solution. I've done something similar Redirecting internal links to $root URL with xsl:apply-templates and managing internal links within entries, based on Allen's ideas. Looks like you're doing the same thing with slightly different XSL techniques.

Ah yes. Very nice. ;)

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