Cakephp span in Html link - cakephp

I have this code
<li id="Tiempo">
<?= $this->Html->link(__('En Tiempo'), ['action' => 'index', 'Tiempo']) ?><span class="label label-success ml-10"><?php echo $entiempo ?></span>
</li>
But i want the span into a but I don't know how

You have to modify the content/title of the link. As the docs point out, this is the first parameter of HtmlHelper->link(). To add the span inside your a tag you have 2 options.
Add the Span in the Link
You could either modify it directly and set the escape options to false:
<li id="Tiempo">
<?= $this->Html->link('<span class="label label-success ml-10">' . $entiempo . '</span>', ['action' => 'index', 'Tiempo'], ['escape' => false]) ?>
</li>
Modify the template
Or you modify the template HtmlHelper uses.
$this->Html->setTemplates([
'link' => '{{content}}</span>',
]);
and use the link() method like this:
<?= $this->Html->link($entiempo, ['action' => 'index', 'Tiempo']) ?>
See Changing the Tags Output by HtmlHelper

Related

In my <a> tag, there are lots of code HTML. How to add Html link (CakePHP)

My code:
<a href="#">
<div class="list_content">
<p class="title"><?php echo $note['Note']['title']; ?></p>
<p class="create_at"><?php echo $note['Note']['create_at'] ?></p>
<p> <?php echo substr($note['Note']['content'], 0,100) . '...' ?></p>
</div>
</a>
How to add <?php echo $this->Html->link('...') ?> in CAKEPHP 2.x
If you want to insert HTML element in any HTML helper, you have to add 'escape' => false. Check the document https://book.cakephp.org/2.0/en/core-libraries/helpers/html.html#HtmlHelper::link
Simple Example:
$this->Html->link('<b>My Content</b>','#',[
'escape' => false
]);
For you case:
$this->Html->link(
$this->Html->div('list_content',
$this->Html->para('title',$note['Note']['title']).
$this->Html->para('create_at',$note['Note']['create_at']).
$this->Html->para(null,substr($note['Note']['content'], 0,100) . '...')
),
'#',
['escape' => false]
);
If you are going to use Aman's answer, remember that by setting 'escape' => false you are disabling a default security feature. So you probably want to make sure you then escape any user input using the h() method:-
$this->Html->link(
$this->Html->div('list_content',
$this->Html->para('title', h($note['Note']['title'])).
$this->Html->para('create_at', h($note['Note']['create_at'])).
$this->Html->para(null, substr(h($note['Note']['content']), 0,100) . '...')
),
'#',
['escape' => false]
);
If you've got a lot of markup you want inside your <a> tags it is sometimes simpler to use $this->Html->url() instead (and can lead to more readable code):-
<a href="<?= $this->Html->url('#') ?>">
<div class="list_content">
<p class="title"><?php echo $note['Note']['title']; ?></p>
<p class="create_at"><?php echo $note['Note']['create_at'] ?></p>
<p> <?php echo substr($note['Note']['content'], 0,100) . '...' ?></p>
</div>
</a>
The only real disadvantage I am aware of doing this second example is that you lose any functionality that you may add to $this->Html->link(), but I suspect this isn't a concern for the majority of users.

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 how to use the PaginatorHelper output follows the effect?

I hope the output effect:
<li class="prev"><i class="icon-previous"></i></li>
The Official Handbook:
You can change the wrapping tag using the tag option:
echo $this->Paginator->prev(__('previous'), array('tag' => 'li'));
Output:
<li class="prev">
<a rel="prev" href="/posts/index/page:1/sort:title/order:desc">
previous
</a>
</li>
I imitate the way it:
<?php
echo $this->Paginator->prev(__('<i class="icon-previous"></i>'), array('tag' => 'li'));
?>
But the output is:
<li class="prev"><i class="icon-previous"></i></li>
What should I do?
try:
echo $this->Paginator->prev(
__('previous'),
array('tag' => 'li', 'class' => 'icon-previous')
);
echo $this->Paginator->prev(__('<i class="icon-previous"></i>'),
array(
'tag' => 'li',
'escape'=>false
));
Also make sure there IS a previous record otherwise the tag is omitted anyway

Cakephp Paginator Custom Styling

I am using the Paginator Component and Helper.
My Code block for Styling is as follows.
<!-- Pagination -->
<div class="pagination">
<ul class="pages">
<li class="prev"><</li>
<li>1</li>
<li>2</li>
<li>3</li>
<li class="next">></li>
</ul>
</div>
When i try to create the pagination using the following
<!-- Pagination -->
<div class="pagination">
<ul class="pages">
<?php if($this->Paginator->hasPrev()) echo $this->Paginator->prev('<', array('tag' => 'li', 'escape' => false)); ?>
<?php echo $this->Paginator->numbers(array('separator' => false, 'tag' => 'li', 'currentClass' => 'active')); ?>
<?php if($this->Paginator->hasNext()) echo $this->Paginator->next('>', array('tag' => 'li', 'escape' => false)); ?>
</ul>
</div>
The Active class is given to the li tag but my template uses the active class to the a-href tag
is there any way i can make cake give it to the a tag?
You will have to extend and override the PaginatorHelper::number() method for that.
Then use your customized paginator helper app wide by using the aliasing feature to make it available as $this->Paginator in your views to replace it in all views.
public $helpers = array(
'Paginator' => array(
'className' => 'MyPaginator',
)
);
Instead of overwriting the whole method you could do this as well:
public function numbers($options = array()) {
$numbers = parent::numbers($options);
/* see explanation below */
return $numbers;
}
Explanation: Use DOM or regex to find, modify and replace the <li class="active"><a></a></li> active <li> element with your modification. This might be the better way because if the core behavior changes you just need to adept your replacement logic instead of having to figure out what has changed in the code and update your method. This bootstrap paginator helper does it this way.

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