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?
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
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.
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