Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

show same content in modal and plain

hi,

to simplify things, I would like to show the same content sometimes in a modal, sometimes in a normal page.

how could I do that?

thanks!

sorry for my english..



7.3k
Accepted
answer

I have an application that does this; shows the same content from a view but sometimes as an AJAX request or as a full page.

I have a 'default' template in my layouts folder that has all the page HTML markup for a full page and an 'ajax' template that just contains {{ content() }} and nothing else. I have a BaseController that all my other controllers inherit.


    <?php

        use Phalcon\Mvc\Controller,
        Phalcon\Tag;

    class BaseController extends Controller {

        public function initialize() {
            if($this->request->isAjax()){
                $this->view->setTemplateAfter('ajax');
            } else {
                $this->view->setTemplateAfter('default');
            }
            Tag::setTitle('My Default Title!');
        }

        # Other code ...
    }

Then in my other controllers that require the same behaviour I call this parent init.


       class DashboardController extends BaseController {

              public function initialize() {
                  parent::initialize();
              }

              public function indexAction() {

              }
    }

This is not what is actually in my code as it's a bit long winded for me. If you would like more code elegance than this you could use a variable that controls the template used. At the top of my BaseController I have:


     class BaseController extends Controller {
       protected $_template;

        public function initialize() {
            $this->_template = ($this->request->isAjax() ? 'ajax' : 'default');

            $this->view->setTemplateAfter($this->_template);

            Tag::setTitle('My Default Title');
        }

The means I can inspect $this->_template within other controllers if need be.

I not did just as you did, but helped with new ideas

thank you