Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Template within a template/Partials
  • Hello there,

    I'm struggling to find the best way to structure templates for a modal box that works whether Javascript is enabled or not. Let me explain:

    I have a partial for a modal box, let's call it _modal.php.

    Inside a controller that extends Controller_Template containing the logic for the modal.

    If this input is from an ajax call then I just want to respond with the partial and nothing else and the JS displays it as a nice modal.

    If it's not ajax then I assume it's not from a Javascript call so I have to put a bit more display around the modal to make it look nice within the template.

    At the moment for each modal box I have to create two template files, the partial and then an extra one that displays the partial with a bit more around it. Is there a way to remove this extra template, perhaps by having a template within a template?

    Here's some code examples to hopefully make that make more sense:

    Here's is the modal controller that extends Controller_Template
    [code]
    if(Input::is_ajax())
    {
    $this->template = View::forge('_modal');
    }
    else
    {
    $this->template->content = View::forge('modal');
    }
    [/code]

    Here's is modal.php view

    [code]
    <div class="container">
    <div class="row">

    <div class="modal modal-static">
    <?php echo View::forge('_modal'); ?>
    </div>

    </div>
    </div>
    [/code]

    This might be okay if my app only had one modal, but it has a few and it's getting messy. Perhaps using Controller_Template isn't appropriate, or perhaps I need to use a ViewModel which has the is_ajax() call in it?

    Any ideas?

    Thanks

    Mike
  • Seems like it should not matter how many modal variants you have since they are just individual files in their perspective folder(s)

    What does your action look like for rendering a single model?
  • I have ended up with two files for each modal, a partial (used by the JS) and then a full template that uses that partial (used when there's JS).

    Say I have two modals, one for login, one for register.

    I have a view template.php which contains:

    <html>
    <body>
    ...
    <?php echo $content ?>
    ...
    <div id="modal"></div>
    </body>
    </html>

    I have a controller, call it auth.php:

    <?php

    class Controller_Auth extends Controller_Template
    {
    public function action_login()
    {
    if(Input::is_ajax())
    {
    $this->template = View::forge('_login');
    }
    else
    {
    $this->template->content = View::forge('login');
    }
    }

    public function action_register()
    {
    if(Input::is_ajax())
    {
    $this->template = View::forge('_register');
    }
    else
    {
    $this->template->content = View::forge('register');
    }
    }
    }

    Where's login.php would contain:

    <div class="container">
    <div class="row">

    <div class="modal modal-static">
    <?php echo View::forge('_login'); ?>
    </div>

    </div>
    </div>

    register.php would be:

    <div class="container">
    <div class="row">

    <div class="modal modal-static">
    <?php echo View::forge('_register'); ?>
    </div>

    </div>
    </div>

    And _login.php and _register.php contain the contain the forms.

    Does that make more sense?

  • daminatedaminate
    Accepted Answer
    Are you re-using the _login and _register forms in multiple locations?

    Overall it makes sense, you might want to create a new "base template" controller that you use and move your is_ajax() logic into an after() function in it, that way you wont have to have that if/else in every controller action that might be ajax html response or full page load
  • Hi Daminate

    No they're not used in multiple locations.

    That's interesting though, it does seem like a much better place for the if/else, I'll have a play around with the after method.

    I might have to write my own Controller_Template, the template is pre-loaded in the before() so it doesn't seem possible to switch between templates depending on what happens in your controller logic.

    Thanks for your help.
  • Since I am using Theme() for my rendering my stuff is little different, but I ended up creating a new Controller_Template extends Controller_Hybrid and all of my controllers extend my custom one.  I am still figuring out some weirdness after following the fueldepot project example, but leveraging the before() and after() hooks in my base custom controller to handle proper partial logic has worked well so far

    If you run into more issues/questions post up some examples, will do my best to suggest some direction to take or examples to look at
  • Hi

    I finally sorted this in a round about way, it's still not ideal but it works for now. I created my own Controller_Template with this in the before:

    public function before()
    {
    if(Input::is_ajax())
    {
    //This is ajax load it in a modal instead
    $this->template = \View::forge('modal');
    }
    else
    {
    if ( ! empty($this->template) and is_string($this->template))
    {
    // Load the template
    $this->template = \View::forge($this->template);
    }
    }

    return parent::before();
    }

    I had to change around my modal templates so they didn't require the extra template and just relied on this.

    I'll take a look at the Themes class.

    Thanks for your help, it was very much appreciated.

Howdy, Stranger!

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

In this Discussion