Welcome Guest, Not a member yet? Register   Sign In
HMVC two modules passing the same variable name to their views and causing conflict
#1

[eluser]TomTom[/eluser]
I have two modules in my installation. Both modules' controllers pass a variable called $data['content'] to their views. Also, the first module's view runs the second module via <?php echo Modules::run('module2'); ?>, and after that is supposed to display values from its $data['content'] variable. Unfortunately, that's when the first module's $data content is substituted with the second module's $data. This is pretty inconvenient for me, so I wuold like to know if there is a way to "protect" the $content variables and keep them only within their associated modules?

I would like to avoid renaming $data['content'] if possible. I've found a not-so-perfect solution in using $data(__CLASS__), but I am curious if it is possible not to change $data['content'].
#2

[eluser]CroNiX[/eluser]
Not sure if it would be helpful here, but you can use $this->load->vars($data_array) and make those globally available to all views. Doing that, you don't have to explicitly pass the data to the view.

Code:
$data['page_title'] = 'page title';
$this->load->vars($data);
$this->load->view('view_file');  //Don't need to pass $data

You can then also use $this->load->get_var($key) to see if it exists.

More info in the User Guide for the Loader class

You could also try not assigning both as 'content' and use 2 different keys for it. $data['module1_content'], $data['module2_content'], etc., so they don't override each other.
#3

[eluser]TomTom[/eluser]
CroNiX thanks a lot for the advice. I'll read more about the Loader class and see if I can make it useful for this problem.

Renaming my $data['content'] is exactly what I would like to avoid. I'll try to explain why as briefly as I can.

1. I would like to use the same view files for different modules. I'm not really sure if that's a good idea, but having different variable names in different controllers would make it much more difficult, if not impossible.
2. I would like to use for example one and the same module as a view partial on more than one place in the same page, but displaying different data depending on the parametres passed to it. This now seems impossible to me, since the module loaded last will always overwrite the previous modules' variables.

However, since posting this question, I've been thinking how useful this idea would be at all, even if it is possible, because then I would have several different variables with the same name in one view. I'll think it over to improve my concept.

Thanks for the advice about the Loader class again. I'll have a good look at it.
#4

[eluser]CroNiX[/eluser]
In your view, you can always test to see if it exists.

Code:
<?php if (isset($content_2)): ?>
<div class="sub-content">&lt;?=$content_2; ?&gt;</div>
&lt;?php endif; ?&gt;

What you're doing is not impossible. Each view partial should really have it's own unique variables so you don't run into the problems you are with views calling other views (or whatever) with the same named variable.

I've created a fairly complex templating system that does all of this. I'm a big fan of DRY.

I use the same view for adding as well as editing. However, if I'm adding, it knows that and outputs the appropriate content that might differ a bit from editing. Such as "Edit User: $usernmae" for an H1 vs "Create New User".
#5

[eluser]TomTom[/eluser]
Trying to get DRY is what got me to this point actually. I know I should test if the variable is set first, but the problem is in the final product I will not have a finite number of such variables.




Theme © iAndrew 2016 - Forum software by © MyBB