Welcome Guest, Not a member yet? Register   Sign In
My template library
#1

I'm beginner in codeigniter 4. Recently i created this Template Library for my project. If someone have suggestions please put here.

I inspired from this project: https://github.com/lonnieezell/myth-forums


app/Libraries/TemplateLibrary.php

PHP Code:
<?php namespace App\Libraries;
use 
CodeIgniter\View\View;
use 
Config\Services;

class 
TemplateLibrary {

    private 
$renderer;
    
    private 
$template;
    
    public function 
__construct() {
        
        
$this->template = [];
        
        
/* Theme */
        
        
$this->template['theme'] = 'default';
        
        
/* Headline */
        
        
$this->template['headline'] = '';
        
        
/* Breadcrumb */
        
        
$this->template['breadcrumb'] = '';
        
        
/* Canonical */
        
        
$this->template['canonical'] = '';
        
        
/* Meta */
        
        
$this->template['meta'] = [
            
'title' => '',
            
'description' => '',
            
'keywords' => '',
            
'robots' => 'INDEX, FOLLOW'
        
];
        
        
/* Css files */
        
        
$this->template['css_files'] = [];
        
        
/* Js files */
        
        
$this->template['js_files'] = [];
    
    }
    
    public function 
setTheme(string $theme) {
        
        
$this->template['theme'] = $theme;
        
    }
    
    public function 
setHeadline(string $headline) {
        
        
$this->template['headline'] = $headline;
        
    }
    
    public function 
setBreadcrumb(array $breadcrumb) {
        
        
$this->template['breadcrumb'] = $breadcrumb;
        
    }
    
    public function 
setCanonical(string $canonical) {
        
        
$this->template['canonical'] = $canonical;
        
    }
    
    public function 
setMeta(string $key$value) {
        
        if ( 
array_key_exists($key$this->template['meta']) ) {
            
            
$this->template['meta'][$key] = (string) $value;
            
        }
        
    }
    
    public function 
addCssFile(string $keystring $pathint $priority) {
        
        if ( 
strpos($path'https://') === false ) {
            
            
$path href('themes/' strtolower($this->template['theme']) . '/' $path);
            
        }
        
        
$this->template['css_files'][$key]['path'] = $path;
        
$this->template['css_files'][$key]['priority'] = $priority;
        
        
usort($this->template['css_files'], function($a$b) {
            return 
$a['priority'] - $b['priority'];
        });
        
    }
    
    public function 
addJsFile(string $keystring $pathint $priority) {
    
        if ( 
strpos($path'https://') === false ) {
            
            
$path href('themes/' strtolower($this->template['theme']) . '/' $path);
            
        }
        
        
$this->template['js_files'][$key]['path'] = $path;
        
$this->template['js_files'][$key]['priority'] = $priority;
        
        
usort($this->template['js_files'], function($a$b) {
            return 
$a['priority'] - $b['priority'];
        });
        
    }
    
    public function 
getTemplate() {
        
        return 
$this->template;
        
    }
    
    public function 
getTheme() {
        
        return 
$this->template['theme'];
        
    }
    
    public function 
getHeadline() {
        
        return 
$this->template['headline'];
        
    }
    
    public function 
getBreadcrumb() {
        
        return 
$this->template['breadcrumb'];
        
    }
    
    public function 
getCanonical() {
        
        return 
$this->template['canonical'];
        
    }
    
    public function 
getMeta(string $key) {
        
        if ( 
array_key_exists($key$this->template['meta']) ) {
            
            return 
$this->template['meta'][$key];
            
        } return 
NULL;
        
    }
    
    public function 
getCssFiles() {
        
        return 
$this->template['css_files'];
        
    }
    
    public function 
getJsFiles() {
        
        return 
$this->template['js_files'];
        
        
    }
    
    
/*
    public function renderView(string $name, array $data = [], array $options = []) {
        
        return view('MyApp\Views\\' . ucfirst($this->template['theme']) . '\\' . $name, $data, $options);
        
    }
    
    public function render(string $name, array $data = [], array $options = []) {

        $data['template'] = $this;
        
        return $this->renderView($name, $data, ['saveData' => true]);
        
    }
    */
    
    
private function getRenderer() {
        
        
if ( $this->renderer !== NULL ) {
            
            return $this->renderer;
            
        }

        $path ROOTPATH 'myapp/Views/' ucfirst($this->template['theme']);

        $this->renderer Services::renderer($pathnullfalse);

        return $this->renderer;
        
    }
    
    public function 
renderView(string $name, array $data = [], array $options = []) {
    
        
$renderer $this->getRenderer();

        
$saveData null;
        if (
array_key_exists('saveData'$options)) {
            
$saveData = (bool) $options['saveData'];
            unset(
$options['saveData']);
        }

        return 
$renderer->setData($data'raw')
                        ->
render($name$options$saveData);
                        
    }
    
    public function 
render(string $name, array $data = []) {
        
        $data
['template'] = $this->template;

        return $this->renderView($name$data, ['saveData' => true]);
        
    }
    

Reply
#2

I think, almost everyone passed through this stage - to create a library for templating with useful routines. No problem with that.

When you get annoyed at PHP-templating ( sooner or later this will happen :-) ) I would propose seeking for integration of a good template engine. https://github.com/ivantcholakov/starter...s/Twig.php
Reply




Theme © iAndrew 2016 - Forum software by © MyBB