CakePHP 4 How To Render a Specific Template Tutorial

Share this Article
Reading Time: 4 minutes
1,634 Views

Inside this article we will see the concept i.e CakePHP 4 How to render specific template. CakePHP 4 have few rules by the help of which it can run a template file, execute layout and even load view variables.

Article contains classified information about rendering a user defined template file for cakephp 4 views. CakePHP provides render() method where we can set custom template to run application user interfaces.

Settings up the custom template works exact the same as default template system works with. Custom template will be created inside /templates/<Controller Class> folder.

Learn More –

Let’s get started.

CakePHP 4 Installation

To create a CakePHP project, run this command into your shell or terminal. Make sure composer should be installed in your system.

$ composer create-project --prefer-dist cakephp/app:~4.0 mycakephp

Above command will creates a project with the name called mycakephp.

CakePHP 4 Template Rule

When we create any controller and It’s methods. Then by default application understands that it have templates inside it’s associated folder.

If in case we don’t have any template files then we need to set autoRender to false.

Let’s assume we have a controller SiteController.php inside /src/Controller folder. Then according to default behaviour of CakePHP 4, we need to create Site folder inside /templates folder.

Why Site folder? It’s because Site is the name of controller class.

<?php

declare(strict_types=1);

namespace App\Controller;

class SiteController extends AppController
{
    public function aboutUs()
    {
        //...
    }
    
    public function myProducts()
    {
        //...
    }
}

We create template files inside /templates/Site folder for –

  • about_us.php for aboutUs() method
  • my_products.php for myProducts() method

How To Set a Specific Template

To override or use a different template for views then we prefer render() method. Inside this method we need to pass the name of the template file.

<?php

declare(strict_types=1);

namespace App\Controller;

class SiteController extends AppController
{
    public function aboutUs()
    {
        //...
        $this->set("name", "Sanjay Kumar");
        $this->render("custom_template"); 
    }
    
    public function myProducts()
    {
        //...
    }
}

Here,

Instead of finding the default template about_us.php, it will now look for custom_template.php inside /templates/Site folder.

Also, in the template file custom_template.php we can access the view variable value i.e name in the same way.

<?php echo $name; ?> OR <?= $name ?>

We hope this article helped you to learn about CakePHP 4 How To Render a Specific Template Tutorial in a very detailed way.

Buy Me a Coffee

Online Web Tutor invites you to try Skillshare free for 1 month! Learn CakePHP 4, Laravel APIs Development, CodeIgniter 4, Node Js, etc into a depth level. Master the Coding Skills to Become an Expert in Web Development. So, Search your favourite course and enroll now. Click here to join.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.

Was this post helpful?

Learn Web Development Courses Risk Free @ $5 only.
Web Development Courses @ $5