Yii2 how to map HTML anchor tag to yii2 html :: a () tag

Hi guys, I am learning yii2 for one of my product based products. I convert existing code to yii2 html code format and get problems compiling the following:

<a href="grid_options.html"> <div> <i class="fa fa-upload fa-fw"></i> Server Rebooted <span class="pull-right text-muted small">4 minutes ago</span> </div> </a> 

help me guys if anyone has a solution for the above ??????
+7
yii2
source share
4 answers

Besides Ali's answer, which is fully valid, you can also just write

 use yii\helpers\Url; <a href="<?= Url::to('LINK')?>"> <div> <i class="fa fa-upload fa-fw"></i> Server Rebooted <span class="pull-right text-muted small">4 minutes ago</span> </div> </a> 
+8
source share

The following code generates the desired HTML:

 \yii\helpers\Html::a(\yii\helpers\Html::tag('div', \yii\helpers\Html::tag('i', '', ['class' => 'fa fa-upload fa-fw']) . 'Server Rebooted' . \yii\helpers\Html::tag('span', '4 minutes ago', ['class' => 'pull-right text-muted small']) ), \yii\helpers\Url::to('address')); 

To get a clearer code:

 use yii\helpers\Html; use yii\helpers\Url; Html::a(Html::tag('div', Html::tag('i', '', ['class' => 'fa fa-upload fa-fw']) . 'Server Rebooted' . Html::tag('span', '4 minutes ago', ['class' => 'pull-right text-muted small']) ), Url::to('address')); 

Please note that if you want to create a link to a route, use Url::toRoute(['controller/action'])

+5
source share

this may also work :)

 <?= Html::a('<div><i class="fa fa-upload fa-fw"></i> Server Rebooted <span class="pull-right text-muted small">4 minutes ago</span> </div>', ['/grid-options'], ['class'=>'your_class']) ?> 
+1
source share

if you want to use "controller / action" and "Parameters" in your link, than use below URL function

 Url::toRoute(['product/view', 'id' => 42]); 
0
source share

All Articles