Welcome Guest, Not a member yet? Register   Sign In
Wrapping content in layout after execution (need some opinions)
#11

[eluser]mycroes[/eluser]
[quote author="SitesByJoe" date="1225441851"]
Quote:I seriously don’t consider your code a viable solution. Besides the fact that I very often pass data along to views, I just want to be able to use CI without these kind of hacks.

Please explain what I'm hacking here. I just tried to post a simple example to solve a simple problem.[/quote]
There's no way to manipulate view data anymore, except by setting it globally (which logically isn't always desired and sometimes even impossible without editing views). The example is quite simple, but it doesn't solve my problem... That could mean my problem is too complex (and I think it's at least not just any simple problem) or that your solution is too simple. Maybe a hack isn't the right word, I think workaround would be better suited, that also in one word says it's not the fix (not for my issue at least).

[quote author="SitesByJoe" date="1225441851"]
Quote:Currently my code works, as long as I load views and don’t echo any content. Also show_404 doesn’t work, but I guess there’s something strange with codeigniter going on here…

Sorry to bring this up again, but speaking of hacks?!? I'm not gonna argue code - I'm real impressed with the fact you can perform so many manipulations, but geez - your solution is rather verbose considering it simply adds content to a view.
[/quote]

I'm using about as much code as (minimally) needed, preserving wanted functionality without adding unneeded code. I'm also preserving all functionality inside the controller, so I don't think my code has anything to do with being a hack... I wouldn't consider the file verbose, it's actually lacking comments which should really be in every file... Of course I could shave about 10 lines off, preserving the same functionality, but making the code harder to read/understand (especially without those comments Smile ).

[quote author="SitesByJoe" date="1225441851"]Perhaps I shouldn't even be defending this, but isn't the point of code igniter to do alot with a little code?[/quote]
I think the point of code igniter is to code efficient and effective, not necessarily to do a lot with little code... If I'm wrong, it's time for a fork().
#12

[eluser]mycroes[/eluser]
I felt bored, here's a shorter version (written on the forum, I won't use this):
Code:
<?php
class Layout_controller extends Controller {
        function Layout_controller() {
                parent::Controller();
        }

        function _output($output) {
                echo (isset($this->disabled) && $this->disabled) ? $output : $this->load->view('layout/denc', array('content' => $output), true);
        }
}

It's getting hacky this way. Now it also needs explanation that you need to set a variable called 'disabled' by hand that evaluates to true to disable the view. Also if you want to rename the variable to 'layout_disabled' it means changing all the views, while I will probably never have a controller function called 'layout_off'.

If you're really in the mood...:
Code:
<?php class Layout_controller extends Controller {function Layout_controller() {parent::Controller();}function _output($output) {echo (isset($this->disabled) && $this->disabled) ? $output : $this->load->view('layout/denc', array('content' => $output), true);}}
But I must say I've seen better one-liners...
Regards,

Michael
#13

[eluser]mycroes[/eluser]
I actually forgot I could do without the constructor too in this case, maybe using a disabled and isset will make life a bit easier (though I might one day need that constructor anyway...):
Code:
<?php class Layout_controller extends Controller {function _output($output){echo (isset($this->disabled) && $this->disabled) ? $output : $this->load->view('layout/denc', array('content' => $output), true);}}
Regards,

Michael
#14

[eluser]mycroes[/eluser]
I updated my controller (thanks to SitesByJoe for making me think about different ways of implementing this):
Code:
<?php

abstract class Layout_controller extends Controller {

    function layout_off() {
        $this->layout_disabled = true;
    }

    function _output($output) {
        if (isset($this->layout_disabled) && $this->layout_disabled === true) {
            echo $output;
        } else {
            echo $this->load->view('layout/denc', array('content' => $output),  true);
        }
    }

}
I made the class abstract, which it needs to be any way (I don't want to load my layout without anything in it, or without a title). Because it's abstract, and because it extends Controller the call to parent::Controller in the child will call <Controller>::Controller, which is exactly what I was doing myself. This way I don't need to reroute any functions, however I did have to make sure layout_disabled was safe to use so I added the isset check. Of course I could still use __construct to set $this->layout_disabled (I'm using PHP5, I don't care for portability), but I think I prefer it this way... Would the controller have had more code I would rather define it.

I again want to thank SitesByJoe because this will also make my Login_controller a bit neater!
Regards,

Michael




Theme © iAndrew 2016 - Forum software by © MyBB