Welcome Guest, Not a member yet? Register   Sign In
HTML parser/templater?
#1

[eluser]kimo_gusatava[/eluser]
I just want to post/share this simple HTML parser/templater? I made
Not much but here we go...
Please take note of the <s/cript (remove the / )

Code:
&lt;?php
/**
* Description of templater
*
* @author jatazoulja
*/
class MY_tpl_helper {

    public $header;
    public $footer;
    public $nav;
    public $CI;
    public $config;
    public $title;
    public $meta;
    public $js;
    public $css;

    function __construct() {
        $this->CI = & get_instance();
        $this->init();
        $this->CI->load->helper('html');
    }

    function init() {
        $this->config = parse_ini_file(APPPATH . 'config/htmlTemplate.ini', TRUE);
        return $this;
    }

    function title($prefix='', $suffix='') {

        if (!isset($this->config['siteInfo']['title'])) {
            return $this->title = FALSE;
        } else {
            return $this->title = '&lt;title&gt;' . $prefix . '' . $this->config['siteInfo']['title'] . '' . $suffix . '&lt;/title&gt;';
        }
    }

    function meta($name='', $desc='') {
        if (!isset($this->config['meta'])) {
            return $this->meta = FALSE;
        } else {
            $meta = $this->config['meta'];
            $arMeta = array();
            foreach ($meta as $k => $v) {
                for ($i = 0; $i < sizeof($v); $i++) {
                    $arMeta[$i][$k] = $v[$i];
                }
            }
            return $this->meta = meta($arMeta);
        }
    }

    function js($src='') {
        $this->js = "\n&lt;!-- Js Scripts STARTS  --&gt;\n\n";
        $this->js .= "<s/cript>";
        $this->js .= "var base_url = '".base_url()."'";
        $this->js .= "[removed]\n";
        if (!isset($this->config['script']) AND $src == '') {
            $this->js .= "\n&lt;!-- Please edit the ini file to include stylesheets  --&gt;\n\n";
        } else {
            $script = $this->config['script']['src'];
            $arJs = array();
            $basePath = base_url() . '' . $this->config['script']['folder'];
            for ($i = 0; $i < sizeof($script); $i++) {
                $this->js .= "<s/cript type='text/javascript' src='{$basePath}{$script[$i]}'>[removed]\n";
            }
            if ($src != '') {
                $this->js .= "<s/cript type='text/javascript' src='{$basePath}$src'>[removed]\n";
            }
        }
        
        return $this->js .= "\n&lt;!-- Js Scripts ENDS --&gt;\n\n";
    }

    function css($src='', $rel2='stylesheet') {
        $this->css = "\n&lt;!-- link include starts --&gt;\n\n";
        if (!isset($this->config['css']) AND $src == '') {
            return $this->css .= "&lt;!-- Please edit the ini file to include stylesheets */ \n";
        } else {
            $basePath = base_url();
            foreach ($this->config['css'] as $key => $value) {
                $rel = "rel='$key'";
                foreach ($value as $k) {
                    $this->css .= "&lt;link $rel href='{$basePath}$k' /&gt;\n";
                }
            }
            if ($src != '') {
                $this->css .= "&lt;link rel='$rel2' href='$src' /&gt;\n";
            }
        }
        return $this->css .= "\n&lt;!-- link include Ends --&gt;\n\n";
    }

    function header() {

        $head = '&lt;head&gt;';

        if (!$this->meta) {
            $this->meta();
        }
        if (!$this->title) {
            $this->title();
        }
        if (!$this->js) {
            $this->js();
        }
        $head .= $this->meta;
        $head .= $this->title;
        $head .= $this->css;
        $head .= $this->js;
        $head .= '&lt;/head&gt;';
        return $this->header = $head;
    }

    function html($tpl='',$data = '') {
        //echo doctype('html5');
        echo '&lt;html&gt;';
        echo $this->header();
        if(is_array($tpl)) {
            foreach($tpl as $k) {
                echo $this->CI->load->view($k, $data, TRUE);
            }
        } else {
            echo $this->CI->load->view($tpl, $data, TRUE);
        }
        echo '&lt;/html&gt;';
    }

}

How to use:
Code:
$this->my_tpl_helper->css('css/test.css');
        $this->my_tpl_helper->js('exportAll.js');
        $tpl = "secure/index";
        $data['tester'] = "null";
        $this->my_tpl_helper->html($tpl, $data);

Config file (located at the config directory) htmlTemplate.ini
Code:
[meta]
name[] = "X-UA-Compatible"
name[] = "Keyword"
name[] = "robots"
name[] = "viewport"
content[] = "IE=9,chrome=1"
content[] = "love, passion, intrigue, deception"
content[] = "no-cache"
content[] = "width=device-width, initial-scale=1.0"

[script]
folder = "js/"
src[] = "jquery-1.5.1.min.js"
src[] = "jquery-ui-1.8.12.custom.min.js"
src[] = "jquery.dataTables.js"
src[] = "plugins.js"
src[] = "script.js"

[css]
icon[] = "favicon.ico"
apple-touch-icon[] = "apple-touch-icon.png"
stylesheet[] = "css/black-tie/jquery-ui-1.8.12.custom.css"
stylesheet[] = "css/demo_page.css"
stylesheet[] = "css/demo_table.css"
stylesheet[] = "css/style.css"
stylesheet[] = "css/handheld.css"


[siteInfo]
title = 'CompanyX:Fix Leads'
compName = 'CompanyX'

cheers!




Theme © iAndrew 2016 - Forum software by © MyBB