how to include php in Yii tab widget content

I have just copied the code for yii2 tabs guide and want to include a Form in the first Tab and a Gridview in the second tab.

for testing I have edited just one line in the code with ‘<?= “test”’ , but the same is not working

here is the full code:




<?php


echo Tabs::widget([

    'items' => [

        [

            'label' => 'One',

            'content' => '<?= "test"',

            'active' => true

        ],

        [

            'label' => 'Two',

            'content' => 'Anim pariatur cliche...',

            //'headerOptions' => [...],

            'options' => ['id' => 'myveryownID'],

        ],

        [

            'label' => 'Dropdown',

            'items' => [

                 [

                     'label' => 'DropdownA',

                     'content' => 'DropdownA, Anim pariatur cliche...',

                 ],

                 [

                     'label' => 'DropdownB',

                     'content' => 'DropdownB, Anim pariatur cliche...',

                 ],

            ],

        ],

    ],

]);

?>




'content' => '<?= "test"',



with this code you are telling to display not only "test" but also "<?= " because you must specify a string.

In this case you must write


'content' => 'test',

or if you have a long string you can do:




<?php

$content = "

             <div>

              <p>

                 test

              </p>

             </div>

           ";

echo Tabs::widget([

    'items' => [

        [

            'label' => 'One',

            'content' => $content,

            'active' => true

        ],



The problem i see in your code is that you are into


<?php

tag and you want to create another tag


<?=

but into php tag you must use


echo 'test';

Yes I have also tried exactly as suggested by you i.e.


echo 'test';

but then I am getting the error:

syntax error, unexpected ‘echo’ (T_ECHO)

this the exact code:




echo Tabs::widget([

    'items' => [

        [

            'label' => 'One',

            'content' => echo 'test';,

            'active' => true

        ],



:blink:

but i think you are doing confusion. Look at this code


echo Tabs::widget([

    'items' => [

        [

            'label' => 'One',

            'content' => echo 'test';,

            'active' => true

        ],

the first command you do is ECHO, so all of the setting/function you will insert after echo, must return simply a STRING. You can’t repeat echo inside echo!


'content' => echo 'test';,

Here there is a BIG mistake because is the same error of write:


echo echo 'test';;

Let me explain basic concept:

Your web page is HTML.

HTML contain many tag as <div> <form> <table> etc…

There is one tag <?php ?> that tell to the web-server do compile the php code inside the tag.

So if you write




<p>

 Hello Word!

</p>

is exactly to write




<p>

<?php

      $var = 'Hello';

      echo $var . ' World!';

?>

</p>

Your Error is that you are doing is something like this:




<p>

<?php

      $var = 'Hello';

      echo $var . echo 'test';  ;

?>

</p>

I hope you understand my poor english ;)

Thanks I got it now, what I was doing wrong.