Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Laurence Gillian 597 posts 1214 karma points
    Mar 12, 2010 @ 13:01
    Laurence Gillian
    0

    Variable Mashups

    Hi there,

    A natural progression of working with Umbraco for us is now building super flexible (and hopefully smart!) XSLT macro's which mean we can roll out developments faster. This works around the fact we use grids for our projects.

    It all works nicely, however I was wondering if anyone had any idea's on how to refactor this code.

    Look at the $ModuleWidth and get the correct grid for it.

    --

    The first code snippet is how in a weird Laurence world this would work, (but can't!)

    Get the value of $grid_(x)

    Where x = $moduleWidth (which is an integer)

    <img>
        <xsl:attribute name="src">
            <xsl:text>/umbraco/ImageGen.ashx?image=</xsl:text>
            <xsl:value-of select="umbraco.library:GetMedia($media,'1')/data [@alias = 'mediaCrops']//crop[@name = 'Portrait']/@url"/>
            <!-- This code needs refactoring -->
            <xsl:text>&amp;width=</xsl:text>
          <xsl:value-of select="$grid_$moduleWidth"/>

     

    This is the code which does however work;

    <img>
        <xsl:attribute name="src">
            <xsl:text>/umbraco/ImageGen.ashx?image=</xsl:text>
            <xsl:value-of select="umbraco.library:GetMedia($media,'1')/data [@alias = 'mediaCrops']//crop[@name = 'Portrait']/@url"/>
            <!-- This code needs refactoring -->
            <xsl:text>&amp;width=</xsl:text>
            <xsl:choose>
                <xsl:when test="$moduleWidth = 1">
                    <xsl:value-of select="$grid_1" />
                </xsl:when>
                <xsl:when test="$moduleWidth = 2">
                    <xsl:value-of select="$grid_2" />
                </xsl:when>
                <xsl:when test="$moduleWidth = 3">
                    <xsl:value-of select="$grid_3" />
                </xsl:when>
                <xsl:when test="$moduleWidth = 4">
                    <xsl:value-of select="$grid_4" />
                </xsl:when>
                <xsl:when test="$moduleWidth = 5">
                    <xsl:value-of select="$grid_5" />
                </xsl:when>
                <xsl:when test="$moduleWidth = 6">
                    <xsl:value-of select="$grid_6" />
                </xsl:when>
                <xsl:when test="$moduleWidth = 7">
                    <xsl:value-of select="$grid_7" />
                </xsl:when>
                <xsl:when test="$moduleWidth = 8">
                    <xsl:value-of select="$grid_8" />
                </xsl:when>
                <xsl:otherwise></xsl:otherwise>
            </xsl:choose>
        </xsl:attribute>
        <xsl:attribute name="alt">
            <xsl:value-of select="@nodeName"/>
        </xsl:attribute>
    </img>

     

    Thanks in advance!

    Hope this isn't a dumb problem, but more of an interesting one! It's getting me thinking! Laurie

  • Harald Ulriksen 207 posts 248 karma points
    Mar 12, 2010 @ 13:09
    Harald Ulriksen
    1

    <xsl:value-of select="$grid_$moduleWidth"/> will not work as $grid_$moduleWidth is not a valid xpath expression.

    You can rewrite the <xsl:choose to an xslt extension if you wish to compress the visual size of it.

    Or perhaps just make your own version of GetMedia which takes an extra parameter and returns the url with the correct width.

    /Harald

  • Laurence Gillian 597 posts 1214 karma points
    Mar 12, 2010 @ 13:20
    Laurence Gillian
    0

    Indeed, but is there a way of writing <xsl:value-of select="$grid_$moduleWidth"/>  as a variable its self, which then produces the correct output. Having to use a <xsl:choose> isn't really ideal as it ties you down into that array, (and plus its visually quite large!) 

    Maybe I'm just being anal! Lau

  • Benjamin Howarth 305 posts 771 karma points c-trib
    Mar 12, 2010 @ 13:20
    Benjamin Howarth
    0

    Hi Lau,

    Harald's right - store the array using an XSLT extension, I think dynamic evaluation of an expression is a bit beyond XSLT's basic capabilities.

    Benjamin

  • Chriztian Steinmeier 2726 posts 8320 karma points MVP 4x admin c-trib
    Mar 12, 2010 @ 13:40
    Chriztian Steinmeier
    2

    Hi Laurence,

    You should be able to use a variable with XML content for this - just need to convert it to a nodeset before you can XPath your way into it:

    <xsl:variable name="grid">
        <value id="1">10</value>
        <value id="2">14</value>
        <value id="3">36</value>
        <value id="4">98</value>
        <!-- More values here... -->
    </xsl:variable>
    <!-- Make useable from XPath -->
    <xsl:variable name="gridValues" select="msxml:node-set($grid)" />
    
    <img alt="{@nodeName}">
        <xsl:attribute name="src">
            <xsl:text>/umbraco/ImageGen.ashx?image=</xsl:text>
            <xsl:value-of select="umbraco.library:GetMedia($media,'1')/data [@alias = 'mediaCrops']//crop[@name = 'Portrait']/@url" />
            <xsl:value-of select="concat('&amp;width=', $gridValues/value[@id = $moduleWidth])" />
        </xsl:attribute>
    </img>
    

    /Chriztian 

  • Laurence Gillian 597 posts 1214 karma points
    Mar 12, 2010 @ 13:56
    Laurence Gillian
    0

    Beautiful solution Chriztian (: Thank-you, Laurie

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies