title: Building a multi-language navigation menu in CodeIgniter
body: <p>I'm trying to create a function that builds and displays a navigation menu, whilst keeping to MVC as much as possible (though I'm new to it, so I don't understand it completely).</p>
  
  <p>My script dies without providing an error message. Let's investigate!</p>
  
  <p>In my view, I call a function that builds the menu's contents, and send in the names of the pages that should exist in the menu:</p>
  
  <pre><code>// application/views/templates/header.php
  
  &lt;ul class="navigation"&gt;
          &lt;?php 
          //  Send in the English name, which also becomes the slug.
          //  Function should return the name in the appropriate language,
          //  plus the slug in English.
  
          $args = ['home','compete','gallery','finalists','about'];
          build_navigation($args);
          ?&gt;
  &lt;/ul&gt;
  </code></pre>
  
  <p>The idea now is to loop through those arguments, and build a list-item for each argument containing the file name — which is also the URL slug — and the display name in the appropriate language.</p>
  
  <pre><code>// application/helpers/navigation_helper.php
  // This is loaded in autoload.php — confirmed working
  
  &lt;?php
  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  
  if ( ! function_exists('build_navigation')) {
  
      function build_navigation($args) {
          foreach ($args as $token)
              echo "&lt;li&gt;&lt;a href=\"{$token}\"&gt;{$this-&gt;lang-&gt;line($token)}&lt;/a&gt;&lt;/li&gt;\n";
      }   
  }
  
  ?&gt;
  </code></pre>
  
  <p>When I look at that, it sort of makes sense in my head, but at the same time raises questions like “what is <code>$this</code> in the given context?”</p>
  
  <p><strong>If I change</strong> <code>$this-&gt;lang-&gt;line($token)</code> to just <code>$token</code>, <strong>the script runs</strong> (though I don't get my multi-language functionality).</p>
  
  <p>I have the language files I need…</p>
  
  <pre><code>// application/language/english/en_lang.php
  
  &lt;?php
  
  $lang['home'] = "Home";
  $lang['compete'] = "Compete";
  $lang['gallery'] = "Gallery";
  $lang['finalists'] = "Finalists";
  $lang['about'] = "About";
  
  ?&gt;
  
  // application/language/swedish/sv_lang.php
  
  &lt;?php
  
  $lang['home'] = "Hem";
  $lang['compete'] = "Tävla";
  $lang['gallery'] = "Galleri";
  $lang['finalists'] = "Finalister";
  $lang['about'] = "Info";
  
  ?&gt;
  </code></pre>
  
  <p>…And here you can see that I'm loading my language files in my controller (which almost exactly mirrors the pages controller in the CI docs):</p>
  
  <pre><code>&lt;?php
  
  /**
   * Pages
   *
   * Class for building static pages.
   *
   */
  
  class Pages extends CI_Controller {
      public function view ($page = 'home') {
  
          if (!file_exists('application/views/pages/'.$page.'.php'))
              show_404();
  
          $data['title'] = ucfirst($page); // Capitalise first letter of title
  
          $this-&gt;lang-&gt;load('en','english');
          $this-&gt;lang-&gt;load('sv','swedish');
          $this-&gt;load-&gt;view('templates/header', $data);
          $this-&gt;load-&gt;view('pages/'.$page, $data);
          $this-&gt;load-&gt;view('templates/footer', $data);
  
      }
  }
  
  ?&gt;
  </code></pre>