Created
October 23, 2012 14:42
-
-
Save jmather/3939165 to your computer and use it in GitHub Desktop.
Use multiple templates dependent on host name in SF1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| modules/*/config/view.yml: | |
| class: dcmsViewConfigHandler |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| class myViewConfigHandler extends sfViewConfigHandler | |
| { | |
| /** | |
| * Adds a layout statement statement to the data. | |
| * | |
| * @param string $viewName The view name | |
| * | |
| * @return string The PHP statement | |
| */ | |
| protected function addLayout($viewName = '') | |
| { | |
| // true if the user set 'has_layout' to true or set a 'layout' name for this specific action | |
| $hasLocalLayout = isset($this->yamlConfig[$viewName]['layout']) || (isset($this->yamlConfig[$viewName]) && array_key_exists('has_layout', $this->yamlConfig[$viewName])); | |
| // the layout value | |
| $layout = $this->getConfigValue('has_layout', $viewName) ? $this->getConfigValue('layout', $viewName) : false; | |
| // the user set a decorator in the action | |
| $data = <<<EOF | |
| if (null !== \$layout = sfConfig::get('symfony.view.'.\$this->moduleName.'_'.\$this->actionName.'_layout')) | |
| { | |
| \$this->setDecoratorTemplate(false === \$layout ? false : \$layout.\$this->getExtension()); | |
| } | |
| EOF; | |
| if ($hasLocalLayout) | |
| { | |
| // the user set a decorator in view.yml for this action | |
| // This is the important part | |
| $data .= <<<EOF | |
| else | |
| { | |
| \$site = \$_SERVER['HTTP_HOST']; | |
| \$mlayout = \$site; | |
| \$this->setDecoratorTemplate('' == \$mlayout ? false : \$mlayout.\$this->getExtension()); | |
| } | |
| EOF; | |
| } | |
| else | |
| { | |
| // no specific configuration | |
| // set the layout to the 'all' view.yml value except if: | |
| // * the decorator template has already been set by "someone" (via view.configure_format for example) | |
| // * the request is an XMLHttpRequest request | |
| $data .= <<<EOF | |
| else if (null === \$this->getDecoratorTemplate() && !\$this->context->getRequest()->isXmlHttpRequest()) | |
| { | |
| \$site = \$_SERVER['HTTP_HOST']; | |
| \$mlayout = \$site; | |
| \$this->setDecoratorTemplate('' == \$mlayout ? false : \$mlayout.\$this->getExtension()); | |
| } | |
| EOF; | |
| } | |
| return $data; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment