On 1/28/06, Mark Stosberg <[EMAIL PROTECTED]> wrote:
> You may be interested in referencing the Rails APIs for this, since they
> already have methods for this.
>
> They are named:
>
>  url_for
>  link_to (ActionView::Helpers::UrlHelper)
>  link_to_function (ActionView::Helpers::JavaScriptHelper)
>  link_to_if (ActionView::Helpers::UrlHelper)
>  link_to_image (ActionView::Helpers::UrlHelper)
>  link_to_remote (ActionView::Helpers::JavaScriptHelper)
>  link_to_unless (ActionView::Helpers::UrlHelper)
>  link_to_unless_current

I think the only one that really applies is the url_for method.  The
others are all HTML generating methods that happen to also create
URLs.  Those methods are more apt for something like the
HTML::Prototype library (which already supports link_to_function and
link_to_remote).

> I haven't read through their docs enough myself to have an opinion yet
> about how much I like any of them, though.

The docs for 'url_for' are actually quite similar to what I have done,
although the parameters are slightly different.  I followed a
structure that I was already used to which is the
Template::Plugin::URL interface, which just accepts a URL base as the
first parameter, and a hash of query params as the second parameter. 
The URL base is optional and will default to the current URL (minus
the query params and path_info).

>From the query params we can pull out parameters for the path info
depending on the structure of the dispatch table.  Here are a couple
of examples:

Lets say we have the following conditions:
  current URL:  /index.cgi/store/browse
  module name: Example::Store
  dispatch prefix: Example
  root_uri: /eg   (This is prepended to any relative URLs)
  dispatch table:
     ':app/gander/:item'    => { rm => 'view' },
     ':app/:rm/:item'         => { },
     'admin/:app/:rm'       => { prefix   => 'Example::Admin' },
     ':app/:rm'                 => { },

And here are some calls to 'url' with the results:

$self->url( { rm => 'view', item => 'sportscar' } )
    -->  /eg/index.cgi/store/gander/sportscar
$self->url( { rm => 'buy', item => 'sportscar' } )
    -->  /eg/index.cgi/store/buy/sportscar
$self->url( { rm => 'buy', item => 'sportscar' , options =>
['color:red', 'interior:leather'] } )
    -->  
/eg/index.cgi/store/buy/sportscar?options=color:red&options=interior:leather
$self->url( { app => 'Example::Admin::Products', rm => 'list' } )
    -->  /eg/index.cgi/admin/products/list
$self->url( { app => 'Example::Cart', rm => 'add', item =>
'sportscar', qty => 1 } )
    -->  /eg/index.cgi/cart/add/sportscar?qty=1
$self->url( { app => 'Example::Cart', rm => 'clear' } )
    -->  /eg/index.cgi/cart/clear
$self->url( 'http://expensivecars.com/index.cgi', { product => 'sportscar' } )
    -->  http://expensivecars.com/index.cgi?product=sportcar
$self->url( 'css/styles.css' )
    -->  /eg/css/style.css
$self->url( '/js/calendar.js' )
    -->  /js/calendar.js

Of course, I still have to write a test suite to make sure that all of
these actually work properly...  Also, I just typed those out in this
email as examples of what I think should work so it is quite possible
that I made a mistake or two.

Cheers,

Cees

---------------------------------------------------------------------
Web Archive:  http://www.mail-archive.com/[email protected]/
              http://marc.theaimsgroup.com/?l=cgiapp&r=1&w=2
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to