title: Codeigniter default layout like in Twig
body: <p>I'd like to know how to implement this in codeigniter cause it's kind of a hassle to always have this code in the controller.</p>
  
  <pre><code>$this-&gt;load-&gt;view('header');
  $this-&gt;load-&gt;view('content_view',$data);
  $this-&gt;load-&gt;view('footer');
  </code></pre>
  
  <p>In twig, you can define a default template, base.html.twig like this:</p>
  
  <pre><code>&lt;html&gt;
  &lt;body&gt;
      {% block content %}
      {% endblock %}
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>and then in your content_view, you can extend that default template and add the content sent by the current controller:</p>
  
  <pre><code>{% extends 'base.html.twig' %}
  
  {% block content %} 
  ... your data here ...
  {% endblock %}
  </code></pre>
  
  <p>so that i only have to call this in my controller</p>
  
  <pre><code>$this-&gt;load-&gt;view('content_view',$data);
  </code></pre>
  
  <p>and voila, the system loads my default template along with my content_view data</p>
  
  <p>i do not want to use libraries or anything like that. i want to implement this without using third party software.</p>
  
  <p>edit:</p>
  
  <p>I've seen one good idea here, first answer, <a href="https://stackoverflow.com/questions/4146215/getting-the-hang-of-codeigniter-templating-loading-views?rq=1">Getting the hang of CodeIgniter - Templating / loading views</a></p>
  
  <p>but it's only close to what I wanted to implement which is more like the twig template engine</p>
  
  <p>Let's say I wanted to add another block to my default template, the navigation, as well as the website name and page title:</p>
  
  <pre><code>&lt;html&gt;
  &lt;head&gt;
      &lt;title&gt;{{ app_name }} :: {{ page_title }}&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;div id="navi"&gt;
      {% block navi %}
      {% endblock %}
  &lt;/div&gt;
  
  &lt;div id="content"&gt;
      {% block content %}
      {% endblock %}
  &lt;/div&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>navigation.html.twig</p>
  
  <pre><code>{% extends 'base.html.twig' %}
  
  (% if active ? 'class="active" : '' %}
  
  &lt;ul&gt;
      &lt;li {{ active }}&gt;Home&lt;/li&gt;
      &lt;li {{ active }}&gt;About&lt;/li&gt;
      &lt;li {{ active }}&gt;Contact&lt;/li&gt;
  &lt;/ul&gt;
  </code></pre>
  
  <p>then in my controller, i have the following data i want to pass on content_view:</p>
  
  <pre><code>$content = array(
      'page_title' =&gt; 'About this Site', 
      'content_body' =&gt; 'blah blah, blah'
  );
  
  $data = array(
      'app_name' =&gt; 'Example',
      'page_title' =&gt; 'About',
      'active' =&gt; 'about',    // the active page to highlight in navigation
      'contents' =&gt; $content
  )
  
  $this-&gt;load-&gt;view('content_view',$data);
  </code></pre>
  
  <p>now even though the variables are in different locations, since they are all linked from one default page, the values will be printed all together.</p>
  
  <pre><code>&lt;html&gt;
  &lt;head&gt;
      &lt;title&gt;Example :: About&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;div id="navi"&gt;
      &lt;ul&gt;
          &lt;li&gt;Home&lt;/li&gt;
          &lt;li class="active"&gt;About&lt;/li&gt;
          &lt;li&gt;Contact&lt;/li&gt;
      &lt;/ul&gt;
  &lt;/div&gt;
  
  &lt;div id="content"&gt;
      &lt;h3&gt;About this Site&lt;/h3&gt;
  
      blah, blah blah
  &lt;/div&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>