CakePHP – html helper how to turn off escaping? - php

I use Cake 2.2.2, and to build a link I use HtmlHelper.
<?php echo $this->Html->link('Link',array('controller' => 'mycontroller', 'action' => 'myAction', '3'."#map"), array('escape' => false));?>
I need to pass value 3 to my controller and I also need the link to have #map (html anchor).
But despite the fact that I use array('escape' => false), output appears to be escaped and #map becomes 3%23map.
Where I made a mistake? Thanks.

http://book.cakephp.org/2.0/en/core-libraries/helpers/html.html#HtmlHelper::url
you need to use
<?php echo $this->Html->link('Link', array(..., '#' => 'map'), ...);?>

Related

Cakephp how to remove underline on links

New to cakephp came across a scenario where I have to make a text and an image a link.
Fortunately it was successful, however I noticed that there is an underline on my link and I have to remove it, as you can see on the picture below + the code on how I made it like that:
Cakephp Code:
echo $this->Html->link(
$this->Html->image('img/f.png', array('height' => '40', 'width' => '40'))
. '' . ('forensics express&nbsp&nbsp&nbsp&nbsp'),
'http://example.com', array('escape' => false));
normally this would be easy just by adding:
style="text-decoration:none"
in the css, however since I'm new to cake I am not aware of the syntax on how I can put an id or class that I can use for css, or in what I prefer, directly adding the style="text-decoration:none" in the array.I tried this one but it didn't work.
echo $this->Html->link(
$this->Html->image('img/f.png', array('height' => '40', 'width' => '40'))
. '' . ('forensics express&nbsp&nbsp&nbsp&nbsp'),
'http://example.com', array('escape' => false,'style'=>'text-decoration:none'));
How can I solve this? Any help is appreciated
This may help
echo $this->Html->link(
$this->Html->image('img/f.png', array('height' => '40', 'width' => '40'))
. '' . ('forensics express&nbsp&nbsp&nbsp&nbsp'),
'http://example.com', array('escape' => false,'class'=>'no-decoration'));
In the stylesheet, you can create this class
.no-decoration{
text-decoratino:none;
}
You can use this as reference link : How to call CSS class on a CakePHP Html->link?
Just set it in your css (stylesheet) like this, instead of inline code.
a {text-decoration:none;}

CakePHP - Creating a link URL without the anchor tag

I am using CakePHP and want to create a URL to a controller/view without including the anchor tag.
In other words if I use
$this->Html->link('foo',array('controller'=>'bar','action'=>'display'));
Then the output is a formatted link that can be displayed... but I just want the URL without the HTML around it.
echo $this->Html->url(array('controller' => 'bar', 'action' => 'display'));
With optional second parameter to make it a full URL including http:// and so on:
echo $this->Html->url(array('controller' => 'bar', 'action' => 'display'), true);
I just needed the same thing, but it changed on Cake 3.
Now we have to use:
echo $this->Url->build(["controller" => "bar", "action" => "display","bar"]);
If you only need URL:
echo $this->Html->url(array('controller'=>'bar','action'=>'display'));

How to add <b> tag in HTML helper

I am try add <b> tag inside <a> in cake php
I need output like that
<b>Logout</b>
but i dont how to add tag in this code
<?php echo $this->Html->link('Logout', '/users/logout'); ?>
Please keep in mind that styling should not be part of your HTML Output and - as already suggested by my previous posters - should be in your CSS.
However, there you go (note the escape=false):
echo $this->Html->link(
'<b>' . __('Logout') . '</b>',
array(
'controller' => 'users',
'action' => 'logout',
),
array(
'escape' => false,
)
);
or even more HtmlHelper magick:
echo $this->Html->link(
$this->Html->tag('b', __('Logout')),
array(
'controller' => 'users',
'action' => 'logout',
),
array(
'escape' => false,
)
);
Edit: added Ish Kumar's suggestion for localisation, in cakephp 2.0 we don't need the "true" anymore ;)
One more thing: if you use escape=false keep in mind to sanitize the tags content (in this case the <b>Logout</b>) by yourself, especially if its generated user content e.g. <b>$userInputVar</b>.
My advice would be don't try to use the helpers for every single task, additionally you should use CSS to add bold to the logout link.
echo $this->Html->link('Logout', array('controller'=>'users', 'action'=>'logout'), array('class' => 'logout'));
Then in your CSS:
.logout {
font-weight: bold;
}
Update: If you REALLY REALLY must use deprecated HTML tags in your code:
echo $this->Html->link('<b>Logout</b>', array('controller'=>'users', 'action'=>'logout'), array('class' => 'logout', 'escape' => false));
That's the equivalent of doing this:
<b><?php echo $this->Html->link('Logout', '/users/logout'); ?></b>
As Dunhamzzz noted, you're better off using a CSS class and styling it that way.

How to add a class to all the input elements inside a form in cakephp

How do i use the inputDefaults to add a common class to all the input elements in my form. also pls give a brief description of the inputDefaults.
isn't it:
echo $this->Form->create('User', array(
'inputDefaults' => array(
'class' => 'someclass'
)
);
`
You should read the cookbook. theres a good example: http://book.cakephp.org/view/1639/options-inputDefaults
When you create a form you add inputdefaults key in options:
echo $this->Form->create('User', array(
'inputDefaults' => array(
'div' => array('class' => 'someclass')
)
);
After browsing the source file i didn't find anything either. So the only way is to use it explicitly for every call to the input function.

CakePHP custom route w/ %20 in url want to replace w/ "-" not sure how to make this happen in the route

My link:
echo $link->link($planDetailsByCompany['PlanDetail']['name'],
array('controller' => 'plan_details', 'action' => 'view_benefit_schedule',
'id' => $planDetailsByCompany['PlanDetail']['id'],
'slug' => $planDetailsByCompany['PlanDetail']['name']));
My custom route:
Router::connect('/pd/:id-:slug',
array('controller' => 'plan_details', 'action' => 'view_benefit_schedule'),
array('pass' => array('id', 'slug'),
'id' => '[0-9]+'));
My url is displaying like so:
..pd/44-Primary%20Indemnity
I cannot determine how to remove the %20 and replace it with a "-". There is a space in the company name that is causing this. Is this possible within the CakePHP router functionality? If so, how? Or another method.
Geeze.. I just solved this!
In my link above, replace the 'slug' line with:
...'slug' => Inflector::slug($planDetailsByCompany['PlanDetail']['name'])...
The Inflector handles the spaces in the url. And my result url is:
...pd/44-Primary_Indemnity

Categories