Copied to clipboard

Flag this post as spam?

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


  • trfletch 598 posts 604 karma points
    Mar 05, 2009 @ 17:09
    trfletch
    0

    Using the replace function to create a dynamic URL

    Hi,

    I'm not sure the best way to explain this but I want to create a link based on an umbraco page field, I have the following code which works fine on single words but it causes a problem when the page field contains two words:

    .aspx">

    The page field is called "articletype" and contains values such as:

    Editorials
    Features
    High Profile

    I also have pages by with the same names as above so I want the links to point to those pages. If it is for one of the single worded "articletype" page fields such as "Editiorials" then it works fine because it gives me the following link "www.mywebsite.co.uk/Editorials.aspx" but if it is for one with more than one word such as "High Profile" it gives me "www.mywebsite.co.uk/High%20Profile.aspx" which does not work because the url for the High Profile page is "www.mywebsite.co.uk/high-profile.aspx".

    I wondered if I could somehow use Umbraco.Library:replace which I have briefly read about to replace blank spaces with hypens but I don't know how I would use this within my link code (if it is even possible). Any help or suggestions would be much appreciated.

  • trfletch 598 posts 604 karma points
    Mar 09, 2009 @ 10:29
    trfletch
    0

    I assume this is not possible or no-one knows how to do it? Can anyone suggest a different method to do this instead?

  • Dirk De Grave 4541 posts 6021 karma points MVP 3x admin c-trib
    Mar 09, 2009 @ 11:09
    Dirk De Grave
    0

    Hi tony,

    Can't you just use umbraco.library.NiceUrl() and feed it with an id of the page instead of using the name? Any particular reason to use your method?

    Regards,
    /Dirk

  • trfletch 598 posts 604 karma points
    Mar 09, 2009 @ 11:29
    trfletch
    0

    The link is there to take users back to the parent section of that particular article. I have a set of pages named as follows:

    Editorials
    Features
    High Profile

    These pages list the articles that are of the page type (i.e. the Editorials page shows all the articles that have Editorial selected on the "articletype" drop down).

    I then have a set of articles, these articles need to have a link that takes them to the one of the pages above depending on what is selected in the "articletype" dropdown (i.e. an article with Editorials selected in the "articletype" drop down needs to be listed on the Editorials page, also that article then needs to contain a link to take you back to the Editorials page).

    This would be easy if the articles were child pages of the relevant article type but there is a reason why this cannot be the case.

    The articletype dropdown is a manually populated drop down list.

  • Dirk De Grave 4541 posts 6021 karma points MVP 3x admin c-trib
    Mar 09, 2009 @ 11:58
    Dirk De Grave
    0

    Hi,

    use url.FormatUrl() in the umbraco.cms namespace in that case.

    Regards,
    /Dirk

  • trfletch 598 posts 604 karma points
    Mar 09, 2009 @ 12:14
    trfletch
    0

    Hi, thank you for the quick response. I am not family with url.formatUrl or the umbraco.cms namespace. Could you please explain further. Sorry for being a pain.

  • Dirk De Grave 4541 posts 6021 karma points MVP 3x admin c-trib
    Mar 09, 2009 @ 13:27
    Dirk De Grave
    0

    Hi,

    there's a class 'url' in the cms assembly, which has a static function called FormatUrl() which replaces some chars (those chars are stored in the umbracoSettings.config configuration file)

    In your case, it will replace the space with a '-'.

    It's pure .net code, which you should be able to use from the template. Not sure about yet how to use in combination with the
    [code]
    .aspx">[/code]

    Regards,
    /Dirk

  • trfletch 598 posts 604 karma points
    Mar 09, 2009 @ 17:43
    trfletch
    0

    Thank you for the response, I have checked the umbracoSettings.config file and I can sort of see what you mean, it is setup to replace blank spaces (" ") with hypens ("-") in page links. So what I need to do is find some way to apply this to my URL so that it changes any blank spaces to hypens rather than to '%20'. I did try to use [code]FormatUrl(.aspx">)[/code] but that did not work. Has anyone else come across anything like this and could therefore throw some light onto the problem?

  • Dirk De Grave 4541 posts 6021 karma points MVP 3x admin c-trib
    Mar 09, 2009 @ 18:33
    Dirk De Grave
    0

    Hi Tony,

    It might be a good idea to use following code snippet in your template:

    [code]Node currentNode = Node.GetCurrent();
    string articleType = currentNode.getProperty("Alias").Value.ToString();

    string url = umbraco.cms.helpers.url.FormatUrl(articleType);[/code]

    and use url to feed the a href's attribute:
    [code]
    [/code]

    (Not tested though, so might need to tweak a bit...)

    Regards,
    /Dirk

  • trfletch 598 posts 604 karma points
    Mar 17, 2009 @ 11:42
    trfletch
    0

    Hi Dirk,

    Thank you for the response on this, I have just started looking at this issue again (I got sidetracked on something else). I assume I am going to have to create an XSLT file then because currently I have the following code directly in a Masterpage:

    .aspx">

  • trfletch 598 posts 604 karma points
    Mar 17, 2009 @ 12:47
    trfletch
    0

    Ok well I think I know that I'm going to have to create an XSLT file to do this, currently I have the following directly in my template:

    [code].aspx"> [/code]

    I assume I am going to have to transfer this to an XSLT file and use something similar to the following somewhere in the code:


    [code][/code]

    But I don't know what to put where the X's are or how the rest of XSLT should look to make it work like my link currently does (but allow "articletypes" with more than one word) . Can anyone help me on the bits I cannot figure out, or suggest a better method?

  • trfletch 598 posts 604 karma points
    Mar 18, 2009 @ 20:35
    trfletch
    0

    Can no-one offer any suggestions on this? Is it not possible?

  • trfletch 598 posts 604 karma points
    Mar 20, 2009 @ 11:00
    trfletch
    0

    I'll give it one last try before I give up asking. Can anyone offer any advice on this? Even just to tell me how to get the following out of a template and into an XSLT file then I can try and work out how or if I can use the replace command to convert spaces to hypens?

    .aspx">

  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Mar 20, 2009 @ 13:19
    Lee Kelleher
    0

    Hi Tony,

    I can understand your frustration, but my advice is that XSLT is the best way to go for this.

    Here's what I'd suggest, try this XSLT...

    [code]
    ]>

    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxml="urn:schemas-microsoft-com:xslt"
    xmlns:umbraco.library="urn:umbraco.library"
    exclude-result-prefixes="msxml umbraco.library">





    [/code]


    To quickly talk you through it...

    1. The XSLT checks if the currentPage has a property called "articletype" (and that it has a value).
    2. We call umbraco.library:GetXmlNodeByXPath, passing the "articletype", to pull back the XML node for that page. We're doing this in order to get hold of the nodeId for that page.
    3. Using the nodeId, we can call umbraco.library:NiceUrl to display the URL of the page ... this is much better than messing around with string replacements, etc.

    It might seem a bit overkill to be calling GetXmlNodeByXPath, but given your document data-type/structure, it's a quick way of getting it to work.

    A better alternative would be to use a Content Picker data-type so that you can pass through the nodeId directly to the XSLT (or page template).

    Let me know how it works out.

    Cheers,
    - Lee

  • trfletch 598 posts 604 karma points
    Mar 23, 2009 @ 11:37
    trfletch
    0

    Excellent, thank you for your response that works perfectly. This has been bugging me for ages. I don't completely understand how it works but I'm just glad it does. When you say about using a content picker do you mean making the dropdown automatically populate by selecting the level above those pages such as an ultimate picker?

  • trfletch 598 posts 604 karma points
    May 14, 2009 @ 14:36
    trfletch
    0

    Hi,

    I am not sure what is going on but this no longer seems to work the way I want it to, instead of me getting: www.mywebsite.com/xxxxxx

    It is showing the full path then the bit on the end like so www.mywebsite.com/subsection/subsection/xxxxxx

    Which is not what I want, I want it to always show the link article type link at the root no matter what page I am on. Can anyone help?

  • Brittany McCain 6 posts 26 karma points
    Aug 17, 2011 @ 21:35
    Brittany McCain
    0

    Where in the Umbraco Version 4.7 is the url.FornatUrl() method located. I cannot find which .dll to reference in my visual studio project ?

Please Sign in or register to post replies

Write your reply to:

Draft