[Osaka/Yokohama/Tokushima] Looking for infrastructure/server side engineers!

[Osaka/Yokohama/Tokushima] Looking for infrastructure/server side engineers!

[Deployed by over 500 companies] AWS construction, operation, maintenance, and monitoring services

[Deployed by over 500 companies] AWS construction, operation, maintenance, and monitoring services

[Successor to CentOS] AlmaLinux OS server construction/migration service

[Successor to CentOS] AlmaLinux OS server construction/migration service

[For WordPress only] Cloud server “Web Speed”

[For WordPress only] Cloud server “Web Speed”

[Cheap] Website security automatic diagnosis “Quick Scanner”

[Cheap] Website security automatic diagnosis “Quick Scanner”

[Reservation system development] EDISONE customization development service

[Reservation system development] EDISONE customization development service

[Registration of 100 URLs is 0 yen] Website monitoring service “Appmill”

[Registration of 100 URLs is 0 yen] Website monitoring service “Appmill”

[Compatible with over 200 countries] Global eSIM “Beyond SIM”

[Compatible with over 200 countries] Global eSIM “Beyond SIM”

[If you are traveling, business trip, or stationed in China] Chinese SIM service “Choco SIM”

[If you are traveling, business trip, or stationed in China] Chinese SIM service “Choco SIM”

[Global exclusive service] Beyond's MSP in North America and China

[Global exclusive service] Beyond's MSP in North America and China

[YouTube] Beyond official channel “Biyomaru Channel”

[YouTube] Beyond official channel “Biyomaru Channel”

How to reuse common parts in Views with CakePHP

Hello.
This is Hase from the development team.

I often develop with CakePHP, and
when writing views, I have always
written common parts such as the head, header, and footer into individual files.

However, if you write it like this, if you need to modify it,
you will have to modify each file one by one, which is very difficult and troublesome. .

Based on this, I thought there was a way to group together the common parts, and after doing some research,
I found a method and would like to introduce it to you.

method

1. Create a View file with a common layout under app/View/Elements/

First, create a common layout under app/View/Elements/.
The file name is {arbitrary file name}.ctp, same as normal View.

This time let's use head.ctp, header.ctp, footer.ctp.
Just copy and paste the head, header, and footer parts written in the normal View.

By the way, it is also possible to create a directory under app/View/Elements/ and place the View file under it.
Example: app/View/Elements/Admin/header.ctp etc.

2. Load the file created earlier in normal View

to the part you want to read

// common head<?php echo $this-> element('head'); ?> // common header<?php echo $this-> element('header'); ?> // common footer<?php echo $this-> element('head'); ?>

If you write this, the file you created earlier will be automatically loaded.

Also, if you have created a directory like app/View/Elements/Admin/

// common head<?php echo $this-> element('Admin/head'); ?> // Common header<?php echo $this-> element('Admin/header'); ?> // common footer<?php echo $this-> element('Admin/head'); ?>

It is OK if you write .

Problem

I think there are many things that are different even though they have something in common.
The most common is the title tag within the head.
The content of the title tag element often varies depending on the page.

We can handle these cases as well.


You can pass parameters by using an associative array as the second argument of $this->element() and using ["variable name" => "value"]

Usage example:

// element side (head.ctp)<head> ......<title> <?php echo $title; ?></title></head>
// Normally View side (caller)<?php echo $this-> element('head', ["title" => "top"]); ?>

It's very convenient because you can call it like this.

That's it.

If you found this article helpful , please give it a like!
3
Loading...
3 votes, average: 1.00 / 13
8,388
X facebook Hatena Bookmark pocket
[2025.6.30 Amazon Linux 2 support ended] Amazon Linux server migration solution

[2025.6.30 Amazon Linux 2 support ended] Amazon Linux server migration solution

The person who wrote this article

About the author

Tatsuya Hase

Joined Beyond Co., Ltd. as a new graduate.

We develop web systems (development of services and systems that run on browsers, such as web services, digital content, and business management systems) and game APIs (development of programs that communicate with application games).

We also develop private/custom apps for Shopify.

Originally worked at the Osaka office, but transferred to the Yokohama office in 2019.
His hobbies are baseball/karaoke/anime