Copied to clipboard

Flag this post as spam?

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


  • Jonas Berling 30 posts 50 karma points
    Aug 17, 2011 @ 19:37
    Jonas Berling
    0

    How to make bracket syntax work

    I have a macro with one parameter. Calling that macro with a static value works just fine, like:

    <umbraco:Macro area="MyArea" Alias="ListNews" runat="server"></umbraco:Macro>

    But I want to call the macro with a dynamic parameter, picked from my query string, and thought "bracket syntax" would do the trick:

    <umbraco:Macro area="[@area]" Alias="ListNews" runat="server"></umbraco:Macro>

    But even though my address bar states someting like http://mydomain.com/myPage.aspx?area=MyArea the macro seems to receive an empty parameter and diaplsying the parameter in the page renders an empty string.

    What am I missing?

    /Jonas

  • Dirk De Grave 4541 posts 6021 karma points MVP 3x admin c-trib
    Aug 17, 2011 @ 21:51
    Dirk De Grave
    0

    you ain't missing anything, syntax is correct and should reproduce exactly that result. Meanwhile, if you're still stuck on this, fetch the querystring var in code (either xslt through umbraco.library:RequestQuerystring('key') or through c# code)

     

    Cheers,

    /Dirk

  • Jonas Berling 30 posts 50 karma points
    Aug 17, 2011 @ 22:52
    Jonas Berling
    0

    Thank you, Dirk. Doing it in the XSL feels tricky as I actually need the macro itself to work with the parameter being set both both statically and from the query string. I'm an old time classic ASP hacker, any hints on how I would do it in C#? I tried

    <umbraco:Macro area="<%=Request.QueryString("area")%>" Alias="ListNews" runat="server"></umbraco:Macro>

    but that did not work (and I didn't expect it to).

    From (the very little) I know about ASP.NET I would create a code-behind file, but that is not what I should do when running Umbraco, is it?

    /Jonas

  • Jonas Berling 30 posts 50 karma points
    Aug 17, 2011 @ 22:53
    Jonas Berling
    0

    Oh, and I guess I should mention that I run Umbraco version 4.5.1, if that matters.

     

    /Jonas

  • Dirk De Grave 4541 posts 6021 karma points MVP 3x admin c-trib
    Aug 17, 2011 @ 23:06
    Dirk De Grave
    0

    Jonas,

    Yeah, mixing quotes and double quotes can get messy, and won't work indeed. Anyway, if you need it work both ways, still can do the same trick. Check in xslt whether a value is passed (if it's set statically) and if so, you're done and use that value. If no value is passed, ie. empty string, check for the querystring var? Does that make sense?

    And, no, version doesn't matter in this case :D

     

    Cheers,

    /Dirk

  • Jonas Berling 30 posts 50 karma points
    Aug 18, 2011 @ 12:26
    Jonas Berling
    0

    Finally made it work. Did as you said but the other way around, tested if the request param existed and if not assumed a static param could be used. this was because I could not find a way to compare the static param with an empty string - comparing it with '' always returned false. I could however test the request param for existance. See code example below.

    First assigned to local vars:

    <xsl:variable name="reqArea" select="umbraco.library:RequestQueryString('area')"/>
    <xsl:variable name="area" select="/macro/area"/>

    Then tried these different versions:

    <xsl:if test="$reqArea">   <!-- Works fine -->

    <xsl:if test="$area">   <!-- No good - always returns true even if no parameter is set-->

    <xsl:if test="$area = ''">   <!-- No good - always returns true even if no parameter is set-->

    If anyone knows what I'm doing wrong it would be interesting to know. But in terms of the original question the problem is solved.

    Thanks for all help!


     

     

  • Don Ross 28 posts 49 karma points
    Aug 19, 2011 @ 13:04
    Don Ross
    0

    Is it that you want to test first if coming in on query string, if not then use macro parameter? I would use a <xsl:choose> in this scenario, but it may not meet your needs. Hard to tell from the info provided if you want one to override the other.

    Don

  • Jonas Berling 30 posts 50 karma points
    Aug 19, 2011 @ 13:33
    Jonas Berling
    0

    Well, the base problem was that I can't make [@queryParam] work. That aside I can't seem to test (with neither xsl:choose nor xsl:if) if a given param is equal to the empty string (''). Both <xsl:if test="$area"> and <xsl:if test="$area = ''"> as well as the equivalents using xsl:choose returned true whatever I value I gave the parameter. I solved it by instead first creating a local var (<xsl:variable name="reqArea" select="umbraco.library:RequestQueryString('area')"/>) and then doing <xsl:choose test="@reqArea">. That returns true only if I have set the parameter in the query.

    I'm confused about not being able to test if a given (static) param is empty or not.

     

    /Jonas

  • Don Ross 28 posts 49 karma points
    Aug 20, 2011 @ 01:30
    Don Ross
    0

    Hey Jonas,

    The reason you're test is returning true is because if the [area] parameter is not populated at the time the macro is called, then the literal '[area]' string is passed in. If you run an xsl:choose, always test the querystring first, then if it is empty, you could have another xsl:when that tests that $area is != '[area'] and $area is != ''.

    This will definitely solve your issue.

    Don

  • Don Ross 28 posts 49 karma points
    Aug 20, 2011 @ 01:32
    Don Ross
    0

    Hey Jonas,

    The reason your test is returning true is because if the [area] parameter is not populated at the time the macro is called, then the literal '[area]' string is passed in. If you run an xsl:choose, always test the querystring first, then if it is empty, you could have another xsl:when that tests that $area is != '[area'] and $area is != ''.

    This will definitely solve your issue.

    Don

  • Jonas Berling 30 posts 50 karma points
    Aug 20, 2011 @ 10:01
    Jonas Berling
    0

    Aha, that was very interesting! As mentioned earlier I've solved the problem pretty much like that, but this is very good to know for future needs.

    Just curious, I guess there is some kind of typo in you post. You wrote

    $area is != '[area'] and $area is != ''

    The single quoutes on the first expression is wrong, I guess. Should it be

    $area is != '[area]' and $area is != ''

    or

    $area is != ['area'] and $area is != ''

    I suspect that the first version is the correct one, right?

    /Jonas

Please Sign in or register to post replies

Write your reply to:

Draft