Hej Daniel
Jo. Som udgangspunkt smider jeg lige hele mit theme library med undtagelse af nogle specifikke funktioner til det projekt jeg udvikler lige nu:
- class Theme {
- // hold CI instance
- private $_CI;
-
- public function __construct() {
- $this->_CI =& get_instance();
- $this->_CI->load->database();
- }
- public function current() {
- //returns the current theme from the database
- return ($this->_CI->uri->segment(1) == 'admin' || $this->_CI->config->config['force_admin_theme'] == TRUE) ? $this->_CI->configuration->get('admin_theme'): $this->_CI->configuration->get('site_theme');
- }
- public function path_to_theme() {
- if(is_dir('frontend/themes/'.$this->current()) && $this->current() != 'bootstrap') {
- //Theme is in core
- return 'frontend/themes/'.$this->current();
- }
- else if(is_dir('site/themes/'.$this->current()) && $this->current() != 'bootstrap') {
- //Theme is from 3rd party
- return 'site/themes/'.$this->current();
- }
- else {
- return 'frontend/themes/bootstrap';
- }
- }
- public function path_to_specifc_theme($theme) {
- if(is_dir('frontend/themes/'.$theme)) {
- //Theme is in core
- return 'frontend/themes/'.$theme;
- }
- else if(is_dir('site/themes/'.$theme)) {
- //Theme is from 3rd party
- return 'site/themes/'.$theme;
- }
- else {
- return false;
- }
- }
- public function tpl_path($tpl_name) {
- //returns path for correct tpl-file to use
- if(file_exists($this->path_to_theme().'/'.$tpl_name.'.tpl.php') && $this->current() != 'bootstrap') {
- //return theme foldername as tpl_path
- return $this->current().'/';
- }
- else if(file_exists($this->path_to_theme().'/templates/'.$tpl_name.'.tpl.php') && $this->current() != 'bootstrap') {
- //return theme foldername and templates subfolder as tpl path
- return $this->current().'/templates/';
- }
- else {
- //return default theme tpl path if requested file does not exist
- return 'bootstrap/';
- }
- }
- public function tpl_exists($tpl_name) {
- //returns path for correct tpl-file to use
- if(file_exists($this->path_to_theme().'/'.$tpl_name.'.tpl.php') && $this->current() != 'bootstrap') {
- //return theme foldername as tpl_path
- return true;
- }
- else if(file_exists($this->path_to_theme().'/templates/'.$tpl_name.'.tpl.php') && $this->current() != 'bootstrap') {
- //return theme foldername and templates subfolder as tpl path
- return true;
- }
- else {
- //return default theme tpl path if requested file does not exist
- return false;
- }
- }
- }
Der mangler nogle kommentarer og andre småting, men som udgangspunkt virker den kode. Koden er bygget op i mod at der i systemet findes et default theme der hedder bootstrap, så den ser du nok et par gange.
Dertil har jeg i min model en metode til at klargøre en side og returnere de klargjorte data til min controller.
- public function prepare($static_data = NULL) {
- if(is_object($static_data)) {
- $this->_data = $static_data;
- }
- else {
- $this->_prepare_data();
- }
- $this->_return['head_title'] = $this->_prepare_head_title();
- $this->_return['head'] = $this->_prepare_head()."\n";
- $this->_return['styles'] = $this->_prepare_head_styles();
- $this->_return['page_top'] = '';
- $this->_return['page'] = $this->_prepare_page();
- $this->_return['page_bottom'] = '';
- $this->_return['scripts'] = (isset($this->_theme_data['scripts'])) ? $this->_prepare_scripts(): $this->_mandatory_scripts();
- return $this->_return;
- }
Denne har så en række metodekald som forbereder hver sin del af siden. Dertil kan man overstyre de værdier som kommer fra databasen med "statiske" data som jeg har valgt at kalde det. Dog kunne det også være data fra et modul når man benytter HMVC.
F.eks. denne:
- protected function _prepare_page($area = NULL) {
- $sections = array();
- foreach ($this->_theme_data['sections'] as $section => $name) {
- $sections[$section] = $this->_prepare_page_section($section);
- }
- $data = array(
- 'page' => $sections,
- 'site_name' => $this->configuration->get('site_name'),
- 'breadcrumb' => breadcrumb((count($this->uri->segments)) ? $this->uri->segments: array(0 => $this->configuration->get('site_home')))
- );
- $tpl_name = '';
- if($this->_is_front()) {
- if($this->theme->tpl_exists('page--front')) {
- $tpl_name = $this->theme->tpl_path('page--front').'page--front.tpl.php';
- }
- else {
- $tpl_name = $this->theme->tpl_path('page').'page.tpl.php';
- }
- }
- else {
- $tpl_name = $this->theme->tpl_path('page').'page.tpl.php';
- }
-
- return $this->load->view($tpl_name, $data, true);
- }
-
- protected function _prepare_page_section_widget($widget) {
- if($widget->section == 'content' && $widget->content == '{"func":"PAGE"}') {
- //load tpl to for rendering primary content
- $data['title'] = $this->_data->title;
- $data['content'] = (isset($this->_data->body) ? $this->_data->body : $this->_data->content);
- return $this->load->view($this->theme->tpl_path('node').'node.tpl.php', $data, true);
- }
- else {
- //Determine if the widget has content that is rendered by a module
- $widget_contents = $widget->content;
- if(isJson($widget->content)) {
- $widget_data = json_decode($widget->content, TRUE);
- if(isset($widget_data['module'])) {
- $widget_contents = Modules::run($widget_data['module'].'/_widget');
- }
- if(isset($widget_data['menu'])) {
- $widget_contents = array_to_menu($this->db->order_by('position asc, title asc')->get_where('menu_links', array('mid' => $widget_data['menu']))->result_array());
- }
- }
- //load normal widget tpl file
- return $this->load->view($this->theme->tpl_path('widget').'widget.tpl.php', array('content' => $widget_contents), true);
- }
- }
Her benyttes den metode jeg skrev om i forhold til at hente den korrekte template baseret på en værdi i databasen og ligeledes kan man benytte forskellige templates baseret på en værdi i det objekt jeg henter fra databasen.
For at koden virker i dit projekt skal der nok laves nogle tilpasninger, men det burde virke.
Indlæg senest redigeret d. 06.04.2015 01:03 af Bruger #17072