2

Using CakePHP 2.2, I would like to generate the following link in a view:

<a href="/teams/edit/${$data.Team.id}">Edit</a>

This markup is later picked up by a Javascript templating engine, hence the ${$data.Team.id} part.

This was my obvious first attempt:

echo $this->Html->link(
    'Edit',
    array('controller' => 'teams', 'action'=>'edit', '${$data.Team.id}')
);

The result:

<a href="/teams/edit/%24%7B%24data.Team.id%7D">Edit</a>

$, {, and } are nicely URL encoded - which I don't want.

OK, but there's an 'escape' => false option, says The Book.

echo $this->Html->link(
    'Edit',
    array('controller' => 'teams', 'action'=>'edit', '${$data.Team.id}'),
    array('escape' => false)
);

The result doesn't change. Apparently, the 'escape' param only influences the handling of actual query strings, in the '?' key of the URL array.

How can I achieve the desired result using the HtmlHelper?

11
  • 3
    It is not valid to have those characters unescaped in the path name. Feb 13, 2013 at 12:06
  • This isn't cake, your browser is encoding those characters. paste <a href="/teams/edit/${$data.Team.id}">Edit</a>; the result is the same. could you use data- attributes?
    – Ross
    Feb 13, 2013 at 12:07
  • @FabrícioMatté: as I've written: the markup goes through further processing by Javascript before it is inserted into the DOM as an actual link. These characters are placeholders and will be replaced.
    – pixelistik
    Feb 13, 2013 at 12:10
  • @pixelistik doesn't really matter. It is a browser mechanic as Ross said. Once the URL is parsed into the DOM as an href property of an anchor element, it will be forced through encoding. Feb 13, 2013 at 12:12
  • @Ross: it definitely is the HtmlHelper. When I directly paste the desired markup, the characters stay as they are.
    – pixelistik
    Feb 13, 2013 at 12:14

2 Answers 2

1

There is no way to achieve what you want. The Router will urlencode all named and passed params. You can use HtmlHelper::url() to generate the /teams/edit/ part and then use simple string concatination.

Edit: You could probably use a custom RouteClass and overwrite the CakeRoute::_writeUrl() method which does the encoding. Pass a special param in the url array to tell it to not encode and that unset that param.

1
  • Thanks for a clear "no". Your suggestion for a workaround is also nice, I'll do it like that. It's just about 1-2 links in my application.
    – pixelistik
    Feb 13, 2013 at 14:46
0

I ended up creating my own helper for this and not using the link helper. In my helper, I used the url helper to generate the base url, then I appended my desired parameters.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.