CakePHP HTML link - cakephp

I am trying to use CakePHP HTML Linker for the following code
<li class="iAdd"><span>Add Cuisine</span></li>
Since the span tag needs to be inside the a tag. am not able to get the output as need. Any suggestions on how to get it done ?

Disable the escape option in your link code, like so:
<li class="iAdd">
<?php echo $this->Html->link(
'<span>Add Cuisine</span>',
array('action' => 'add'),
array('escape' => false) // This line will parse rather then output HTML
); ?>
</li>

you can always use normal html in links:
$this->Html->link('<span>'.h($text).'</span>', array('action'=>'add'), array('escape'=>false));

Related

How to add <i> and <span> tag inside a tag in cakephp 3?

My html code is given below:
<a href="/patients/index" class="m-menu__link ">
<i class="m-menu__link-bullet m-menu__link-bullet--dot">
<span></span>
</i>
<span class="m-menu__link-text">
Add Medicines
</span>
</a>
and i want to convert it by using HtmlHelper in cakephp 3.
You want to use the 'escape' => false parameter in the link() method. This stops Cake from escaping the markup:-
<?= $this->Html->link(
'<i class="m-menu__link-bullet m-menu__link-bullet--dot"><span></span></i><span class="m-menu__link-text">' . h('Add Medicines') . '</span>',
'/patients/index',
[
'escape' => false,
'class' => 'm-menu__link'
]
) ?>
It's important to remember to still escape any user generated content using h(). I've shown this in the example above by escaping 'Add Medicines', but if this is hardcoded you wouldn't need to wrap it in the h() method.
The below code for add and tag inside a hyperlink in cakephp 3
<?php echo $this->Html->link(
$this->Html->tag("i", "<span></span>",array("class" => "m-menu__link-bullet m-menu__link-bullet--dot")).$this->Html->tag("span", "Add Medicine",
array("class" => "m-menu__link-text")),
["controller"=>"Medicines", "action"=>"index"],
["class"=>"m-menu__link",
"escape"=>false]
);
?>

CakePHP - Trying to use an image instead of a text to link to somewhere else

I´m quite new to CakePHP ... I am using CakePHP 3 and want display an image in a <td>, that links to an URL or a Javascript function.
$this->Html->tag('td', $this->Html->link($this->Html->image('/img/do_something.png'), array('action' => 'do_something')))
My problem is, that the image isn´t displayed but instead it shows:
<img src="/img/do_something.png" alt=""/>
Any ideas ?
Reading the manual usually helps:
HTML special characters in $title will be converted to HTML entities.
To disable this conversion, set the escape option to false in the
$options array.
echo $this->Html->link(
$this->Html->image("recipes/6.jpg", ["alt" => "Brownies"]),
"recipes/view/6",
['escape' => false]
);
Will output:
<a href="/recipes/view/6">
<img src="/img/recipes/6.jpg" alt="Brownies" />
</a>
burzum's answer about using 'escape' => false on the link works great and gives you the ability to easily set attributes on both the link and image. However, if you just simply want to link an image you can do this by setting the url option on the image:-
$this->Html->image(
'/do_something.png',
[
'url' => ['action' => 'do_something']
]
);
Which will give you something like:-
<a href="/controller/do_something">
<img src="/img/do_something.png" alt="" />
</a>
As burzum mentioned, it's all clearly explained in the docs.
Simply Do :
$this->Html->image('image.jpg',['url' => ['action' => 'action_name']]
);

cakephp, how to link with two span

How to add two span tags to a link?
<?php echo $this->Html->tag('span’,
$this->Html->link($v['name']['lastname'],
$v['link’]
);
?>
//Output
<a class=« test" href="./#">
<span>name</span>
<span>lastname</span>
</a>
I'm not sure to be a good start, thank you for the help
Simply concatenate two span tags with the HtmlHelper in the first argument of the link() method.
echo $this->Html->link(
$this->Html->tag('span', $v['name']) .
$this->Html->tag('span', $v['lastname']),
$v['link'],
array('escape' => false)
);
I believe this should do it:
<?php echo $this->Html->link('<span>name</span><span>lastname</span>', '#', array('escape' => false)); ?>

CakePHP 2: Passing Buttons in an element depending on view

Id like to pass buttons from the cakePHP 2 (note: I'm using Twitter Bootstrap 2.2.1) into an Element in app/View/Elements/Toolbar.ctp
<div class="actions">
<?php echo $this->Html->link(__('New User'), array('action' => 'add'), array('class' => 'btn btn-small',)); ?>
</div>
Above is an example of the button.
Depending on the view, I'd like to pass in different Buttons with different actions.
How do I do that? With Elements, Blocks, requestaction() or what?
The cakephp way doing would look like this
In view
<?= $this->element('toolbar', array('button' => $button_type)) ?>
in element there will be variable called $button available. Note that $button_type variable is also available in element.
That should do it

Outputting multiple elements within <a> using CakePHP HTML Helper

I'd like to know if this code...
echo $this->Html->link(
"<h3>test</h3>".$this->Html->image("image.jpg")."<p>Some text</p>",
"/link",
array('escape' => false)
);
...is the best way to generate this HTML in CakePHP...
<a href="/path/to/link">
<h3>test</h3>
<img alt="" src="/path/to/image.jpg">
<p>Some text</p>
</a>
Or, is there a more "correct" way of doing this? I want the and all to be within the tag so that I can set the to display: block; in CSS and have the whole area clickable with a hover effect.
Something tells me that having HTML echoed like this isn't the right way to go about it, but I can't see an alternative if I'm going to use the HTML Helper. Is there one?
Just use the URL method of the HTML helper instead of the link one,do the rest as static HTML, may as well keep the amount of PHP down to a minimum as far as I see it.
<a href="<?php echo $this->Html->url($params); ?>">
<h3>test</h3>
<?php echo $this->Html->image($params); ?>
<p>Some text</p>
</a>
This would be the most efficient way of doing this task. But maybe you should think about your markup structure and what you want to achieve...
I use
$this->Html->tag('li',
$this->Html->link(
'<i class="entypo-book"></i>'.
$this->Html->tag('span', $nom
.$this->Html->tag('span', '32', array('class' => 'badge'))
),
array('controller' => 'Pages', 'action' => 'index'),
array('class' => 'active', 'title' => 'Pages', 'escape' => false)
)
);

Resources