0

I am building a cms on top of Zend, just to practice and for fun. I would like to be able to store the layout scripts and view scripts in the database, and retrieve them from there, so they are easily editable from within my CMS. Could someone point me in the right direction? What I do now is this:

// Disable view
        $this->_helper->viewRenderer->setNoRender(true);
        $this->_helper->layout()->disableLayout();

    $pageDB = new Application_Model_DbTable_Page();
    $page = $pageDB->fetch($identifier);

         // Display the page or a 404 error
        if ($page !== null) {
            $this->view->headTitle($page->title);

            // Get the layout from the DB
            $layoutDB = new Application_Model_DbTable_Layout();
            $layout = $layoutDB->fetch($page->layout);

            $layout = str_replace('{LCMS:title}', $page->title, $layout->content);
            $layout = str_replace('{LCMS:content}', $page->content, $layout);

            $this->getResponse()->setBody($layout);
        } else {
            $this->_forward('notfound', 'error');
        }

But this obviously means I lose all the advantages of Zend in rega

rbnvrw
  • 347
  • 3
  • 15

1 Answers1

2

I think a better approche would be to have your CMS code write a versioned layout script on each change to file. Then set the appropriate layout script for the application from the database.

I would still store all the code in the database for backup purposes and to load for editing, but write it out to file when you are finished editing it.

Layout Database Table

| id | layout | version | filename | content |
  • layout has the identifier for the page.
  • version is an autoincrementer that updates on each change
  • filename is [layout]-[version]
  • content is the content...

When you save to this table. Write the content to a file in application/layout/[layout]-[version].phtml

Then use this pseudocode in your bootstrap to load the page you created in your CMS.

Bootstrap.php

public function _initLayout() {
    $layoutDB = new Application_Model_DbTable_Layout();
    $layout = $layoutDB->fetch($page->layout);
    Zend_Layout::getMvcInstance()->setLayout($layout->filename);
}

This way you can keep all the server side scripting inside the layout file and make use of the placeholders component rather than str_replace

Jamie Sutherland
  • 2,760
  • 18
  • 19
  • But Zend_Layout::getMvcInstance()->setLayout($layout); searches for a phtml file right? $layout is the result from the database, $layout->content contains what normally goes into the .phtml file. Is there a way to create a layout without using a phtml file? – rbnvrw Jul 03 '12 at 10:46
  • Not as far as I know. A quick glance at the Zend_View code, which is what Zend_Layout uses, it relies on the *include* php function which requires a file. I'm going to update my example to explain a little better what I'd do with the database. – Jamie Sutherland Jul 03 '12 at 11:04