public function apply_layout()
 {
     $layout = $this->_layout->to_php();
     $html = '';
     $html_layout = '';
     if ($this->_debugger->is_active(Upfront_Debug::MARKUP)) {
         $html = "<!-- Code generated by Upfront core -->\n";
         $name = !empty($layout['name']) ? $layout['name'] : '';
         $html .= "<!-- Layout Name: {$name} -->\n";
     }
     $layout_view = new Upfront_Layout_View($layout);
     $region_markups = array();
     $region_markups_before = array();
     $region_markups_after = array();
     $container_views = array();
     // Construct container views first
     foreach ($layout['regions'] as $region) {
         $container = empty($region['container']) ? $region['name'] : $region['container'];
         if ($container == $region['name']) {
             $container_views[$container] = new Upfront_Region_Container($region);
         }
     }
     // Iterate through regions
     foreach ($layout['regions'] as $region) {
         $region_view = new Upfront_Region($region);
         $region_sub = $region_view->get_sub();
         $markup = $region_view->get_markup();
         $container = $region_view->get_container();
         if (!isset($region_markups[$container])) {
             $region_markups[$container] = '';
         }
         if (!isset($region_markups_before[$container])) {
             $region_markups_before[$container] = '';
         }
         if (!isset($region_markups_after[$container])) {
             $region_markups_after[$container] = '';
         }
         if ($region_sub == 'top' || $region_sub == 'bottom') {
             if (isset($container_views[$container])) {
                 $type = $container_views[$container]->get_entity_type();
                 if ($type != 'full') {
                     continue;
                 }
                 // Don't add top/bottom sub container if it's not full
             }
             $sub_container = new Upfront_Region_Sub_Container($region);
             $markup = $sub_container->wrap($markup);
             if ($region_sub == 'top') {
                 $region_markups_before[$container] .= $markup;
             } else {
                 $region_markups_after[$container] .= $markup;
             }
         } else {
             if ($region_sub == 'fixed') {
                 $region_markups_after[$container] .= $markup;
             } else {
                 if ($region_sub == 'left') {
                     $region_markups[$container] = $markup . $region_markups[$container];
                 } else {
                     $region_markups[$container] .= $markup;
                 }
             }
         }
     }
     foreach ($container_views as $container => $container_view) {
         $type = $container_view->get_entity_type();
         $html_layout .= $container_view->wrap($region_markups[$container], $region_markups_before[$container], $region_markups_after[$container]);
     }
     $html .= $layout_view->wrap($html_layout);
     if ($this->_debugger->is_active(Upfront_Debug::MARKUP)) {
         $html .= "<!-- Upfront layout end -->\n";
     }
     do_action('upfront-layout-applied', $layout);
     return $html;
 }