Removing WhiteSpace from URLS

Hi there

Wondering If anyone could assist

I have a handler which contains the following exithandlers...(i have
stripped out some of the code in the handler as its not totally
neccessary for the explanation)

<cffunction name="home">

         <cfset artist = createObject
('component',"myapp.model.mdl_artists")>
    <cfset rc.qryArtist = artist.getArtData(nameArt=rc.name_art)>

<!---Set View--->
<cfset EVENT.setView ("a/home")>

<!---Exit Handlers--->
<cfset rc.xh={artBio='artist.bio',
        artFans='artist.fans'}>

</cffunction>

In the view for this function I create links for artists like this

  <cfoutput query="rc. qryArtist ">
  <p><a href="#event.buildLink(rc.xh.artBio/
#nameArtist#/">#nameArtist#</a> </p>
        </cfoutput>

This produces a link for all the different artists outputted from the
query..

For example...../artist/bio/radiohead
                 ..../artist/bio/boards of canada

The thing is that with the artist names that contain white space the
url looks like this...

artist/bio/boards%20of%20canada

And I would rather it looked like this

artist/bio/boards+of+canada

I know that this can be resolved by changing my code to

  <cfoutput query="rc. qryArtist ">
  <p><a href="#event.buildLink(rc.xhBio/##replace(nameArtist, " ", "+",
"ALL")#">#nameArtist#</a> </p>
        </cfoutput>

But wondered if coldbox had any better way of achieving this?

Many thanks

Assuming “nameArtist” is coming from the “rc.qryArtist” query, you could add an extra colum to the “rc.qryArtist” query like “customEncodedNameArtist” or whatever and then your view code would be like:

#nameArtist#

In other words, move the name conversion into the query that’s created in “myapp.model.mdl_artists”. That way, that custom value will be accessible by any event that creates a “rc.qryArtist” query instead of having to replicate it in every event or view.

  • Gabriel

Or you could do it on the fly…

#replace(urlDecode(event.buildLink(myXH)),’ ‘,’+’,‘all’)#

This is a good idea actually as it would prevent me from having to
replicate the name conversion in every view or handler..

Thanks for your help

Hi there

Bit confused

Where would I place this code?

In the handler?

Thanks

That would go wherever you’re building your links at. Some people build them in handlers, some people build them in views. It’s a personal choice thing.

Ok...im unsure that would assist here, as the part of the link that
needs to be converted is actually outputted from a query in a view...

<cfoutput query="rc. qryArtist ">
        <p><a href="#event.buildLink(rc.xhBio/
#nameArtist#">#nameArtist#</a> </p>
        </cfoutput>

#nameArtist# being the artists name output from the query.....

Im trying to avoid having to convert every link in my views, so I
presumed that gabriels solution would be most efficient?

Or maybe im just misunderstanding your method...

Thanks

Couldn't one use a view helper and combine both suggestions to make it all clean??
just a thought

havent come across view helpers....could you elobarate?

Thanks

Right, so in your example you’d do it in the view. I would do exactly what Gabriel suggested, personally. That is, I would have a column/field in my database which I would grab from. (Or I would have the database do the work when querying.) But since I have no idea if you have that kind of control over the application, I was merely offering another suggestion. :slight_smile:

No no no, I really appreciate your input...sorry if it came across
badly...

I just get a bit confused with some of the suggestions made with
Coldbox, I just like to completely understand what is being
suggested...

I have control over all areas of the application...Im just trying to
do stuff at cleanly and professionally as possible..

Hit the Docs... Can't be verbose on a phone at this moment.

Basically..
If you have a view called artist.cfm. Then make a artistHelper.cfm.. Write a formatLink function that accepts the artist name and any formatting param and return the built link... So In the artist.cfm view you could call #formatLink(artistName)# anywhere without duplicating the code

Ok, just like an UDF..

This is what im looking for, stuff to clean up my code

WIll try and find it in the docs

THanks

Yes just like udf... You could also abstract the format function and put it in the applicationHelper.cfm defined in .xml config.

Now to really screw you up.. You could also achieve this with a custom buildlink function in your requestcontextdecorator.

I heart coldbox.

So if I put it in the applicationHelper I dont have to recreate it
over and over for seperate views?

Im not sure what the requestContextDecorator is...ill check the docs
for this as well

Yeah, i absolutely love coldbox...I find it really fun to develop
with, and it covers all bases...

Heh. No worries. I did not take it “badly”. :slight_smile: