Convert html template into cakephp 3 template
to convert a html template into cakephp template please follow these steps* first download cakephp latest version
extract it into a folder in your xampp/htdocs/newcake
now download your html template
* create a new database. navigate to localhost/phpmyadmin and create new database with name like newcake
* open cofig/app.php and setup database connection
go to line 217 change and change three elements value username , password and database.
Example-
'Datasources' => [
'default' => [
'className' => 'Cake\Database\Connection',
'driver' => 'Cake\Database\Driver\Mysql',
'persistent' => false,
'host' => 'localhost',
'username' => 'root', //if you are on localhost then change deault username into into root
'password' => '', //left it as a blank
'database' => 'newcake', //chnage db name ...in our example our db name is newcake
'encoding' => 'utf8',
'timezone' => 'UTC',
'flags' => [],
'cacheMetadata' => true,
'log' => false,
now navigate to localhost/newcake and you will see default home page of cakephp 3.
HTML Template Implementaion in cakephp 3
open your html template folder and navigate to css folder and copy all css and paste all css into htdocs/newcake/webroot/css
now open your jd folder of your template and copy all js and paste it into htdocs/newcake/webroot/js
now open your template's index.html in your favourite text editor
navigate to htdocs/newcake/src/tempalte/element
and create two files header.php and footer.php and paste your header code in header.php and footer code in footer.php
example of index.html page of our theme or template
<!DOCTYPE html>
<html>
<head>
<title>Bootstrap Admin Theme v3</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- Bootstrap -->
<link href="bootstrap/css/bootstrap.min.css" rel="stylesheet">
<!-- styles -->
<link href="css/styles.css" rel="stylesheet">
<!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
<script src="https://oss.maxcdn.com/libs/respond.js/1.3.0/respond.min.js"></script>
<![endif]-->
</head>
<body>
<div class="header">
<div class="container">
<div class="row">
<div class="col-md-5">
<!-- Logo -->
<div class="logo">
<h1><a href="index.html">Bootstrap Admin Theme</a></h1>
</div>
</div>
<div class="col-md-5">
<div class="row">
<div class="col-lg-12">
<div class="input-group form">
<input type="text" class="form-control" placeholder="Search...">
<span class="input-group-btn">
<button class="btn btn-primary" type="button">Search</button>
</span>
</div>
</div>
</div>
</div>
<div class="col-md-2">
<div class="navbar navbar-inverse" role="banner">
<nav class="collapse navbar-collapse bs-navbar-collapse navbar-right" role="navigation">
<ul class="nav navbar-nav">
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">My Account <b class="caret"></b></a>
<ul class="dropdown-menu animated fadeInUp">
<li><a href="profile.html">Profile</a></li>
<li><a href="login.html">Logout</a></li>
</ul>
</li>
</ul>
</nav>
</div>
</div>
</div>
</div>
</div>
<div class="page-content">
<div class="row">
<div class="col-md-12">
this is content part
</div>
</div>
</div>
<footer>
<div class="container">
<div class="copy text-center">
Copyright 2014 <a href='#'>Website</a>
</div>
</div>
</footer>
<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="https://code.jquery.com/jquery.js"></script>
<!-- Include all compiled plugins (below), or include individual files as needed -->
<script src="bootstrap/js/bootstrap.min.js"></script>
<script src="js/custom.js"></script>
</body>
</html>
paste blue code in header.php and green code in footer.php
now open src/template/layout/default.ctp
we can call css and javascript using cakephp method
now load all css in head section and js at the bottom of the layout.php
example of default.ctp page after changes
<html>
<head>
<?= $this->Html->charset() ?>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>
<?= $cakeDescription ?>:
<?= $this->fetch('title') ?>
</title>
<?= $this->Html->meta('icon') ?>
<?= $this->Html->css('bootstrap.min.css') ?>
<?= $this->Html->css('styles.css') ?>
<?= $this->Html->css('mytheme.css') ?>
<?= $this->html->css('https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css') ?>
<?= $this->fetch('meta') ?>
<?= $this->fetch('css') ?>
<?= $this->fetch('script') ?>
</head>
<body>
<?= $this->element('header') ?>
<div class="page-content">
<div class="row">
<div class="col-md-12">
<?= $this->fetch('content') ?>
</div>
</div>
</div>
<?= $this->element('footer') ?>
<?= $this->Html->script('https://code.jquery.com/jquery.js') ?>
<?= $this->Html->script('bootstrap.min.js') ?>
<?= $this->Html->script('custom.js') ?>
</body>
</html>
Now open src/template/pages/home.ctp
and delete all code from it and paste your homepage code which you want to show on your homepage.
You have successfully converted html template into cakephp template.now you can check your new cakephp html template by navigate to localhost/newcake
//notes
if your js and css are not loading then read this how to solve cakephp 3 css and js not loading problem in 2 steps
Thanks for sharing such a nice information with us on PHP and related topics. Very useful lines and to the point. I appreciate your effort, keep sharing such wonderful things.
ReplyDeleteIn How many ways we can use Model in CakePHP