Welcome Guest, Not a member yet? Register   Sign In
Using the language strings in the view
#1

[eluser]Unknown[/eluser]
This might be a stupid question but I found no answer to it.

In the controller I use:
$this->lang->line('language_key');

but I have no use fo the language strings in the controller, i need them in the view.

But what do I use in the view?! Do I have to pass all the language keys in an array to the view or is there something more automatic?
#2

[eluser]BravoAlpha[/eluser]
[quote author="naicuoctavian" date="1187415451"]In the controller I use:
$this->lang->line('language_key');[/quote]
You can use that in the view too.
#3

[eluser]esra[/eluser]
You can also use Bravo's approach to assign language strings to variables and reuse the variables directly or indirectly as necessary in your controllers and views.

Quote: $page = $this->lang->line('dashboard_title');
$this->view->set('title', $page);

In your view, you could use:

Quote: <title><?=$title ?></title>

The second line of the above uses Coolfactor's View library, but many of the other View libraries and template solutions have a similar method for setting variables. The above solution is good if you need to reuse the same variable multiple times within the same controller:

You can also do this:

Quote: $this->view->set('title', $this->lang->line('dashboard_title');

A more complete example:

Code:
<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

require(APPPATH.'libraries/application'.EXT);

class Dashboard extends Application {

    public function __construct()
    {
        parent::Application();    
        $this->lang->load('dashboard', 'en_us');    
    }
    
    public function index()
    {
        $component = $this->lang->line('dashboard_component');
        $page = $this->lang->line('dashboard_title');
        
        $this->view->set('title', $page);
        $this->view->set('component', $component);
        $this->view->part('tree', 'tree');
        $this->view->part('navigation', 'navigation');
        $this->view->part('tabpanel', 'tabpanel');
        $this->view->part('content', $component);
        $this->view->part('properties', 'properties');
        $this->view->part('output', 'output');
        $this->view->load('complex');
    }
}
?>

The master view below is composed of multiple view fragments and the $component variable is used multiple times in the controller and view:

Code:
<html>
<head>
  <title><?=$title ?></title>
    <link rel="stylesheet" type="text/css" href="<?=js_url();?>ext/resources/css/ext-all.css" />

    <!-- GC --> <!-- LIBS -->    
    < script type="text/javascript" src="<?=js_url();?>ext/adapter/yui/yui-utilities.js">    
    < script type="text/javascript" src="<?=js_url();?>ext/adapter/yui/ext-yui-adapter.js">    
    < script type="text/javascript" src="<?=js_url();?>ext/ext-all.js">
    <!-- ENDLIBS -->
  
    
    <link rel="stylesheet" type="text/css" href="<?=templates_url();?>complex/css/template.css" />
</head>
<body>

<div id ="container">
    &lt;?=$tree ?&gt;
    &lt;?=$navigation ?&gt;
    &lt;?=$tabpanel ?&gt;
    &lt;?=$content ?&gt;
    &lt;?=$properties ?&gt;
    &lt;?=$output ?&gt;
</div>
&lt;/body&gt;
&lt;/html&gt;
#4

[eluser]Unknown[/eluser]
Thks for the reply BravoAlpha.
esra the approach you are describing does not seem to appropriate for when one has many language strings, but thks. I personally use Smarty.




Theme © iAndrew 2016 - Forum software by © MyBB