Welcome Guest, Not a member yet? Register   Sign In
Accessing to global "data" variables from helper
#1

[eluser]asumaran[/eluser]
Hello.

I want to do some functions to display some parts of my site, like wordpress.

ie.
the_title() displays the page title, previously declared in $data['title']
and get_the_title, retrieve the content (not displays) the content of $data['title']

the_meta()...
get_the_meta()...

and so on...

This is my controller
Code:
<?php
class Home extends Controller {
    private $data             =    array();
    
    public function __construct()
    {
        parent::__construct();
        $this->load->helper('template');
    }
    function index()
    {        
        $data['title']      =    'Home | '.$this->config->item('title');
        $data['content']    =    $this->load->view('home',$data,TRUE);
    
        $this->load->view('basic_template', $data);
    }
}

in basic_template, I have this

Code:
<html>
<head>
<title><?php the_title(); ?></title>
</head>
<body>
[...]
</body>
</html>


and in my template_helper, I have this:

Code:
function the_title()
{
   echo $data['title'];
}

function get_the_title()
{
   return $data['title'];
}

When I run the code, It give to me a error "undefined variable: data"
what I'm doing wrong?
please help

*sorry for my bad english Smile
#2

[eluser]danmontgomery[/eluser]
In class members

Code:
$data

should be

Code:
$this->data
#3

[eluser]asumaran[/eluser]
It didn't works, or I didn't get it how to do
please, you could explain a little?

thanks in advance
#4

[eluser]danmontgomery[/eluser]
Code:
<?php
class Home extends Controller {
    private $data             =    array();
    
    public function __construct()
    {
        parent::__construct();
        $this->load->helper('template');
    }
    function index()
    {        
        $this->data['title']      =    'Home | '.$this->config->item('title');
        $this->data['content']    =    $this->load->view('home',$this->data,TRUE);
    
        $this->load->view('basic_template', $this->data);
    }
}

You can't really access controller members from helpers, helper functions are not members of any class and shouldn't be expected to access anything they aren't given.

http://php.net/manual/en/language.oop5.php
#5

[eluser]asumaran[/eluser]
thanks, I'll try a different way




Theme © iAndrew 2016 - Forum software by © MyBB