Skip to content

Commit 1de0f98

Browse files
committed
add template parser library
1 parent 654132f commit 1de0f98

File tree

12 files changed

+250
-9
lines changed

12 files changed

+250
-9
lines changed

application/controllers/Test.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
defined('BASEPATH') OR exit('No direct script access allowed');
3+
4+
class Test extends CI_Controller {
5+
6+
public function index()
7+
{
8+
$this->load->library('template');
9+
$this->load->helper('html5');
10+
11+
$data['name'] = array('Arya', 'Putra', 'Sadewa');
12+
13+
$this->template->view('test', $data);
14+
}
15+
}

application/helpers/extended_helper.php

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

44
if ( ! function_exists('csrf_field'))
55
{
6-
function csrf_field() {
6+
function csrf_field()
7+
{
78
echo form_hidden($this->security->get_csrf_token_name(), $this->security->get_csrf_hash());
89
}
910
}
1011

1112
if ( ! function_exists('nanoid'))
1213
{
13-
function nanoid($size = 21) {
14+
function nanoid($size = 21)
15+
{
1416
$randomizer = new \Random\Randomizer();
1517

1618
return $randomizer->getBytesFromString('useandom-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwyzrict', $size);
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
defined('BASEPATH') OR exit('No direct script access allowed');
3+
4+
if ( ! function_exists('html5'))
5+
{
6+
function html5($tag, $attributes = array(), $children = NULL)
7+
{
8+
if ($tag === 'doctype')
9+
{
10+
return '<!doctype html>';
11+
}
12+
13+
$attribute = '';
14+
15+
foreach ($attributes as $name => $value)
16+
{
17+
switch ($name)
18+
{
19+
case 'class':
20+
if (is_array($value))
21+
{
22+
$value = implode(' ', $value);
23+
}
24+
break;
25+
26+
case 'style':
27+
if (is_array($value))
28+
{
29+
$style = $value;
30+
$value = '';
31+
32+
foreach ($style as $n => $v)
33+
{
34+
$value .= "{$n}:{$v};";
35+
}
36+
}
37+
break;
38+
39+
default:
40+
break;
41+
}
42+
$name = strtolower($name);
43+
$attribute .= " {$name}=\"{$value}\"";
44+
}
45+
46+
if ($children === NULL)
47+
{
48+
return "<{$tag}{$attribute} />";
49+
}
50+
else
51+
{
52+
if (is_array($children) === FALSE)
53+
{
54+
$child = $children;
55+
}
56+
else
57+
{
58+
$child = implode('', $children);
59+
}
60+
61+
return "<{$tag}{$attribute}>{$child}</{$tag}>";
62+
}
63+
}
64+
}

application/libraries/Layout.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ public function __construct()
1414
$this->CI =& get_instance();
1515
}
1616

17-
public function view($view, $vars = array(), $return = FALSE)
17+
public function view($view, $vars = array())
1818
{
19-
$this->CI->load->view($view);
20-
$this->CI->load->view($this->layout, $vars, $return);
19+
$this->CI->load->view($view, $vars);
20+
$this->CI->load->view($this->layout);
2121
}
2222

2323
public function extend($layout)
@@ -51,8 +51,8 @@ public function render_section($section_name)
5151
}
5252
}
5353

54-
public function include($view, $vars = array(), $return = FALSE)
54+
public function include($view, $vars = array())
5555
{
56-
$this->CI->load->view($view, $vars, $return);
56+
$this->CI->load->view($view, $vars);
5757
}
5858
}

application/libraries/Template.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
defined('BASEPATH') OR exit('No direct script access allowed');
3+
4+
class Template {
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())
18+
{
19+
$this->CI->load->view($view, $vars);
20+
$this->CI->load->view($this->layout);
21+
}
22+
23+
public function extend($layout)
24+
{
25+
$this->layout = $layout;
26+
}
27+
28+
public function section($name, $contents)
29+
{
30+
if (is_array($contents) === FALSE)
31+
{
32+
$content = $contents;
33+
}
34+
else
35+
{
36+
$content = implode('', $contents);
37+
}
38+
39+
$this->current_section = $name;
40+
$this->sections[$this->current_section] = $content;
41+
}
42+
43+
public function render_section($section_name)
44+
{
45+
if (isset($this->sections[$section_name]) === FALSE)
46+
{
47+
return '';
48+
}
49+
else
50+
{
51+
return $this->sections[$section_name];
52+
}
53+
}
54+
55+
public function include($view, $vars = array())
56+
{
57+
return $this->CI->load->view($view, $vars, TRUE);
58+
}
59+
}

application/templates/Helper.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
<?php
22
defined('BASEPATH') OR exit('No direct script access allowed');
33

4-
if ( ! function_exists(''))
4+
if ( ! function_exists('example'))
55
{
6-
function example() {
6+
function example()
7+
{
78
return 'example';
89
}
910
}

application/views/layout/test.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?= html5('doctype') ?>
2+
<?=
3+
html5(
4+
'html',
5+
array(
6+
'lang' => 'id'
7+
),
8+
array(
9+
html5(
10+
'head', array(),
11+
array(
12+
html5(
13+
'meta',
14+
array(
15+
'charset' => 'utf-8'
16+
)
17+
),
18+
html5(
19+
'meta',
20+
array(
21+
'content' => 'initial-scale=1.0, width=device-width',
22+
'name' => 'viewport'
23+
)
24+
),
25+
html5(
26+
'meta',
27+
array(
28+
'content' => 'ie=edge',
29+
'http-equiv' => 'X-UA-Compatible'
30+
)
31+
),
32+
html5('title', array(), 'Template Test'),
33+
html5(
34+
'link',
35+
array(
36+
'href' => 'https://cdn.jsdelivr.net/npm/bulma/css/bulma.min.css',
37+
'rel' => 'stylesheet'
38+
)
39+
)
40+
)
41+
),
42+
html5(
43+
'body',
44+
array(
45+
'class' => 'content',
46+
'style' => array(
47+
'margin' => '1rem'
48+
)
49+
),
50+
$this->template->render_section('content')
51+
)
52+
)
53+
)
54+
?>

application/views/picsum.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?=
2+
html5(
3+
'img',
4+
array(
5+
'alt' => 'Lorem Picsum',
6+
'loading' => 'lazy',
7+
'src' => 'https://picsum.photos/seed/picsum/1280/720',
8+
'width' => 480
9+
)
10+
)
11+
?>

application/views/templates/begin.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,6 @@
1313

1414
<link href="https://cdn.jsdelivr.net/npm/bootstrap/dist/css/bootstrap.min.css" rel="stylesheet" />
1515
<link href="https://cdn.jsdelivr.net/npm/bootstrap-icons/font/bootstrap-icons.min.css" rel="stylesheet" />
16+
<link href="<?= base_url(array('assets', 'css', 'style.css')) ?>" rel="stylesheet" />
1617
</head>
1718
<body class="d-flex flex-column min-dvh-100">
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<script src="https://cdn.jsdelivr.net/npm/bootstrap/dist/js/bootstrap.bundle.min.js"></script>
22
<script src="https://cdn.jsdelivr.net/npm/jquery/dist/jquery.min.js"></script>
3+
<script src="<?= base_url(array('assets', 'js', 'app.js')) ?>"></script>
34
</body>
45
</html>

0 commit comments

Comments
 (0)