To help generating views, CodeCube uses PHP Template Inheritance plugin, as well as various custom methods.
As explained in directory structure, all the files necessary to generate views are stored in resources directory. There, you can store css, js, images and various asset files in resources/assets directory, layouts and class-names for various PHP tags for the views that will be automatically generated by the system in resources/xml directory, and necessary PHP view files in resources/views directory.
Defining Layouts
To define a layout, you can create a layout.php file, and use startblock() & endblock() methods to define each section for the layout like below-
<html>
<head>
<title><?php startblock('title') ?><?php endblock() ?></title>
</head>
<body>
<?php startblock('content') ?> <?php endblock() ?>
</body>
</html>
Extending a Layout
Once defined you can extend the layout file in home.php file like below-
<?php inherits(‘layout’); ?>
<?php startblock('title') ?>Home<?php endblock() ?>
<?php startblock('content') ?>This is my content!<?php endblock() ?>
Above, the inherits() method is used to call the parent view the child view is extending form.
Including other views & assets
To include other views, use the append() method like below-
append(‘front.leftbar', [‘items’ => $items]);
Above, views/front/leftbar.php file is included in the home.php file and items array is passed to it.
To include css files, use the style() method like below-
echo style('style.css');
To include js files, use the script() method like below-
echo script('script.js');
To show the title of the application, use the title() method. To show any other text other then title, pass the text as parameter-
echo title(‘My Title’);
It will show the generated title using the text along with project name given in the APP_NAME constant, like this-
My Title || Project Name
To get the source of a file uploaded in the directory defined in the upload key in config/app.php file in a view, use the asset() method like below-
echo asset(‘document.pdf’);
To show image in view, use the image() method like below-
echo image(‘my_image.jpg’);
To show an image’s thumbnail in view that was saved during upload, pass the thumbnail extension text as an argument to the image() method like below-
echo image(‘my_image.jpg’, 'alt', [], '_thumb');
The framework will automatically detect whether the image exists, If not it will use the default image from https://placeholder.com/ as placeholder.
To show the default icon of the website, use the icon() method like below-
echo icon('favicon.ico');
Working with Routes & Urls
CodeCube provides some default functions for working with routes and URLs in views.
To show a route link in a view, use the route() method. The route must be defined in the routes/web.php file as a route key, otherwise the framework will report error.
echo route('signup');
You can add additional parameters to the route as an associative array like below-
echo route('items/show', ['id' => $item['id']]);
You can add $_GET parameters of the current URL to a route using urlStr() method-
echo urlStr('items/show', ['id' => $item['id']]);
To get the route from the current URL, use get_route() method. For URL your-site-url/home, this method will extract the route /home-
echo get_route();
To check whether the current URL is a specific route, use the route_is() method.
echo route_is('home') ? 'active' : ‘’;
In the above code, the framework checks whether the current route is/home and if it is, shows active.
