Example #1
0
 /**
  * @param HaploTemplate $template
  * @param array $escapeTypes
  */
 public function assignToTemplate(HaploTemplate $template, array $escapeTypes = [])
 {
     $this->nonce = $this->app->nonce->get();
     foreach ($this->getPublicProperties() as $object) {
         $property = $object->name;
         if (array_key_exists($property, $escapeTypes)) {
             $template->set($property, $this->{$property}, array('escapeMethod' => $escapeTypes[$property]));
         } else {
             $template->set($property, $this->{$property}, array('escapeMethod' => 'escapeAttr'));
         }
     }
 }
 /**
  * Render template
  *
  * @throws HaploTemplateNotFoundException
  * @return string
  */
 public function render()
 {
     $output = '';
     // looping rather than using extract because we need to determine the value type before assigning
     foreach ($this->vars as $key => &$value) {
         // is this variable a reference to a sub-template
         if ($value instanceof HaploTemplate) {
             // pass variables from parent to sub-template but don't override variables in sub-template
             // if they already exist as they are more specific
             foreach ($this->vars as $subKey => $subValue) {
                 if (!$subValue instanceof HaploTemplate && !array_key_exists($subKey, $value->vars)) {
                     $value->vars[$subKey] = $subValue;
                 }
             }
             // display sub-template and assign output to parent variable
             ${$key} = $value->render();
         } else {
             ${$key} = $value;
         }
     }
     // use output buffers to capture data from require statement and store in variable
     ob_start();
     $path = $this->filePath . '/Templates/' . $this->filename;
     if (file_exists($path)) {
         require $path;
     } else {
         throw new HaploTemplateNotFoundException("Template ({$this->filename}) doesn't exist.");
     }
     $output .= ob_get_clean();
     while ($inherit = array_pop($this->inherits)) {
         $parent = new HaploTemplate($this->app, $inherit);
         $parent->vars = $this->vars;
         $parent->regionNames = $this->regionNames;
         $parent->regions = $this->regions;
         $output = $parent->render();
     }
     // process content against defined post filters
     foreach ($this->postFilters as $postFilter) {
         $output = $postFilter($output);
     }
     return $output;
 }