3
\$\begingroup\$

I created a library class in CodeIgniter that handles building the HTML body view.

This class builds the body, adds content and adds javascript element tags at the end. The body is built by using a layout, inserting the content to the appropriate hooks within the layout and finally the javascript. Layout, content and javascript elements have their own objects and instantiated by the objFactory. objFactory was necessary because CI follows a singleton approach and library classes can only be loaded within the super class and not instantiated as a new object. The class also has a default content that can be set and default javascript (i'm using bootstrap).

Most of the variables used by the class I made into class properties. I'm not sure if I did this correctly

Did I implement the correct OOP standards? Is my code coherent? understandable and well-formed?

private $_CI, $_layout, $_header;   
private $_defaultContent    = array();
private $_defaultJS         = array();
private $_JS                = array();
private $_content           = array();

public function __construct($layout = 'Base'){  
    $this->_CI      =& get_instance();
    $this->_layout   = $layout;

    $this->_JS['inline'] = "";
    $this->_JS['external'] = array();

    $this->_setDefaultJS();
}

public function addContent($contentOpt){
    $this->_CI->load->library('ObjFactory');
    $this->_content[] = $this->_CI->objfactory->createContentObj($contentOpt);
}

public function addJS($location, $script){
    if($location == "external")
        $_JS[$location][] = $this->_CI->objfactory->createJSTagObj($location, $script);
    else
        $_JS[$location] .= $script;
}

public function build(){
    $layoutObj = $this->_CI->objfactory->createLayoutObj($this->_layout);
    $layoutObj->setContentData($this->_content);        

    // build layout with content
    $data['content'] = $layoutObj->build();

    // build script elements
    $data['scripts'] = $this->_buildJS();   

    // build actual body and close </body>
    $this->_CI->load->view('body.php',$data);
}

private function _buildContentHtml(){
    $htmlString = "";

    foreach($this->_content as $content)
    {
        $htmlString .= $content->build();
    }

    return $htmlString;
}

private function _buildJS(){
    return $this->_generateJSHtmlString("default"). 
           $this->_generateJSHtmlString("external"). 
           $this->_generateJSHtmlString("inline");
}

private function _generateJSHtmlString($type){
    $jsString = "";

    switch($type)
    {
        case "default":             
            $jsCollection = $this->_defaultJS;                  
            break;

        case "external":                
            $jsCollection = $this->_JS["external"];             
            break;

        default:
            if(strlen($this->_JS["inline"]) > 0)
            {
                $newJSObj = $this->_CI->objfactory->createJSTagObj($this->_JS["inline"]);
                $jsString = $newJSObj->build();
            }
            return $jsString;                   
            break;
    }

    foreach($jsCollection as $jsTagObj)     
        $jsString .= $jsTagObj->build();


    return $jsString;           
}

private function _setDefaultJS(){
    $script1 = "https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js";
    $script2 = "https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js";       

    $this->_defaultJS[] = $this->_CI->objfactory->createJSTagObj('external', $script1);
    $this->_defaultJS[] = $this->_CI->objfactory->createJSTagObj('external', $script2);
}   
\$\endgroup\$
  • \$\begingroup\$ @Quill Comments should be for seeking clarification, not for making suggestions to change the code. If you have suggestions that would demonstrably improve the code, then please write an answer and add supporting details. If you can't substantiate your recommendations, then please refrain from commenting or answering. \$\endgroup\$ – 200_success Apr 17 '15 at 3:04
  • \$\begingroup\$ I don't know if you want to reinvent the wheel or if you want to improve your CI Skills, but you should more often than not look for some alternatives out there. In your case, I recommend that you take a look at Phil Sturgeon's Template Library \$\endgroup\$ – sintakonte May 2 '15 at 13:29
  • \$\begingroup\$ I think you can utilize smarty or any other MVC based library to do the same thing. \$\endgroup\$ – FreeMind Jun 1 '15 at 18:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.