0

I`m from Russia, so sorry for bad English. I want to load template in every page in controller. For example (library parser in autoload),

class Blog extends CI_Controller {
$header = array(
'header' => 'Welcome to my blog!'
);
$this->parser->parse('header', $header);

function index() {
...
echo "My text";
}   

header.php:

<h1>{header}</h1>
<script>
alert("Welcome!");
</script>

But I get an PHP error: syntax error, unexpected T_VARIABLE, expecting T_FUNCTION in line 6. Line 6:

$header = array(

How can I load header in every page? Thanks.

0

4 Answers 4

1

config.php:

$autoload['libraries'] = array('parser');

controller blog.php:

<?php

class Blog extends Controller {

function __construct() 
    {
            parent::Controller();
            $header = array('header' => 'Welcome to my blog!');
            $this->parser->parse('header', $header);
    }

function index()
    {
        echo "Мой текст";
    }


}
?>

view header.php:

{header}

it works for me..

Sign up to request clarification or add additional context in comments.

Comments

0

try calling the array with $this->header

Comments

0

Maybe it's a typing error but in the code from header.php you have typed {header} where I guess it should be {$header}

Comments

0

When loading class properties, like your $header above, php expects the property's visibility to be declared before the variable name. PHP5 has three visibility options: 'public', 'private', or 'protected'. I think this is why you are getting "unexpected T_VARIABLE". The difference between them are described at swik.net as:

To protect from accessibility pollution, PHP v5 introduces 3 prefixes for declaring class methods or variables: public, protected, and private.

Public methods and variables are accessible outside of the class. Protected are only accessible from inside of the class and inherited or parent classes. Private are only accessible from within the class itself.

try this: (I chose 'public' visibility, you can determine which is appropriate for your use)

public $header = array('header'=>'Welcome to my blog");

Next, I think you should call your parser in a constructor, rather than outside of a class method.

function _construct(){
    parent::_construct();
    $this->parser->parse('header',$this->header);
}

The constructor will be called every time the class is instantiated, loading your parser library method along with it.

Update:

Your comment suggests that the parser isn't working like you expect. I assume you have placed

$this->parser->parse('header,$this->header);

in the constructor function like I suggested. If that isn't working, create a function with the same name as your class and put the parser in there, that function will load every time the class is called, similar to the constructor, but let's see if that does the trick. I suggest taking the parser library out of auto load until you've resolved your problem, just to simplify things.

function blog(){
    $this->load->library('parser');
    $this->parser->parse('header',$this->header);
}

1 Comment

No errors, page loaded, but view doesn`t loaded. Only "My text" without header.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.