global $module_name, $lang_module, $module_data;
Block module sẽ dùng được các tài nguyên của module đó, do đó có thể sửa dụng Xtemplate, truy vấn CSDL…. Cho block.
Tất cả nội dung của block được hiển thị cần phải lưu vào biến $content.
Một block hoàn chỉnh sẽ có dạng:
sqlreset()
->select('catid, title, alias, download_hits')
->from(NV_MOD_TABLE)
->where('status=1')
->order('download_hits DESC')
->limit(5);
$result = $db->query($db->sql());
$xtpl = new XTemplate('block_topdownload.tpl', $path);
$xtpl->assign('LANG', $lang_module);
$i = 1;
while ($row = $result->fetch()) {
$catalias = $list_cats[$row['catid']]['alias'];
$row['link'] = NV_BASE_SITEURL . 'index.php?' . NV_LANG_VARIABLE . '=' . NV_LANG_DATA . '&' . NV_NAME_VARIABLE . '=' . $module_name . '&' . NV_OP_VARIABLE . '=' . $catalias . '/' . $row['alias'] . $global_config['rewrite_exturl'];
$row['order'] = $i;
$xtpl->assign('loop', $row);
$xtpl->parse('main.loop');
++$i;
}
$xtpl->parse('main');
$content = $xtpl->text('main');
===== Viết block global =====
Trước khi viết block global cần xác định đặt block ở đâu? Block của module hay block của giao diện.
Tiếp theo cần xác định block có phần cấu hình hoặc ngôn ngữ block không?
Nếu block có phần cấu hình cần tạo thêm file ini, file này có dạng:
Block Groups
VinaDes.,Jsc
http://vinades.vn
0
5
1
bottom
150
nv_block_config_news_groups
nv_block_config_news_groups_submit
Trong đó cần quan tâm đến
1
5
1
Phần này là các biến để thiết lập block.
Trong đó biến $lang_translator không nên chỉnh sửa, biến $lang_block gồm nội dung của ngôn ngữ block.
Sau khi tạo thêm các thành phần trên, ta tiến hành viết nội dung chính của block.
Tạo file block và đặt vào vị trí thích hợp.
Vì block có thể dùng nhiều lần trên một trang do đó cần kiểm tra xem một block đã được dùng chưa? Việc kiểm tra sẽ tránh tình trạng lặp đi lặp lại các phần trong block.
Ví dụ:
if (!nv_function_exists('nv_news_blocks')) {
function nv_news_blocks($block_config) {
// Some code here
}
}
Nội dung của block cần đặt trong một function, theo ví dụ trên đó là function nv_news_blocks.
Vì nội dung được đặt trong function nên để sử dụng các biến dùng chung cũng cần gọi biến global.
Nếu block có cấu hình, thêm tiếp hai function cấu hình block.
Ví dụ:
function nv_block_config_news_groups($module, $data_block, $lang_block)
{
global $nv_Cache, $site_mods;
$html_input = '';
$html = '';
$html .= '';
$html .= '' . $lang_block['blockid'] . ' ';
$html .= '';
$html .= $html_input;
$html .= '';
$html .= ' ';
$html .= '';
$html .= '' . $lang_block['numrow'] . ' ';
$html .= ' ';
$html .= ' ';
$html .= '';
$html .= '' . $lang_block['showtooltip'] . ' ';
$html .= '';
$html .= '
';
$tooltip_position = array( 'top' => $lang_block['tooltip_position_top'], 'bottom' => $lang_block['tooltip_position_bottom'], 'left' => $lang_block['tooltip_position_left'], 'right' => $lang_block['tooltip_position_right'] );
$html .= '' . $lang_block['tooltip_position'] . ' ';
$html .= ' ' . $lang_block['tooltip_length'] . ' ';
$html .= ' ';
$html .= ' ';
return $html;
}
function nv_block_config_news_groups_submit($module, $lang_block)
{
global $nv_Request;
$return = array();
$return['error'] = array();
$return['config'] = array();
$return['config']['blockid'] = $nv_Request->get_int('config_blockid', 'post', 0);
$return['config']['numrow'] = $nv_Request->get_int('config_numrow', 'post', 0);
$return['config']['showtooltip'] = $nv_Request->get_int('config_showtooltip', 'post', 0);
$return['config']['tooltip_position'] = $nv_Request->get_string('config_tooltip_position', 'post', 0);
$return['config']['tooltip_length'] = $nv_Request->get_string('config_tooltip_length', 'post', 0);
return $return;
}
Tiếp theo thêm function chính (nội dung block)
Ví dụ:
function nv_block_news_groups($block_config)
{
global $module_array_cat, $module_info, $site_mods, $module_config, $global_config, $nv_Cache, $db;
$module = $block_config['module'];
$show_no_image = $module_config[$module]['show_no_image'];
$blockwidth = $module_config[$module]['blockwidth'];
$db->sqlreset()
->select('t1.id, t1.catid, t1.title, t1.alias, t1.homeimgfile, t1.homeimgthumb,t1.hometext,t1.publtime')
->from(NV_PREFIXLANG . '_' . $site_mods[$module]['module_data'] . '_rows t1')
->join('INNER JOIN ' . NV_PREFIXLANG . '_' . $site_mods[$module]['module_data'] . '_block t2 ON t1.id = t2.id')
->where('t2.bid= ' . $block_config['blockid'] . ' AND t1.status= 1')
->order('t2.weight ASC')
->limit($block_config['numrow']);
$list = $nv_Cache->db($db->sql(), '', $module);
if (! empty($list)) {
if (file_exists(NV_ROOTDIR . '/themes/' . $global_config['module_theme'] . '/modules/news/block_groups.tpl')) {
$block_theme = $global_config['module_theme'];
} else {
$block_theme = 'default';
}
$xtpl = new XTemplate('block_groups.tpl', NV_ROOTDIR . '/themes/' . $block_theme . '/modules/news');
$xtpl->assign('NV_BASE_SITEURL', NV_BASE_SITEURL);
$xtpl->assign('TEMPLATE', $block_theme);
foreach ($list as $l) {
$l['link'] = NV_BASE_SITEURL . 'index.php?' . NV_LANG_VARIABLE . '=' . NV_LANG_DATA . '&' . NV_NAME_VARIABLE . '=' . $module . '&' . NV_OP_VARIABLE . '=' . $module_array_cat[$l['catid']]['alias'] . '/' . $l['alias'] . '-' . $l['id'] . $global_config['rewrite_exturl'];
if ($l['homeimgthumb'] == 1) {
$l['thumb'] = NV_BASE_SITEURL . NV_FILES_DIR . '/' . $site_mods[$module]['module_upload'] . '/' . $l['homeimgfile'];
} elseif ($l['homeimgthumb'] == 2) {
$l['thumb'] = NV_BASE_SITEURL . NV_UPLOADS_DIR . '/' . $site_mods[$module]['module_upload'] . '/' . $l['homeimgfile'];
} elseif ($l['homeimgthumb'] == 3) {
$l['thumb'] = $l['homeimgfile'];
} elseif (! empty($show_no_image)) {
$l['thumb'] = NV_BASE_SITEURL . $show_no_image;
} else {
$l['thumb'] = '';
}
$l['blockwidth'] = $blockwidth;
$l['hometext'] = nv_clean60($l['hometext'], $block_config['tooltip_length'], true);
if (! $block_config['showtooltip']) {
$xtpl->assign('TITLE', 'title="' . $l['title'] . '"');
}
$xtpl->assign('ROW', $l);
if (! empty($l['thumb'])) {
$xtpl->parse('main.loop.img');
}
$xtpl->parse('main.loop');
}
if ($block_config['showtooltip']) {
$xtpl->assign('TOOLTIP_POSITION', $block_config['tooltip_position']);
$xtpl->parse('main.tooltip');
}
$xtpl->parse('main');
return $xtpl->text('main');
}
}
Lưu ý:
* Đối với block global cần chú ý biến $block_config đây là biến chứa cấu hình của block, tên module chứa block, tên block.
array (
'blockid' => 2,
'numrow' => 5,
'bid' => '120',
'module' => 'news',
'block_name' => 'global.block_blocknews',
)
* Cần chú ý đến biến ''$module_name, $module_data, $module_file''. Vì block global được dùng trên toàn bộ site, đó mỗi module giá trị các biến trên sẽ thay đổi. Cần thay ''$module_name'' bằng ''$block_config[‘module’], $site_mods[$block_config[‘module’]][‘module_data’], $site_mods[$block_config[‘module’]][‘module_file’]''.