/** * Renders a requested partial. * The framework uses this method internally. * @param string $partial The view to load. * @param array $parameters Parameter variables to pass to the view. * @param bool $throwException Throw an exception if the partial is not found. * @return mixed Partial contents or false if not throwing an exception. */ public function renderPartial($name, $parameters = [], $throwException = true) { $vars = $this->vars; /* * Alias @ symbol for :: */ if (substr($name, 0, 1) == '@') { $name = '::' . substr($name, 1); } /* * Process Component partial */ if (strpos($name, '::') !== false) { list($componentAlias, $partialName) = explode('::', $name); /* * Component alias not supplied */ if (!strlen($componentAlias)) { if ($this->componentContext !== null) { $componentObj = $this->componentContext; } elseif (($componentObj = $this->findComponentByPartial($partialName)) === null) { if ($throwException) { throw new CmsException(Lang::get('cms::lang.partial.not_found_name', ['name' => $partialName])); } else { return false; } } /* * Component alias is supplied */ } else { if (($componentObj = $this->findComponentByName($componentAlias)) === null) { if ($throwException) { throw new CmsException(Lang::get('cms::lang.component.not_found', ['name' => $componentAlias])); } else { return false; } } } $partial = null; $this->componentContext = $componentObj; /* * Check if the theme has an override */ if (strpos($partialName, '/') === false) { $overrideName = $componentObj->alias . '/' . $partialName; $partial = Partial::loadCached($this->theme, $overrideName); } /* * Check the component partial */ if ($partial === null) { $partial = ComponentPartial::loadCached($componentObj, $partialName); } if ($partial === null) { if ($throwException) { throw new CmsException(Lang::get('cms::lang.partial.not_found_name', ['name' => $name])); } else { return false; } } /* * Set context for self access */ $this->vars['__SELF__'] = $componentObj; } else { /* * Process theme partial */ if (($partial = Partial::loadCached($this->theme, $name)) === null) { if ($throwException) { throw new CmsException(Lang::get('cms::lang.partial.not_found_name', ['name' => $name])); } else { return false; } } } /* * Run functions for CMS partials only (Cms\Classes\Partial) */ if ($partial instanceof Partial) { $this->partialStack->stackPartial(); $manager = ComponentManager::instance(); foreach ($partial->settings['components'] as $component => $properties) { // Do not inject the viewBag component to the environment. // Not sure if they're needed there by the requirements, // but there were problems with array-typed properties used by Static Pages // snippets and setComponentPropertiesFromParams(). --ab if ($component == 'viewBag') { continue; } list($name, $alias) = strpos($component, ' ') ? explode(' ', $component) : [$component, $component]; if (!($componentObj = $manager->makeComponent($name, $this->pageObj, $properties))) { throw new CmsException(Lang::get('cms::lang.component.not_found', ['name' => $name])); } $componentObj->alias = $alias; $parameters[$alias] = $partial->components[$alias] = $componentObj; $this->partialStack->addComponent($alias, $componentObj); $this->setComponentPropertiesFromParams($componentObj, $parameters); $componentObj->init(); } CmsException::mask($this->page, 300); $parser = new CodeParser($partial); $partialObj = $parser->source($this->page, $this->layout, $this); CmsException::unmask(); CmsException::mask($partial, 300); $partialObj->onStart(); $partial->runComponents(); $partialObj->onEnd(); CmsException::unmask(); } /* * Render the partial */ CmsException::mask($partial, 400); $this->loader->setObject($partial); $template = $this->twig->loadTemplate($partial->getFullPath()); $result = $template->render(array_merge($this->vars, $parameters)); CmsException::unmask(); if ($partial instanceof Partial) { $this->partialStack->unstackPartial(); } $this->vars = $vars; return $result; }
public static function makeBaguetteGallery($images, $layout = "", $class = "") { // Set component properties if (empty($class)) { $class = self::$defaultClass; } if (empty($layout)) { $layout = self::$defaultLayout; } $parameters = ['layout' => $layout, 'class' => $class, 'images' => []]; foreach ($images as $key => $val) { $parameters['images'][$key] = new BaguetteImage($val, true); } // Get twig runtime $twig = App::make('twig.environment'); $partial = null; $result = ""; // Create an instance of the component $manager = ComponentManager::instance(); $component = $manager->makeComponent(self::$componentName, null, $parameters); $component->init(); $component->alias = self::$componentName; // Try first to find a partial from the theme $overrideName = $component->alias . '/' . $parameters['layout']; if (Filesystem::isFile(Theme::getActiveTheme()->getPath() . '/partials/' . $overrideName . '.htm')) { $partial = Partial::loadCached(Theme::getActiveTheme(), $overrideName); } // If not found we use one from the plugin if (is_null($partial)) { if (Filesystem::isFile(plugins_path() . '/nsrosenqvist/baguettegallery/components/baguettegallery/' . $parameters['layout'] . '.htm')) { $partial = ComponentPartial::loadCached($component, $parameters['layout']); } else { $partial = ComponentPartial::loadCached($component, self::$defaultLayout); } } // Render the component if (!is_null($partial) && !empty($images)) { $template = $twig->loadTemplate($partial->getFullPath()); $result = $template->render($parameters); } return $result; }
/** * Renders a requested partial. * The framework uses this method internally. * @param string $partial The view to load. * @param array $params Parameter variables to pass to the view. * @param bool $throwException Throw an exception if the partial is not found. * @return mixed Partial contents or false if not throwing an exception. */ public function renderPartial($name, $parameters = [], $throwException = true) { /* * Alias @ symbol for :: */ if (substr($name, 0, 1) == '@') { $name = '::' . substr($name, 1); } /* * Process Component partial */ if (strpos($name, '::') !== false) { list($componentAlias, $partialName) = explode('::', $name); /* * Component alias not supplied */ if (!strlen($componentAlias)) { if ($this->componentContext !== null) { $componentObj = $this->componentContext; } elseif (($componentObj = $this->findComponentByPartial($partialName)) === null) { if ($throwException) { throw new CmsException(Lang::get('cms::lang.partial.not_found', ['name' => $name])); } else { return false; } } } else { if (($componentObj = $this->findComponentByName($componentAlias)) === null) { if ($throwException) { throw new CmsException(Lang::get('cms::lang.component.not_found', ['name' => $componentAlias])); } else { return false; } } } $partial = null; $this->componentContext = $componentObj; /* * Check if the theme has an override */ if (strpos($partialName, '/') === false) { $overrideName = $componentObj->alias . '/' . $partialName; $partial = Partial::loadCached($this->theme, $overrideName); } /* * Check the component partial */ if ($partial === null) { $partial = ComponentPartial::loadCached($componentObj, $partialName); } if ($partial === null) { if ($throwException) { throw new CmsException(Lang::get('cms::lang.partial.not_found', ['name' => $name])); } else { return false; } } /* * Set context for self access */ $this->vars['__SELF__'] = $componentObj; } else { /* * Process theme partial */ if (($partial = Partial::loadCached($this->theme, $name)) === null) { if ($throwException) { throw new CmsException(Lang::get('cms::lang.partial.not_found', ['name' => $name])); } else { return false; } } } CmsException::mask($partial, 400); $this->loader->setObject($partial); $template = $this->twig->loadTemplate($partial->getFullPath()); $result = $template->render(array_merge($this->vars, $parameters)); CmsException::unmask(); $this->componentContext = null; return $result; }
/** * Renders a requested partial. * The framework uses this method internally. * @param string $partial The view to load. * @param array $params Parameter variables to pass to the view. * @param bool $throwException Throw an exception if the partial is not found. * @return mixed Partial contents or false if not throwing an exception. */ public function renderPartial($name, $parameters = [], $throwException = true) { $vars = $this->vars; /* * Alias @ symbol for :: */ if (substr($name, 0, 1) == '@') { $name = '::' . substr($name, 1); } /* * Process Component partial */ if (strpos($name, '::') !== false) { list($componentAlias, $partialName) = explode('::', $name); /* * Component alias not supplied */ if (!strlen($componentAlias)) { if ($this->componentContext !== null) { $componentObj = $this->componentContext; } elseif (($componentObj = $this->findComponentByPartial($partialName)) === null) { if ($throwException) { throw new CmsException(Lang::get('cms::lang.partial.not_found', ['name' => $name])); } else { return false; } } /* * Component alias is supplied */ } else { if (($componentObj = $this->findComponentByName($componentAlias)) === null) { if ($throwException) { throw new CmsException(Lang::get('cms::lang.component.not_found', ['name' => $componentAlias])); } else { return false; } } } $partial = null; $this->componentContext = $componentObj; /* * Check if the theme has an override */ if (strpos($partialName, '/') === false) { $overrideName = $componentObj->alias . '/' . $partialName; $partial = Partial::loadCached($this->theme, $overrideName); } /* * Check the component partial */ if ($partial === null) { $partial = ComponentPartial::loadCached($componentObj, $partialName); } if ($partial === null) { if ($throwException) { throw new CmsException(Lang::get('cms::lang.partial.not_found', ['name' => $name])); } else { return false; } } /* * Set context for self access */ $this->vars['__SELF__'] = $componentObj; } else { /* * Process theme partial */ if (($partial = Partial::loadCached($this->theme, $name)) === null) { if ($throwException) { throw new CmsException(Lang::get('cms::lang.partial.not_found', ['name' => $name])); } else { return false; } } } /* * Run functions for CMS partials only (Cms\Classes\Partial) */ if ($partial instanceof Partial) { $manager = ComponentManager::instance(); foreach ($partial->settings['components'] as $component => $properties) { list($name, $alias) = strpos($component, ' ') ? explode(' ', $component) : [$component, $component]; if (!($componentObj = $manager->makeComponent($name, $this->pageObj, $properties))) { throw new CmsException(Lang::get('cms::lang.component.not_found', ['name' => $name])); } $componentObj->alias = $alias; $parameters[$alias] = $partial->components[$alias] = $componentObj; array_push($this->partialComponentStack, ['name' => $alias, 'obj' => $componentObj]); $this->setComponentPropertiesFromParameters($componentObj, $parameters); $componentObj->init(); $componentObj->onInit(); // Deprecated: Remove ithis line if year >= 2015 } CmsException::mask($this->page, 300); $parser = new CodeParser($partial); $partialObj = $parser->source($this->page, $this->layout, $this); CmsException::unmask(); CmsException::mask($partial, 300); $partialObj->onStart(); $partial->runComponents(); $partialObj->onEnd(); CmsException::unmask(); } /* * Render the parital */ CmsException::mask($partial, 400); $this->loader->setObject($partial); $template = $this->twig->loadTemplate($partial->getFullPath()); $result = $template->render(array_merge($this->vars, $parameters)); CmsException::unmask(); if ($partial instanceof Partial) { if ($this->partialComponentStack) { array_pop($this->partialComponentStack); } } $this->vars = $vars; $this->componentContext = null; return $result; }