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:
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.
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.
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.
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]
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?
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:
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?
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?
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).
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?
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.
I assume this is not possible or no-one knows how to do it? Can anyone suggest a different method to do this instead?
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
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.
Hi,
use url.FormatUrl() in the umbraco.cms namespace in that case.
Regards,
/Dirk
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.
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
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?
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
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">
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?
Can no-one offer any suggestions on this? Is it not possible?
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">
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
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?
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?
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 ?
is working on a reply...