CakePHP View Variables

Facebooktwitterredditpinterestlinkedinmailby feather

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:

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:

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.

Now we will create fun3.ctp file

Output:

view variables

Alternate use of Echo function

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

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

Using Short Tag support:

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

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 “$”.

And you have to write in action.ctp file:

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:

Another example, using if/elseif/else.

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.

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

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

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.

Facebooktwitterredditpinterestlinkedinmailby feather