Skip to content

Commit 654132f

Browse files
committed
add view layout library and refactoring templates cli tools
1 parent 1b8e5d7 commit 654132f

File tree

11 files changed

+100
-9
lines changed

11 files changed

+100
-9
lines changed

application/controllers/Home.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
defined('BASEPATH') OR exit('No direct script access allowed');
3+
4+
class Home extends CI_Controller {
5+
6+
public function index()
7+
{
8+
$this->load->library('layout');
9+
10+
$this->layout->view('home');
11+
}
12+
}

application/libraries/Layout.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
defined('BASEPATH') OR exit('No direct script access allowed');
3+
4+
class Layout {
5+
6+
protected $CI;
7+
8+
protected $layout;
9+
protected $current_section;
10+
protected $sections = array();
11+
12+
public function __construct()
13+
{
14+
$this->CI =& get_instance();
15+
}
16+
17+
public function view($view, $vars = array(), $return = FALSE)
18+
{
19+
$this->CI->load->view($view);
20+
$this->CI->load->view($this->layout, $vars, $return);
21+
}
22+
23+
public function extend($layout)
24+
{
25+
$this->layout = $layout;
26+
}
27+
28+
public function section($name)
29+
{
30+
$this->current_section = $name;
31+
32+
ob_start();
33+
}
34+
35+
public function end_section()
36+
{
37+
$content = ob_get_clean();
38+
39+
$this->sections[$this->current_section] = $content;
40+
}
41+
42+
public function render_section($section_name)
43+
{
44+
if (isset($this->sections[$section_name]) === FALSE)
45+
{
46+
echo '';
47+
}
48+
else
49+
{
50+
echo $this->sections[$section_name];
51+
}
52+
}
53+
54+
public function include($view, $vars = array(), $return = FALSE)
55+
{
56+
$this->CI->load->view($view, $vars, $return);
57+
}
58+
}

application/templates/Config.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
<?php
22
defined('BASEPATH') OR exit('No direct script access allowed');
33

4-
$config[''] = ;
4+
$config['example'] = 'example';

application/templates/Controller.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ class <controller> extends CI_Controller {
55

66
public function index()
77
{
8-
8+
return 'Hello World!';
99
}
1010
}

application/templates/Helper.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
if ( ! function_exists(''))
55
{
6-
function () {
7-
6+
function example() {
7+
return 'example';
88
}
99
}

application/templates/Hook.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ public function __construct()
1010
$this->CI =& get_instance();
1111
}
1212

13-
public function ($params = NULL)
13+
public function example($params = NULL)
1414
{
15-
15+
return 'example';
1616
}
1717
}

application/templates/Language.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
<?php
22
defined('BASEPATH') OR exit('No direct script access allowed');
33

4-
$lang['<language>_'] = '';
4+
$lang['<language>_example'] = 'example';

application/templates/Library.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ public function __construct($params = NULL)
1010
$this->CI =& get_instance();
1111
}
1212

13-
public function ()
13+
public function example()
1414
{
15-
15+
return 'example';
1616
}
1717
}

application/views/home.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php $this->layout->extend('layout/template') ?>
2+
3+
<?php $this->layout->section('content') ?>
4+
<?php $this->layout->include('image') ?>
5+
6+
<h1>Hello World!</h1>
7+
<p>Lorem ipsum dolor sit, amet consectetur adipisicing elit. Impedit repellendus consectetur magnam, doloremque vel atque perferendis distinctio voluptatem nulla facilis iusto, assumenda officiis vero praesentium sapiente quis dignissimos tenetur, est.</p>
8+
<?php $this->layout->end_section() ?>

application/views/image.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<img alt="Lorem Picsum" loading="lazy" src="https://picsum.photos/seed/picsum/1280/720" width="480" />

0 commit comments

Comments
 (0)