Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Nesting Template Controller
  • Is it possible to use a template Controller within a Template controller? 

    I mean, I've created a simple Template Controller(let's call it A controller), as the doc shows. After, I followed a tutorial about scaffolding. After this second tutorial I have another 

    template controller (B controller, let's say) that allows me to create/edit/delete/show the content of a DB table called 'product'. After this, I tried to combine these two Template 

    Controller in order to have the footer and header (included in my first template controller) wrapping the 'product' managing. Aaaand... I just got stuck here. 

    I tried by making the B controller extending my A controller. This way I'd have the footer and header, but I'd lose half of the content displayed by B controller... 

    I'm sure this is something that I might achieve using fuelphp, But still I don't know how. 

    Any Ideas? 

    Thanks in advance
  • I'm not sure I understand what you're trying to do.

    You have "Controller_B extends Controller_A extend Controller_Template"? That should not be a problem.

    If both A and B need to add something to the template, you'll have to make sure B doesn't overwrite content from A (or the other way around).

    If you need more advanced features, you'll need the Theme class.
  • Firstly, thanks for your answer. Sorry if I didn't make myself clear. 

    Having a ""Controller_B extends Controller_A extend Controller_Template"?" is something that I tried and that didn't work as I expected.

    I'll try to explain it again posting some images too. I hope that'll help.

    I have a Controller A extending Controller_Template. This one:


    And, this is my Controller B, after running Oil scaffolding. Controller B also extends Controller_Template and looks like:


    I would like to have something like:


    nesting both templates, but I'm not sure about how to do it. My best try, by having a Controller_B extending Controller_A extending Controller_Template was:


    But this way, half of the second template is lost. I'd like to know wich is the right way to do what I want (if it's possible). Also, I'll take your advice and have a look at Theme class.

    I hope this time I have expressed myself clearly. 
    Thanks again :-)
  • That's not different from what I said.

    You need a base controller (your controller A) which sets up the page template, and fills in all static sections of the template, in the before() method of the base controller.

    Your app controller (controller B) extends your base controller, and needs to fill in the body section of your template in the action method being called.

    The problem you have is that your app controller now uses the standard generated code, which assumes it has full control over the template. It needs to fill in only the body section of the template. A standard scaffold controller will assign the body view to the 'content' property of the template. You will have to make sure this is the body section of the template.

    If you're saying the output is not correct, then you've setup your base controllers before method wrong, or your template layout is not correct.

    You will have exactly the same issue with Theme, so switching to that is not going to solve your coding problem.
  • Perhaps you should post the code of Controller A, B and your template (on http://snipr.it) so I can have a look at it and tell you where you've gone wrong?
  • Thanks again, Harro

    I've tried to follow your advice and now the page is shown as I was looking for:


    I like to think i'm using template controller the right way, but I'd like to have your opinion, 
    as fuelphp core developper.  If you could just take a quick look to my code, 
    just to be sure:

    This is my first Controller, extending Template_Controller:


    and its template:




    And here the code from my second controller, extending the first:


    also, its template:


    Thanks in advance.
  • Apart from your coding style, I think it looks ok. :-)

    At first glance, the only remark I have is about your after() method. Once you have called the parent::after(), your page template is wrapped into a response object and returned. So you should assign the content to the template before you call the parent.

    I this case it will work because it's an object and is passed by reference, so later modifications will work, but you should not rely on that.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion