{"id":13560291,"url":"https://github.com/jenssegers/codeigniter-template-library","last_synced_at":"2025-04-14T06:20:31.481Z","repository":{"id":66262578,"uuid":"1870709","full_name":"jenssegers/codeigniter-template-library","owner":"jenssegers","description":"A template library for Codeigniter","archived":false,"fork":false,"pushed_at":"2013-02-18T08:42:18.000Z","size":272,"stargazers_count":107,"open_issues_count":2,"forks_count":59,"subscribers_count":18,"default_branch":"master","last_synced_at":"2025-03-27T20:06:00.390Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"http://jenssegers.be","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jenssegers.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2011-06-09T13:05:54.000Z","updated_at":"2023-06-03T10:39:12.000Z","dependencies_parsed_at":"2023-02-20T01:15:31.023Z","dependency_job_id":null,"html_url":"https://github.com/jenssegers/codeigniter-template-library","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jenssegers%2Fcodeigniter-template-library","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jenssegers%2Fcodeigniter-template-library/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jenssegers%2Fcodeigniter-template-library/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jenssegers%2Fcodeigniter-template-library/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jenssegers","download_url":"https://codeload.github.com/jenssegers/codeigniter-template-library/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248830765,"owners_count":21168350,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-01T13:00:41.256Z","updated_at":"2025-04-14T06:20:31.451Z","avatar_url":"https://github.com/jenssegers.png","language":"PHP","readme":"CodeIgniter Template Library\r\n============================\r\n\r\nThis template library for Codeigniter lets you build complex templates using partial views and widgets. It's built with the same method chaining support that we are seeing so often in Codeigniter so it feels familiar. This library loads a template file that uses partial views. These partial view sections are internally represented by Partial Objects managed by the template library. These objects let you modify their content in a user friendly way through method chaining.\r\n\r\nInstallation\r\n------------\r\n\r\nCopy the files to the corresponding folder in your application folder (or use spark).\r\n\r\nConfiguration\r\n-------------\r\n\r\nIn your template.php config file you can change following configuration parameters (optional):\r\n\r\n    /*\r\n    | -------------------------------------------------------------------\r\n    | Template configuration\r\n    | -------------------------------------------------------------------\r\n    | This file will contain the settings for the template library.\r\n    |\r\n    | 'parser'     = if you want your main template file to be parsed, set to TRUE\r\n    | 'template'   = the filename of the default template file\r\n    | 'cache_ttl'  = the time all partials should be cache in seconds, 0 means no global caching\r\n    */\r\n\r\n    $config['parser']    = FALSE;\r\n    $config['template']  = 'template';\r\n    $config['cache_ttl'] = 0;\r\n\r\nIf you prefer, you can autoload the library by adjusting your autoload.php file and add 'template' to the $autoload['libraries'] array.\r\n    \r\nTemplate files\r\n--------------\r\n\r\nTemplate files are loaded or parsed by Codeigniter and the partials are passed to them as data. You can easily load them like you would normally use data in your view files:\r\n\r\n    \u003chead\u003e\r\n        \u003ctitle\u003e\u003c?php echo $title; ?\u003e\u003c/title\u003e\r\n        \u003c?php echo $stylesheet; ?\u003e\r\n    \u003c/head\u003e\r\n    \u003cbody\u003e\r\n        \u003c?php echo $content; ?\u003e\r\n    \u003c/body\u003e\r\n\r\nOr when parsing is enabled you can use {content} etc.\r\n\r\nHowever, I prefer to directly call the library's methods from inside the template file to work around php's Undefined variable errors when you are not setting all partials. Calling these methods well replace non-existing partials with empty one's so you don't get any errors:\r\n\r\n    \u003chead\u003e\r\n        \u003ctitle\u003e\u003c?php echo $this-\u003etemplate-\u003etitle; ?\u003e\u003c/title\u003e\r\n        \u003c?php echo $this-\u003etemplate-\u003estylesheet; ?\u003e\r\n    \u003c/head\u003e\r\n    \u003cbody\u003e\r\n        \u003c?php echo $this-\u003etemplate-\u003econtent; ?\u003e\r\n    \u003c/body\u003e\r\n\r\nThese variables are in fact Partial Ojects, so you can still manipulate them from inside the template view file like this:\r\n\r\n    \u003c?php echo $title-\u003eprepend('My Website - '); ?\u003e\r\n\r\nPartial manipulation methods will always return the partial object itself for further chaining or for displaying. So this is perfectly possible:\r\n\r\n    \u003c?php echo $sidebar-\u003ecache(500)-\u003ewidget('login')-\u003eprepend('Login: '); ?\u003e\r\n\r\nPartial manipulation\r\n--------------------\r\n\r\nPartials have a few handy methods for manipulating their content such as:\r\n\r\n    $partial-\u003eset() - overwrites the content\r\n    $partial-\u003eappend() - append something\r\n    $partial-\u003eadd() - same as append (alias)\r\n    $partial-\u003eprepend() - prepend something\r\n    $partial-\u003econtent() - gets the content\r\n    $partial-\u003edefault() - only set content if empty\r\n\r\nYou can also load dynamic content inside partials from view files or widgets. The object named partial used in the method chaining below is the name of the partial you want to load the content into.\r\n\r\n    $this-\u003etemplate-\u003epartial-\u003eview()\r\n\r\nAppend or overwrite the partial with a view file with parameters.\r\n\r\n    $this-\u003etemplate-\u003epartial-\u003eview('view-file', array(), $overwrite=FALSE);\r\n\r\nAppend or overwrite the partial with a parsed view file with parameters.\r\n\r\n    $this-\u003etemplate-\u003epartial-\u003eparse('view-file', array(), $overwrite=FALSE);\r\n\r\nAppend or overwrite the partial with a widget's output.\r\n\r\n    $this-\u003etemplate-\u003epartial-\u003ewidget('widget-name', array(), $overwrite=FALSE);\r\n\r\nPublishing\r\n----------\r\n\r\nThe template class only has a few methods. I chose to do this because almost everything can be managed by using the flexible Partial Object. If you want to publish the entire template with the current partials to the output you can use the publish() method.\r\n\r\nYou can pass a custom template file and optional data if wanted:\r\n\r\n    $this-\u003etemplate-\u003epublish('template', array('title'=\u003e'Title is overwritten!'));\r\n\r\nMost of the time this will be empty using the template file from the config:\r\n\r\n    $this-\u003etemplate-\u003epublish();\r\n    \r\nIf you wish to set the template file before publishing, eg. in a controller's constructor:\r\n\r\n    $this-\u003etemplate-\u003eset_template('template2');\r\n    \r\nTriggers\r\n--------\r\n\r\nSome partials have built in triggers:\r\n\r\n    stylesheet - you only need to pass the url\r\n    javascript - you only need to pass the url\r\n    meta - will convert the arguments to the right meta tag\r\n    title - converts special characters\r\n    description - will convert special characters just like the title\r\n\r\nThis is an example of what these built in triggers do:\r\n\r\n    $this-\u003etemplate-\u003estylesheet-\u003eadd('stylesheet.css', array('media' =\u003e 'all'));\r\n    //\u003clink rel=\"stylesheet\" href=\"http://myweb.com/stylesheet.css\" media=\"all\" /\u003e\r\n     \r\n    $this-\u003etemplate-\u003ejavascript-\u003eadd('script.js');\r\n    //\u003cscript src=\"http://myweb.com/script.js\"\u003e\u003c/script\u003e\r\n     \r\n    $this-\u003etemplate-\u003emeta-\u003eadd('robots', 'index,follow');\r\n    //\u003cmeta name=\"robots\" content=\"index,follow\" /\u003e\r\n     \r\n    $this-\u003etemplate-\u003etitle-\u003eset('Dad \u0026 Son');\r\n    //Dad \u0026amp; Son\r\n\r\nYou can set your own triggers for functions or methods for any partial object like this:\r\n\r\n    //function\r\n    $this-\u003etemplate-\u003epartial-\u003ebind('strtoupper');\r\n     \r\n    //method\r\n    $this-\u003etemplate-\u003epartial-\u003ebind($this-\u003etypography, 'auto_typography');\r\n\r\nThis will trigger the function or method whenever you manipulate the partial's content.\r\n\r\n\r\nWidget\r\n------\r\n\r\nWidgets are intelligent partial objects. When their content is asked, their display() method is activated which will fill the content using codeigniter or partial object methods. Widgets classes are found inside the application/widgets folder. They extend the main Widget class which has the same methods as the Partial class. This is an example widget:\r\n\r\n    /* File: widgets/hero_widget.php */\r\n    class hero_widget extends Widget {\r\n         public function display($args = array()) {\r\n             $this-\u003eload-\u003emodel('my_model');\r\n             $data = $this-\u003emy_model-\u003eall();\r\n     \r\n             $this-\u003eload-\u003eview('widgets/hero', $data);\r\n         }\r\n    }\r\n\r\nAnd this is loaded from a controller like this:\r\n\r\n    $this-\u003etemplate-\u003epartial-\u003ewidget('hero_widget', $args = array());\r\n\r\n    \r\nCaching\r\n-------\r\n\r\nI did not want to expand the library in all different ways, therefore I implemented a basic caching function using Codeigniter's caching driver. This might slow your code down on simple websites but allows you to use caching for partials just like you would do yourself with Codeigniter's driver.\r\n\r\nYou can cache particular partials:\r\n\r\n    $this-\u003etemplate-\u003epartial-\u003ecache(100);\r\n\r\nOr you can cache all partials:\r\n\r\n    $this-\u003etemplate-\u003ecache(100);\r\n\r\nBoth methods have an extra optional identification parameter that you can use to have multiple cache files for different pages:\r\n\r\n    $this-\u003etemplate-\u003ecache(100, 'frontpage');\r\n","funding_links":[],"categories":["Templates"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjenssegers%2Fcodeigniter-template-library","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjenssegers%2Fcodeigniter-template-library","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjenssegers%2Fcodeigniter-template-library/lists"}