0

I have a controller that sets up several variables like this...

$data["pageTitle"] = "My Profile";
$data["userData"] = $this->user_model->getUserData($user_id);
$data["userTool"] = $this->api->getUserTool($user_id);

In the view I know I can access each as a separate variable...

$pageTitle
$userData
$userTool

The data in each variable is passed to a function in the view for processing.

showUserData($pageTitle, $userData, $userTool);

What I would like to do is pass $this to the function and parse the data from that rather than have a string of arguments in the call to the function. Is this doable?

3
  • 1
    With certain exceptions, it's generally considered bad form to be calling functions in your views. Perhaps you could call these functions in your controller (or a model, depending on what you are doing) and then take the output of those functions and put them in your $data array. I'm curious what showUserData does that it needs to be in a view. Commented Mar 2, 2018 at 1:49
  • Are you asking whether or not you can use $this in the view? Because you can. Your view isn't just limited to variables you passed via load. If that is not your question, maybe show what kindof code you were thinking of. Commented Mar 2, 2018 at 3:18
  • Thanks guys. The purpose of the function in the view is strictly to render the presentation of the data, tables, forms and etc. as HTML. As I understand it, the presentation is best left in the view. I can perform the visual manipulations in the Controller but then rendering functions would put presentation code in the Controller instead of the view. Since the rendering functions are unique only to that view, my inclination is to keep them there. Open to alternatives though. Commented Mar 2, 2018 at 11:40

2 Answers 2

1

In your controller

$data['mycustomdata']["pageTitle"] = "My Profile";
$data['mycustomdata']["userData"] = $this->user_model->getUserData($user_id);
$data['mycustomdata']["userTool"] = $this->api->getUserTool($user_id);

In your view

showUserData($mycustomdata);
Sign up to request clarification or add additional context in comments.

1 Comment

Brilliant. Don't know why I didn't see that before. This does exactly what I need. Lets me pack several variables into one. Thx.
0

I wonder what what showUserData does that it needs to be in a view(agreed with @S. Imp 's comment).

As a challenge, we can achieve your requirement doing like this,

controller,

$this->pageTitle = "My Profile";
$this->userdata = array('test inpunt','test input2');
$this->userTool = array('test user tool','test user tool2');
$this->load->view('test_view');

View,

function showUserData($obj) {

    echo "<br />".$obj->pageTitle;
    echo "<pre>";print_r($obj->userdata); echo "</pre>";
    echo "<pre>";print_r($obj->userTool); echo "</pre>";

}

showUserData($this);

Comments

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.