source: branches/0.4/web/addons/job_monarch/dwoo/Dwoo/Adapters/CodeIgniter/libraries/Dwootemplate.php @ 755

Last change on this file since 755 was 755, checked in by ramonb, 11 years ago
  • add Dwoo
File size: 4.6 KB
Line 
1<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
2
3require "dwoo/dwooAutoload.php";
4
5/**
6 * Creates an template interface from Codeigniter to DWOO.
7 *
8 * This software is provided 'as-is', without any express or implied warranty.
9 * In no event will the authors be held liable for any damages arising from the use of this software.
10 *
11 *
12 * @author     Stefan Verstege <stefan.verstege@newmedia.nl>
13 * @copyright  Copyright (c) 2008, Stefan Verstege
14 * @license    http://dwoo.org/LICENSE   Modified BSD License
15 * @link       http://www.newmedia.nl/
16 * @version    1.1.0
17 * @date       2009-07-18
18 * @package    Dwoo
19 *
20 * @uses the dwoo package from http://dwoo.org
21 */
22class Dwootemplate extends Dwoo_Core {
23    protected $dwoo_data = array();
24
25    /**
26     * Constructor for the DwooTemplate engine
27     *
28     */
29    public function __construct() {
30        // Call parents constructor
31        parent::__construct();
32
33        // Set the config settings
34        $this->initialize();
35
36        // Assign some defaults to dwoo
37        $CI                         = get_instance();
38        $this->dwoo_data            = new Dwoo_Data();
39        $this->dwoo_data->js_files  = array();
40        $this->dwoo_data->css_files = array();
41        $this->dwoo_data->CI        = $CI;
42        $this->dwoo_data->site_url  = $CI->config->site_url(); // so we can get the full path to CI easily
43        $this->dwoo_data->uniqid    = uniqid();
44        $this->dwoo_data->timestamp = mktime();
45
46        log_message('debug', "Dwoo Template Class Initialized");
47    }
48
49
50    /**
51     * Assign data to dwoo data object
52     *
53     * @param string $key
54     * @param mixed $value
55     */
56    public function assign($key, $value) {
57        $this->dwoo_data->$key = $value;
58    }
59
60
61    /**
62     * Add Javascript files to template
63     *
64     * @param string $js
65     */
66    public function add_js($js) {
67        $current   = $this->dwoo_data->js_files;
68        $current[] = $js;
69        $this->dwoo_data->js_files = $current;
70    }
71
72
73    /**
74     * Add Css stylesheets to template
75     *
76     * @param string $css
77     */
78    public function add_css($css) {
79        $current   = $this->dwoo_data->css_files;
80        $current[] = $css;
81        $this->dwoo_data->css_files = $current;
82    }
83
84
85    /**
86     * Display or return the compiled template
87     * Since we assign the results to the standard CI output module
88     * you can also use the helper from CI in your templates!!
89     *
90     * @param string $sTemplate
91     * @param boolean $return
92     * @return mixed
93     */
94    public function display($sTemplate, $return = FALSE) {
95        // Start benchmark
96        $CI = get_instance();
97        $CI->benchmark->mark('dwoo_parse_start');
98
99        // Check if file exists
100        if ( !file_exists($this->template_dir . $sTemplate ) ) {
101            $message = sprintf('Template file \'%s\' not found.', $sTemplate);
102            show_error($message);
103            log_message('error', $message);
104        }
105
106        // Create new template
107        $tpl = new Dwoo_Template_File($this->template_dir . $sTemplate);
108
109        // render the template
110        $template = $this->get($tpl, $this->dwoo_data);
111
112        // Finish benchmark
113        $CI->benchmark->mark('dwoo_parse_end');
114
115        // Return results or not ?
116        if ($return == FALSE) {
117            $CI->output->final_output = $template;
118        } else {
119            return $template;
120        }
121    }
122
123
124    /**
125     * Toggle Codeigniter profiler on/off
126     *
127     */
128    public function enable_profiler($toggle = TRUE) {
129        $CI = get_instance();
130        $CI->output->enable_profiler($toggle);
131    }
132
133
134    /**
135     * Set http header
136     *
137     * @example $this->output->set_header("HTTP/1.1 200 OK");
138     * @example $this->output->set_header('Last-Modified: '.gmdate('D, d M Y H:i:s', $last_update).' GMT');
139     * @param string $header
140     */
141    public function set_header($header) {
142        $CI = get_instance();
143        $CI->output->set_header($header);
144    }
145
146
147    /**
148     * Set status header
149     *
150     * @example $this->output->set_status_header('401');
151     * @example // Sets the header as: Unauthorized
152     * @param string $header
153     */
154    public function set_status_header($header) {
155        $CI = get_instance();
156        $CI->output->set_status_header($header);
157    }
158
159
160    /**
161     * Assign the dwootemplate config items to the instance
162     *
163     */
164    private function initialize() {
165        $CI = get_instance();
166        $CI->config->load('dwootemplate', TRUE);
167        $config = $CI->config->item('dwootemplate');
168        foreach ($config as $key => $val) {
169                $this->$key = $val;
170        }
171    }
172}
Note: See TracBrowser for help on using the repository browser.