How to add content inside link in cakephp - cakephp-3.0

I would like to have content inside my link with cakephp. I know I can use this syntax to have an image inside my link
$this->Html->image("recipes/6.jpg", [
"alt" => "Brownies",
'url' => ['controller' => 'Recipes', 'action' => 'view', 6]
]);
For an HTML output like this :
<a href="/recipes/view/6">
<img src="/img/recipes/6.jpg" alt="Brownies" />
</a>
But the above code idn't quite what I'm looking for. I would like to have the following HTML output
<a href="/recipes/view/6">
<div>
<img src="/img/recipes/6.jpg" alt="Brownies" />
</div>
</a>
I came with this code witch could work but I will have to figure out the full path link.
<a href="<?= '/recipes/view/6' ?>">
<div>
<?= $this->Html->image("recipes/6.jpg", ["alt" => "Brownies"]); ?>
</div>
</a>
Is there a more robust way of doing what I want with cakephp?

I am not sure if this is a more robust way, but you could possibly do something like this:
<?php
echo $this->Html->link(
$this->Html->div(null, $this->Html->image('/img/recipe/6.jpg')),
array('controller' => 'recipes','action' => 'view', 6),
array('escape' => false)
);
?>
And the output would be this:
<a href="/recipes/view/6">
<div>
<img alt="" src="/img/recipes/6.jpg">
</div>
</a>

You can use Link or URL mechanism.
Using Link
echo $this->Html->link(
$this->Html->div(null,
$this->Html->image('recipes/6.jpg', ['alt' => 'Brownies'])),
array('controller' => 'Recipes', 'action' => 'view', 6),
array('escape' => false)
);
Using URL
Adding the URL inside the HTML
<a href="<?= $this->Html->url( array('controller' => 'Recipes', 'action' => 'view', 6),
array('escape' => false)); ?>" class="light_blue">
<div>
<?= $this->Html->image("recipes/6.jpg", ["alt" => "Brownies"]); ?>
</div>
</a>

Related

cakephp - How to user $this->Html->link as an <a> tag

I am doing a small project using cakephp and I am fairly new at it. I am doing a navbar with login/logout depending the state of the user. The problem, which is minor, is that I want to make the logout look like an tag. At this moment, it just looks like a link because, well, it's a link.. This is the code that I have so far:
<?php
if(isset($loggedInUser))
{?>
<?php echo "<a class='nav-item nav-link active'>".$this->Html->link('Logout',['controller' => 'Users', 'action' => 'logout']);?></a> <!--I want to make this look like an <a> tag-->
<?php }?>
<?php
if (!isset($loggedInUser))
{?>
<a class="nav-item nav-link" href="<?=$this->request->webroot?>users/login">Login</a>
<?php }?>
<?= $this->Html->link('<span class = "nav-item nav-link active"></span> Logout,
['controller' => 'Users', 'action' => 'logout'],
['escape' => false]);
?>
OR you can use URL build
URL BUILD
<a href="<?= $this->Url->build(['controller'=>'Users', 'action'=>'logout','_full'=>true]); ?>">
<span class = "nav-item nav-link active"></span>
Logout
</a>

Yii2 Breadcrumbs with microdata

