/** * Renders block content * * @static * * @param array $block Block data for rendering content * @param array $params Parameters of rendering: * * use_cache - Whether to use cache * * parse_js - Whether to move inline JS of the block to the bottom of the page * * @return string HTML code of rendered block content */ public static function renderBlockContent($block, $params = array()) { $default_params = array('use_cache' => true, 'parse_js' => true); $params = array_merge($default_params, $params); // Do not render block if it disabled in the frontend if (isset($block['is_disabled']) && $block['is_disabled']) { return ''; } $smarty = \Tygh::$app['view']; $smarty_original_vars = $smarty->getTemplateVars(); $display_this_block = true; self::_assignBlockSettingsToTemplate($block); // Assign block data from DB $smarty->assign('block', $block); $theme_path = self::getCustomerThemePath(); $block_schema = SchemesManager::getBlockScheme($block['type'], array()); $grid_id = empty($block['grid_id']) ? 0 : $block['grid_id']; $cache_key = "block_content_{$block['block_id']}_{$block['snapping_id']}_{$block['type']}_{$grid_id}"; if (!empty($block['object_id']) && !empty($block['object_type'])) { $cache_key .= "_{$block['object_id']}_{$block['object_type']}"; } $cache_this_block = $params['use_cache'] && self::allowCache(); if ($cache_this_block && isset($block['content']['items']['filling']) && isset($block_schema['content']['items']['fillings'][$block['content']['items']['filling']]['disable_cache'])) { $cache_this_block = !$block_schema['content']['items']['fillings'][$block['content']['items']['filling']]['disable_cache']; } /** * Determines flags for Cache * * @param array $block Block data * @param string $cache_key Generated name of cache * @param array $block_schema Block schema * @param bool $cache_this_block Flag to register cache * @param bool $display_this_block Flag to display block */ fn_set_hook('render_block_register_cache', $block, $cache_key, $block_schema, $cache_this_block, $display_this_block); if ($cache_this_block) { // We need an extra data to cache Inline JavaScript $smarty->assign('block_cache_name', $cache_key); // Check whether cache was registered successfully $cache_this_block = self::registerBlockCacheIfNeeded($cache_key, $block_schema, $block); } else { $smarty->clearAssign('block_cache_name'); } $smarty->assign('block_rendering', true); $smarty->assign('block_parse_js', $params['parse_js']); // We should load only when cache record exists $load_block_from_cache = $cache_this_block && Registry::isExist($cache_key); $block_content = ''; // Block content is found at cache and should be loaded out of there if ($load_block_from_cache) { $cached_content = Registry::get($cache_key); $block_content = $cached_content['content']; if (!empty($cached_content['javascript'])) { $repeat = false; $smarty->loadPlugin('smarty_block_inline_script'); smarty_block_inline_script(array(), $cached_content['javascript'], $smarty, $repeat); } Debugger::blockFoundAtCache($block['block_id']); } else { if ($block['type'] == Block::TYPE_MAIN) { $block_content = self::_renderMainContent(); } else { $title = $block['name']; if (Registry::get('runtime.customization_mode.live_editor')) { $le_block_types = fn_get_schema('customization', 'live_editor_block_types'); if (!empty($le_block_types[$block['type']]) && !empty($le_block_types[$block['type']]['name'])) { $title = sprintf('<span data-ca-live-editor-obj="block:name:%s">%s</span>', $block['block_id'], $title); } } $smarty->assign('title', $title); if (!empty($block_schema['content'])) { $all_values_are_empty = true; foreach ($block_schema['content'] as $template_variable => $field) { /** * Actions before render any variable of block content * * @param string $template_variable name of current block content variable * @param array $field Scheme of this content variable from block scheme content section * @param array $block_schema block scheme * @param array $block Block data */ fn_set_hook('render_block_content_pre', $template_variable, $field, $block_schema, $block); $value = self::getValue($template_variable, $field, $block_schema, $block); if ($all_values_are_empty && !empty($value)) { $all_values_are_empty = false; } $smarty->assign($template_variable, $value); } // We shouldn't display block which content variables are all empty $display_this_block = $display_this_block && !$all_values_are_empty; } // Assign block data from scheme $smarty->assign('block_scheme', $block_schema); if ($display_this_block && file_exists($theme_path . $block['properties']['template'])) { $block_content = $smarty->fetch($block['properties']['template']); } } if (!empty($block['wrapper']) && file_exists($theme_path . $block['wrapper']) && $display_this_block) { $smarty->assign('content', $block_content); if ($block['type'] == Block::TYPE_MAIN) { $smarty->assign('title', !empty(\Smarty::$_smarty_vars['capture']['mainbox_title']) ? \Smarty::$_smarty_vars['capture']['mainbox_title'] : '', false); } $block_content = $smarty->fetch($block['wrapper']); } else { $smarty->assign('content', $block_content); $block_content = $smarty->fetch('views/block_manager/render/block.tpl'); } fn_set_hook('render_block_content_after', $block_schema, $block, $block_content); // Save block contents to cache if ($cache_this_block && $display_this_block) { $cached_content = Registry::get($cache_key); $cached_content['content'] = $block_content; Registry::set($cache_key, $cached_content); } } $wrap_id = $smarty->getTemplateVars('block_wrap'); $smarty->clearAllAssign(); $smarty->assign($smarty_original_vars); // restore original vars \Smarty::$_smarty_vars['capture']['title'] = null; if ($display_this_block == true) { if (!empty($wrap_id)) { $block_content = '<div id="' . $wrap_id . '">' . $block_content . '<!--' . $wrap_id . '--></div>'; } return trim($block_content); } else { return ''; } }