Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

yii2

I am learning yii2 for one of my product based webapp. I am converting existing code into yii2 html code format & getting problem while comverting 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>
like image 889
Shaggie Avatar asked Dec 04 '14 14:12

Shaggie


3 Answers

Besides Ali's answer that is totally 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>
like image 50
Mihai P. Avatar answered Nov 09 '22 04:11

Mihai P.


Following code generates your 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 have more clear 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'])

like image 45
Ali MasudianPour Avatar answered Nov 09 '22 05:11

Ali MasudianPour


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']) ?>
like image 39
Kalpesh Desai Avatar answered Nov 09 '22 06:11

Kalpesh Desai