How to add microdata (schema.org) to Yii2 Breadcrumps?
I have the following code in the application layout:
<?= Breadcrumbs::widget([
'links' => isset($this->params['breadcrumbs']) ?? []
]); ?>
I want to add microdata attributes for breadcrumbs (https://schema.org/BreadcrumbList)
Add attribute values to options, itemTemplate and activeItemTemplate:
<?= Breadcrumbs::widget([
'links' => $this->params['breadcrumbs'] ?? [],
'options' => ['class' => 'breadcrumb', 'itemscope' => true, 'itemtype' => 'http://schema.org/BreadcrumbList'],
'itemTemplate' => '<li itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem">{link}</li>' . PHP_EOL,
'activeItemTemplate' => '<li class="active" itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem">{link}</li>' . PHP_EOL,
]); ?>
UPDATE:
In fact, for Google it does not work as expected: itemprop="position" and itemprop="item" required inside <li></li>. See https://developers.google.com/search/docs/data-types/breadcrumb

Yii2 How to Use Html::tag or Html::beginTag

i want to echo like this
<li>
<img src="img/h4-slide.png" alt="Slide">
<div class="caption-group">
<h2 class="caption title">some_title
</h2>
<h4 class="caption subtitle">Dual SIM</h4>
<a class="caption button-radius" href="some_route"><span class="icon"></span>check</a>
</div>
</li>
here my code for render image carousel :
$slides = [];
foreach (Slide::find()->orderBy('sortOrder')->all() as $slide) {
/** #var $slide common\models\Slide */
$slides[] = [
'content' => Html::img(Yii::$app->urlManager->baseUrl . '/uploads/slide/' . $slide->id . '.jpg'),
'caption' => Html::tag('content-group', $slide->title)."<br>".$slide->body,
];
}
And my carousel :
<div class="slider-area">
<div class="block-slider block-slider4">
<?= Carousel::widget([
'id' => 'bxlider-home',
'items' => $slides,
'options' => [
'class' => 'slide',
'data-interval' => 3000,
],
'controls' => [
'<span class="bx-next fa fa-angle-left"></span>',
'<span class="bx-prev fa fa-angle-right"></span>',
],
]) ?>
</div>
</div>
how to Slide->title, slide->body, and some links can be in class caption-group ?
I think it would be better to create new partial file.
Create a new file called _slider.php
Call $this->render('_slider') inside configuration of the slider. Please check below code.
$slides = [];
foreach (Slide::find()->orderBy('sortOrder')->all() as $slide) {
/** #var $slide common\models\Slide */
$slides[] = [
'content' => $this->render("_slider"),
'caption' => Html::tag('content-group', $slide->title)."<br>".$slide->body,
];
}
You can write html inside _slider.php easily now. Don't need to use Html::beginTag() etc.
Generating Tags
The code generating a tag looks like the following:
<?= Html::tag('p', Html::encode($user->name), ['class' => 'username']) ?>
<p class="username">samdark</p>
$options = ['class' => ['btn', 'btn-default']];
echo Html::tag('div', 'Save', $options);
// renders '<div class="btn btn-default">Save</div>'
Hyperlinks:
<?= Html::a('Profile', ['user/view', 'id' => $id], ['class' => 'profile-link']) ?>
Images:
<?= Html::img('#web/images/logo.png', ['alt' => 'My logo']) ?>
generates
<img src="http://example.com/images/logo.png" alt="My logo" />
Lists:
<?= Html::ul($posts, ['item' => function($item, $index) {
return Html::tag(
'li',
$this->render('post', ['item' => $item]),
['class' => 'post']
);
}]) ?>
Even more?
visit

Blade, Use html inside variable being passed into partial view is not being rendered

I'm using a partial view to display page header, that view accepts two variables one is Icon the other is a title.
pageHeader.blade.php:
<div class="page-header">
<div class="row">
<!-- Page header, center on small screens -->
<h1 class="col-xs-12 col-sm-4 text-center text-left-sm"><i class="fa {{$icon}} page-header-icon"></i> {{$title}}</h1>
</div>
and I'm using it like so:
#include('zdashboard._partials.pageHeader',['icon'=>'fa-pencil','title'=>'<strong>Editing</strong>'.$center->translations()->whereLang('en')->first()->name])
Sometimes I like to make one word strong or italic like the example above but, blade engine won't render the HTML tags I'm typing as part of title variable (the output like the photo down).
So is there any idea how to solve this? Am I doing it!
wrong?
The output
By default in Laravel 5 {{ $title }} construction wil be escaped.
If you don't want the data to be escaped, you may use the following syntax:
{!! $title !!}
Read more about Blade control structures: http://laravel.com/docs/5.0/templates#other-blade-control-structures
view
#if(Session::has('success'))
<div class="alert alert-success">
{!! Session::get('success')!!}
</div>
#endif
Controller
public function store(Request $request)
{
$this->validate($request, [
'product_code' => 'required|unique:products',
'product_name' => 'required',
'description' => 'required',
'price' => 'required',
'brand_id' => 'required',
'category_id' => 'required',
]);
$product = Product::create([
'product_code' => $request->input('product_code'),
'product_name' => $request->input('product_name'),
'description' => $request->input('description'),
'price' => $request->input('price'),
'brand_id' => $request->input('brand_id'),
'category_id' => $request->input('category_id'),
]);
return redirect()->route('products.index')->with('success',
"The product <strong>".$product->product_name."</strong> has successfully been created.");
}

How to add span tag in CakePHP HtmlHelper?

I have a html tag -
<li>
<a class="" href="http://www.sitename.com/logout">
<i class="icon-key"></i>
<span>Logout</span>
</a>
</li>
I want write above html in CakePHP Html Format-
My Code-
<li>
<?php echo $this->Html->link(
$this->Html->tag('i', 'Logout', array('class' => 'icon-key')),
'/logout',
array('escape'=>false));
?>
</li>
My code is working fine but span tag not showing. How to add span tag in above html?
Thanks
ChatFun
Consider this following code: Simply using two tags of cakephp with concatenating them using .
<?php echo $this->Html->link(
$this->Html->tag('i', '', array('class' => 'icon-key')).$this->Html->tag('span', 'Logout'),
'/logout',
array('escape'=>false));
?>
Gives output:
<a href="/savaganza/logout">
<i class="icon-key">
</i>
<span>Logout</span>
</a>
You can also do :
<li>
<?php echo $this->Html->link(
$this->Html->tag('i', '<span>Logout</span>', array('class' => 'icon-key')),
'/logout',
array('escape'=>false));
?>
</li>