• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Why can't I pass this variable to a Twig view?

#1
I am working on a online newspaper/blogging application with CodeIgniter 3.1.8 and Bootstrap 4. I have decided to add themes to it. The application is not HMVC, only MVC.

The themes directory is outside application as can be see in the image below:
[Image: syL0A.jpg]
Inside themes I have the theme directory (of course) which contains the "master view", 

Code:
layout.php
:
[Image: gOGzZ.jpg]
How I use the theme views


In application/core I have added a MY_Loader.php file with the following contents:

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

class MY_Loader extends CI_Loader {

function theme_view($folder, $view, $vars = array(), $return = FALSE) {
  $this->_ci_view_paths = array_merge($this->_ci_view_paths, array(FCPATH . $folder . '/' => TRUE));
   return $this->_ci_load(array(
    '_ci_view' => $view,
    '_ci_vars' => $this->_ci_prepare_view_vars($vars),
    '_ci_return' => $return
   ));
  }
}


In my Posts controller's index() method, I load the view passing it the data:

Code:
public function index() {
  //more code here
  $data['posts'] = $this->Posts_model->get_posts($config['limit'], $config['offset']);
  $this->load->theme_view('/themes/caminar/', 'layout', $data);
}


Using Twig for the themes


The next step was to add the [i]Twig[/i] template engine to the theme(s). For this purpose, I use CodeIgniter Simple and Secure Twig. I have:
  • Added the [i]Simple and Secure Twig[/i] library in application\libraries


  •  In the third_party directory I have added Twig itself.


  • In application\config\autoload.php I have loaded twing:
    $autoload['libraries'] = array('database', 'form_validation', 'session', 'user_agent', 'twig');

  • Set the path for the Twig views in application\libraries\Twig.php:
Code:
private $config = [ 'paths' => [FCPATH . '/themes', VIEWPATH], 'cache' => '/path/to/twig/cache', ];

The problem

According to the docs of CodeIgniter Simple and Secure Twig, we can set a global variable like so: 

Code:
$this->twig->addGlobal('sitename', 'My Awesome Site');

So, I added 
Code:
$this->twig->addGlobal('siteTitle', 'My Awesome Site');

 in the Posts controller:
Code:
public function index() {

//call initialization method
$config = $this->_initPagination("/", $this->Posts_model->get_num_rows());

$data = $this->Static_model->get_static_data();
$data['pages'] = $this->Pages_model->get_pages();
$data['categories'] = $this->Categories_model->get_categories(); 

//use limit and offset returned by _initPaginator method
$data['posts'] = $this->Posts_model->get_posts($config['limit'], $config['offset']);
$this->twig->addGlobal('siteTitle', 'My Awesome Site');
$this->load->theme_view('/themes/caminar/', 'layout', $data);
}

Yet, in themes\caminar\layout.php the line 
Code:
<title>{{siteTitle}}</title>

 
does not display "My Awesome Site" inside the <title> tag.
What am I doing wrong?
Reply

#2
I think it has something to do with the ThirdParty loading.

Read this may help you.

CodeIgniter: Access Third Party Libraries
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.