home-icon
Adding data to your views
Last updated: Feb 02, 2024

Adding data to your views

It can be very usefull to be able to add extra data into your view files.

Webigniter provides the option to add custom data to layout and section files. This data is easily passed to the sections or layouts.

Adding data

By default, Webigniter handles only the pages that are not defined elsewhere. But this doesn't mean you can't use Webigniter in your own pages.

In this example we are going to add a custom message to the view file. We will keep this example simple  so you can apply it to your situation.

Start by creating a page in your own environment, for example /message. In this file you create the code which handles your custom message, like this.

<?php
if(date('a') === 'am'){
   $message = 'Good morning';
}
else {
   $message = 'Good afternoon';
}
?>

After that, you create a page with the same URL (/message) in the CMS. You can add as many sections to this page as you want, they will able to use the custom message, and so does the layout file.

After the page in the CMS is created, we need to tell our custom page to use the content from the CMS, but with the data we created earlier. You can do this by adding a data array to the Webigniter client. Like this.

<?php
if(date('a') === 'am'){
   $message = 'Good morning';
}
else {
   $message = 'Good afternoon';
}

$extraData['message'] = $message;

$webigniter = new WebigniterClient('YOUR_LICENSE_KEY', $extraData);
$webigniter->getLayoutFile();
?>

In your view files you can now use the $message variable and it will display your custom message. Use <?=$message;?> to display the message. Instead of a string, you can pass any kind of data to the view files, arrays, booleans, objects, etc.