CakePHP View Variables

View variables are those variables that take values from the controller, and these variables pass into view template. By this set() function, we can pass a value to the view template, and we can add any variable from the controller by use of set(). This set variables will be available in both the view and the layout of your action renders.

 Syntax:

Cake\View\View::set (string $var, mixed $value)
$this->set( ‘activevariableName’, ’values’);  

This set function has a variable name and its value. You should always remember to leave out any user data before outputting because CakePHP does not automatically escape from the output. You can avoid user content with the h() function:   

              <?= h($user->bio); ?>

Like:

<?php
<p>My name is <?=h(name)?></p> // this is for security when we passed 

Example: In this example, we will see how we can create variables and pass values from the controller to view.

Here we have created a file SecondController.php, and we created a function fun3() in which we have created a name and tutor variable. Also, we have passed the values of the variables.

<?php
             namespace App\Controller;
             use App\Controller\AppController;
             class SecondController extends AppController
                         {
                                     public function fun3()
                                     {
                          $this->set('name','suresh'); // $name will be used as variable in view file 
                       $this->set("tutor","mahesh");    //$tutor will be used as variable in view file   
                               }
               }
  ?>                                     

Now we will create fun3.ctp file

<p> WELCOME TO THE Tutorials & Examples . IT IS AN ONLINE LEARNING TUTORIAL 
FOR IT PROFESSIONAL.
<br/>
 name: <?php  echo $name; ?>
<br/>
 Tutor <?php  echo $tutor; ?>
</p> 

Output:

view variables

Alternate use of Echo function

Echo, or print a variable for displaying the output in your template file:

 <?php echo $name;
 ?> 

You can also write this syntax without write PHP and echo.

Using Short Tag support:

<? = $name ?>

After coding, run your program on the localhost server like as wamp or xampp, and you will see this type of result;

Similarly, you can also perform in an array:

Example:  You have to write an array on your controller’s action

public function action() {
                   $online = array('name'=> 'Bala', 'teacher' => 'graphics', 'age' => '41');
              };
                  $this->set("data",$online); 
        And in your ctp file you have to write :
                                   <?php print_r($data); ?>  \\ this will show whole array.
   If you want to print only a particular element then
              <?php echo $data['age']; ?>  

There is another method for send your array data to view file by compact() function . In this function of php we can simply pass variable of array without this symbol “$”.

Public function action() {
                   $online = array('name'=> 'Bala', 'teacher' => 'graphics', 'age' => '41');
              }; 
               $this->set(compact("online")); 

And you have to write in action.ctp file:

<?php echo
print_r($online); ?>

Alternative Control Structures

Control structures, like if, for, foreach, switch, and while can be written in a simplified format. Note there are no braces required for terminating the code; instead of this, we can use endforeach tag for the terminating of the code. Also, note that instead of using a semicolon after each statement (except the last one), there is a colon. The following is an example using foreach:

<ul>
 <?php foreach($item as $value): ?>
 <li> <?= value ?></li>
 <?php endforeach; ?>
</ul> 

Another example, using if/elseif/else.

<?php if ($username == ‘amit’):
 ?>
 <h3>Hi amit</h3>
 <?php elseif ($username ==’sumit’):
 ?>
 <h3> Hi sumit </h3>
 <?php else: ?>
    <h3> Hi unknown user</h3>
 <?php endif;  ?> 

View Extending

View extending method allows you to wrap up of one view text onto another view by calling their names using extend function. You can also create view blocks and call in a single block by using the extend function. It works like a parent-child relationship, in which we can call one view into another view by using the extend method.

By using the extending function for a common view file that you want to show on a particular page, then you can use it by defining their names in extending. It also reduces code complexity in the program.

 <?php
 namespace App\Controller;
 use App\Controller\AppController;
 class PostsController extends AppController{
  public function index(){
 }
 }
 ?> 

Now we have created helper.ctp in C:\wamp64\www\CakePHP3.8\src\Template\Posts\ folder.

 <div align = "center"><h1>Hello CakePHP Users</h1></div>
 <?= $this->fetch('content') ?> 

Similarly, we have created index.ctp in C:\wamp64\www\CakePHP3.8\src\Template\Posts\ folder

 <?php $this->extend('header'); ?>
 Here we have used extended function in header.ctp view file for calling in another view by using extend(). 

Now we will run this program in localhost by writing localhost/cakePHP3.8/posts/index.

CakePHP View Variables

Note: If you want to show any content on your web page, then you have to define it in the content block. And when a view call to extend(), execution starts from the bottom of the current view file. We can also call extend() function more than once in the view file, and then it will override the parent view that will proceed next.

$this -> extend(‘view’);
$this -> extend(‘ index’);