示例#1
0
 private function parseContent($placeholder, $content)
 {
     $result = "";
     // If child is an HtmlBuilder object, just add it
     if (is_a($content, "Bootsole\\HtmlBuilder")) {
         $result = $content;
         // If it is an array, then attempt to load a template specified by @source or @template and construct a new HtmlBuilder object
     } else {
         if (is_array($content)) {
             $result = [];
             // Load template, if specified
             if (isset($content['@source'])) {
                 // read in source template
                 $child_template = $this->loadTemplate($content['@source']);
             } else {
                 if (isset($content['@template'])) {
                     $child_template = $content['@template'];
                 }
             }
             // Array must have a '@content' or '@array' field, or be an array of HtmlBuilder objects.
             if (isset($content['@content'])) {
                 // Simple content fields
                 $hb = new HtmlBuilder($content['@content']);
                 $hb->setTemplate($child_template);
                 $result = $hb;
             } else {
                 if (isset($content['@array'])) {
                     // Loop through array, creating new HtmlBuilder for each element.  They will be concatenated upon rendering.
                     $result = [];
                     foreach ($content['@array'] as $i => $row) {
                         $hb = new HtmlBuilder($row);
                         $hb->setTemplate($child_template);
                         $result[] = $hb;
                     }
                     return $result;
                 } else {
                     // Check that every element is an HtmlBuilder object.  They will be concatenated upon rendering.
                     foreach ($content as $i => $row) {
                         if (!is_a($row, "Bootsole\\HtmlBuilder")) {
                             throw new \Exception("The array assigned to placeholder '{$placeholder}' must contain a '@content' or '@array' field, or be an array of HtmlBuilder objects.");
                         }
                     }
                     $result = $content;
                     return $result;
                 }
             }
             // If it is a scalar, just add it
         } else {
             if (is_scalar($content)) {
                 $result = $content;
                 // Otherwise throw an exception
             } else {
                 throw new \Exception("The contents of '{$placeholder}' must be a scalar value, HtmlBuilder object, or subarray.");
             }
         }
     }
     return $result;
 }
 public function __construct($content = [], $template_file = null, $options = [])
 {
     // Load the specified template, or the default navbar template
     if ($template_file) {
         parent::__construct($content, $template_file, $options);
     } else {
         parent::__construct($content, null, $options);
         parent::setTemplate("<li class='{{_active}} {{_display}}'><a role='menuitem' class='{{_css_classes}}' {{_data}} href='{{_url}}'>{{_label}}</a></li>");
     }
     if (isset($content['@label'])) {
         $this->label($content['@label']);
     }
     if (isset($content['@url'])) {
         $this->url($content['@url']);
     }
     // Initialize @display if passed in
     if (isset($content['@display'])) {
         $this->display($content['@display']);
     }
     // Initialize @active if passed in
     if (isset($content['@active'])) {
         $this->active($content['@active']);
     }
     if (isset($content['@data'])) {
         $this->dataAttributes($content['@data']);
     }
     if (isset($content['@css_classes'])) {
         $this->cssClasses($content['@css_classes']);
     }
 }