Welcome Guest, Not a member yet? Register   Sign In
Simple Template library 1.0 + Simple Asset helper 1.0
#1

[eluser]lizdeika[/eluser]
I did try Template Library 1.4 but it was overkill for me because i prefer simple solutions Smile And i didn't like it generating script ant style tags. I think that belongs to HTML and not PHP.
I use ME 4.2 and it's modules::run for template parts, so i do not know if this code is usefull without ME. If you do not use ME, maybe it would be better to use Template Library 1.4.

So here we go:
Code:
application/libraries/tpl.php:

<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

class Tpl {
    var $ci;
    var $masterTemplate;
    
    function Tpl() {
        //$this->ci = &get;_instance();
        $this->ci = &modules;_instance();
        
        $this->ci->config->load('tpl');
        $this->masterTemplate = $this->ci->config->item('master_template', 'tpl');
    }
    
    function setMasterTemplate($masterTemplate) {
        $this->masterTemplate = $masterTemplate;
    }
    
    function fetch($template, $data = array()) {
        return $this->ci->load->view($template, $data, TRUE);
    }
    
    function view($template, $data = array()) {
        $this->ci->load->view($template, $data);
    }
    
    function render($template, $data = array(), $return = FALSE) {
        $master = array();
        $master['content'] = $this->ci->load->view($template, $data, TRUE);
        $this->ci->load->view($this->masterTemplate, $master, $return);
    }
}

/* End of file Tpl.php */
/* Location: ./system/application/libraries/Tpl.php */
Code:
application/config/tpl.php:

<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

$config['tpl']['master_template'] = 'layout';

/* End of file tpl.php */
/* Location: ./system/application/config/tpl.php */

Code:
application/helpers/assets_helper.php:

<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

function asset($assetFile) {
    $ci = &get;_instance();
    
    $assetVersion = $ci->config->item($assetFile, 'assets');
    $assetVersion = ($assetVersion == '') ? '' : '?' . $assetVersion;
    
    return $ci->config->item('server', 'assets') . $assetFile . $assetVersion;
}
?>

Code:
application/config/assets.php:

<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

$config['assets']['server'] = 'http://localhost/ci/assets/';


// JS
$config['assets']['shared/js/jquery.js'] = '126';

// CSS
$config['assets']['shared/css/style.css'] = '1';


/* End of file assets.php */
/* Location: ./system/application/config/assets.php */

$config['assets']['server'] lets you move your static content to other server if needed.
I did not use things like $config['assets']['js']['file'] or $config['assets']['css']['file'] because that is a limitation. Then we would need $config['assets']['swf'], $config['assets']['jpg'] and so on... What we really need is the version because js, css and images are cached in user's browser, so if we upload new version we change the version and we are sure user gets that new one. I think Safari has problems with those versions. Anyone with a solution is welcome Smile

So here is full example with layout and assets.

Code:
application/view/layout.php

<html>
<head>
    <base href="<?php echo base_url(); ?>" />
    <title></title>
    <style type="text/css">
        @import url("<?php echo asset('shared/css/style.css'); ?>");
    </style>
    script type="text/javascript" src="<?php echo asset('shared/js/jquery.js'); ?>" /script
</head>
<body>

    <div id="header">&lt;?php echo Modules::run('layout', '', 'header'); ?&gt;</div>
  
    <div id="content">&lt;?php echo isset($content) ? $content : ''; ?&gt;</div>
    
    <div id="footer">&lt;?php echo Modules::run('layout', '', 'footer'); ?&gt;</div>

&lt;/body&gt;
&lt;/html&gt;

Head'er will look like this:
Code:
&lt;head&gt;
    &lt;base href="http://localhost/ci/" /&gt;
    &lt;title&gt;System&lt;/title&gt;
    &lt;style type="text/css"&gt;
        @import url("http://localhost/ci/assets/shared/css/style.css?1");
    &lt;/style&gt;
    script type="text/javascript" src="http://localhost/ci/assets/shared/js/jquery.js?126" /script
&lt;/head&gt;

Sorry that script tags look like this, this forum is removing proper code for some reason Smile

Now module's controller sample:
Code:
modules/users/controllers/users.php

&lt;?php

class Users extends Controller {

    function Users() {
        parent::Controller();
    }

    function list() {
        <...> // code to get the list
        $this->tpl->render('list', $userList);
    }

?&gt;

It's up to you how you setup and use my code. Suggestions are welcome. Keep it simple.




Theme © iAndrew 2016 - Forum software by © MyBB