You are here

public function InstapageCmsPluginViewModel::fetch in Instapage plugin 8.3

Same name and namespace in other branches
  1. 7.3 core/models/InstapageCmsPluginViewModel.php \InstapageCmsPluginViewModel::fetch()

Renders the templates.

Parameters

array $templates. List of templates to render. If it's null, last user template will be rendered.:

Return value

string Rendered template content.

Throws

Excetion If $templates is null and no templates were used before.

Exception If no template file is found.

File

core/models/InstapageCmsPluginViewModel.php, line 110

Class

InstapageCmsPluginViewModel
Class responsible for displaying a template files.

Code

public function fetch($templates = null) {
  $templates = $templates ? $templates : $this->templates;
  if (!$templates || empty($templates)) {
    throw new Exception("Templates can not be null.");
  }
  if (!is_array($templates)) {
    $templates = array(
      $templates,
    );
  }
  foreach ($templates as $template) {
    if (!file_exists($template)) {
      throw new Exception("Template {$template} not found.");
    }
    if ($this->templateData) {
      foreach ($this->templateData as $variableName => $variableValue) {
        ${$variableName} = $variableValue;
        unset($variableName);
        unset($variableValue);
      }
    }
    ob_start();
    include $template;
    $contents = ob_get_contents();
    ob_end_clean();
  }
  return $contents;
}