You are on page 1of 29

TUT JOOMLA 1.

5
I. Template

H th ng Joomla template bao g m


/templates /basic_tempalte /css template_css.css /images index.php template_thumbnail.png templateDetails.xml l nh ng ph n t i thi u c n t o m t template. L nh nhn i h i m i tn file ph i c tn tr ng nh th . Ch r ng trong khi khng c nh no hi n th trong th m c /images, n l n i c tr ng b n nn ci t b t c nh no ang h tr cho template, nh nh n n (backgrounds), banner, Ch c n ng cc file trong ph n template File index.php l file dn trang template (b no c a template) File template_css.css l css stylesheet cho template (trang nh ki u) File templateDetails.xmll file siu d li u (metadata) theo nh d ng XML File template_thumbnail.png l m t mn hnh (screenshot) thu nh c a template th ng kho ng r ng l 140pixel v cao 90 pixel. M t cu h i t ra l lm sao chng ta load c module vo template.

C php cu l nh Joomla
Cu l nh jdoc: B ng cch s d ng m t s cc i t ng do chnh mnh nh ngh a nh JDocument v JDocumentHTML. Joomla c th load cc n i dung c a cc module v component t database m ch ng h ph i s d ng nhi u m l nh PHP ph c t p trong file template, chng ta th y vi c ny em l i r t nhi u thu n l i cho cc designer. Ta th y trong tempalte c ph ng th c include trong i t ng jdoc tr v t t c thng tin c a header cho trang Joomla hi n t i. B n c ng c th s d ng 1 include g i n yu c u cc tin nh n h th ng hi n th . o n m hi n th nh n h th ng s trng nh th ny: <jdoc: include type = message/> Cu i cng l cch ph bi n nh t s d ng include chn vo cc o n m HTML cho 1 Joomla module. V d a ra thng tin c a ph n module top (th ng l module c ch c n ng hi n th cc banner) b n s d ng 1 c php nh sau: <jdoc:include type= module name = top style = xhtml/> Trn y l 1 vi v d c b n. tm hi u thm v jdoc statementhttp://docs.joomla.org/Jdoc_statements Bi n $thisngoi JDocumentHTML cn c r t nhi u thu c tnh v ph ng th c c nh ngh a s n v b n c th tham chi u n n b ng cu l nh$this->. Cu l nh $this c s d ng tham chi u n cc thu c tnh v ph ng th c bn trong c a i t ng JDocumentHTML. Khi s d ng cc thu c tnh v ph ng th c c a i t ng JDocumentHTML, n s gip ch cho b n trong vi c coding khng ph i vi dng, v cc i t ng ny u c Joomla nh ngh a s n, ch vi c g i ra v s d ng. V d , hi n th ngn ng c ch n l a cho template, thu c tnh language c th c trch xu t nh th ny: <?php echo $this->language?>

Sau y l cc thu c tnh khc c a i t ng JDocumentHTML c th c truy xu t thng qua s tham chi u c a bi n $this ny: - direction c ng d n t tri qua ph i (ltr), ph i sang tri (rtl), - template hi n th tn th m c template - title hi n th tiu c a website - description ch a cc miu t ti li u (t cc metadata HTML). - link ch a cc ng d n (URL) c a ti li u - language bao g m cc thi t l p ngn ng c a website. V JDocumentHTML c ng cung c p 1 s ph ng th c h u ch c th c g i ra thng qua vi c s d ng bi n $this ny. V d ph ng th c countModules() s gip b n ki m tra s t n t i c a cc module t i v tr c ph ng th c countModules() tham chi u n. C u trc c a n nh sau: $this->countModules(v tr c thi t l p module); V d , d ki m tra s l ng cc module t n t i 1 v tr ring l , b n c th th c thi 1 hm nh sau: $this->countModules(user1); C th s d ng hm ki m tra cc tr ng h p c nhi u h n 1 v tr nh th ny: $this->countModules(user1 + user2) Th m ch c th s d ng ton t Boolean logic khi th c thi ph ng th c nh sau: $this->countModules(user1 and user2); $this->countModules(user1 or user2); V d d i y l o n m ki m tra t i v tr top, c module no t n t i hay khng v n u khng s hi n th t m nh placeholder.gif thay th <?php if($this->countModules(top)):?> <?jdoc:include type = modules name = top/> <?php else:?> <img src= placeholder.gif> <?php endif;?> Thng th ng, khi thi t k template cho Joomla, chng ta s dng ph ng th c countModules() ki m tra xem t i v tr c t n t i module no c g i ra khng, n u khng th chng ta s n v tr i ( n cc th div). V d i n hnh nh t l 1 template 3 c t, c nh ng lc chng ta ch s d ng 2 c t, v l d nhin chng ta s n 1 c t i template hi n th khng b l i.

T o m t template Joomla v i cc tnh n ng c b n


B1: Thi t k template m u B2: Rp template vo Joomla B3: Tnh ch nh l i cc stylesheet cho ph h p B1: Thi t k template m u: u tin, tr c h t, chng ta khoan ng n Joomla, cng vi c u tin l thi t k 1 template m u. T o 1 th m c c tn l template_demo, trong th m c ny t o cc file y index.html y css/template.css y images/ Trong file index.html mnh vi t nh sau:
<!DOCTYPE html PUBLIC "//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD /xhtml1-transitional.dtd"> <html>

<head> <link rel="stylesheet" href="css/template.css" type="text/css" /> </head> <body> <div id="tva-container"> <div id="tva-header"> <a href="index.html"> <img src="/images/logo.png" /> </a> </div> <div id="tva-menu_bar"></div> <div id="content"> <div id="tva-left_nav"> LEFT COLUMN </div> <div id="tva-right_nav"> RIGHT COLUMN </div> <div id="tva-info"> CONTENT </div> </div> <div id="tva-footer">&copy 2009 Copyright By TuanVA</div> </div> </body> </html>

Ti p theo mnh

nh ngh a cho cc th div trong file css nh sau:

*{ margin:0; padding:0; } body{ font-family: Tahoma, Arial, sans-serif; color: #484848; font-size:12px; } #tva-container{ margin:0 auto; width:960px; } #tva-header{ background:#000000; height:100px; border:1px solid black; margin-bottom:1px; } #tva-header img{

float:left; } #tva-content{ } #tva-left_nav{ float:left; width:150px; background:#e8f6bf; } #tva-right_nav{ background:#e8f6bf; float: right; width: 150px; } #tva-info{ } #tva-footer{ line-height:20px; color:#FFFFFF; background:#000000; text-align:center; clear:both; } a img { border:medium none; }

Trong th m c \images, mnh t o ra cc t m hnh sau: bullet-list.gif, logo.png, pathway-bg.gif, seperator.gif. B c 2 Rp template vo joomlaby gi ta copy th m c template_demo vo th m c templates trong m ngu n joomla, ng th i t o thm 1 file c tn l templateDetails.xml khai bo cc thng tin v template. Hy nh k l file nh h ng tr c ti p n vi c ci t template. Sau i tn file index.html thnh index.php. Ti p t c chng ta m file templateDetail.xml ra v khai bo nh sau:
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE install PUBLIC "//Joomla! 1.5//DTD template 1.0//EN" "http://www.joomla.org/xml/dtd/1.5/te mplate-install.dtd"> <install version="1.5" type="template"> <name>Template Demo</name> <creationDate>06/08/09</creationDate> <author>TuanVA</author> <authorEmail>seaboyvt@yahoo.com</authorEmail> <authorUrl>http://www.chuaco.com</authorUrl> <copyright>TuanVA</copyright> <license>GNU/GPL</license> <version>1.0.0</version> <description>Demo template for a three column display.</description> <files> <filename>index.php</filename>

<filename>templateDetails.xml</filename> <filename>template_thumbnail.png</filename> <filename>params.ini</filename> <filename>images/bullet-list.gif</filename> <filename>images/logo.png</filename> <filename>images/pathway-bg.gif</filename> <filename>images/seperator.gif</filename> <filename>css/index.html</filename> <filename>css/template.css</filename> </files> <positions> <position>left</position> <position>right</position> <position>user4</position> <position>breadcrumbs</position> </positions> </install>

Nh l m i khi t o thm 1 file m i no, hay s d ng 1 position no b n c n m file ny v khai bo (c th khng c n). Sau khi khai bo xong vo admincp, ph n template manager, chng ta s th y 1 template c tn l template demo, do chng ta v a khai bo trong file templateDetails.xml. Hy th thi t l p n l m c nh v ra trang joomla coi th , s th y template m khng h c n i dung g c a h th ng Joomla c , by gi s thm n i dung vo file index.php b ng cch s d ng cc cu l nh c a Joomla. Khi thm n i dung vo file index.php i u c n nh l g i c 2 file css c a template h th ng Joomla vo, chng c tn l general.css v system.css v no trong \template\system\css.Vi t l i file index.php nh sau:
<?php /** * @TempName * @version * @author * @copyright * @license */ Template DEMO 1.0.0 09-06-2009 13:06:37 TuanVA Copyright (C) 2009 TuanVA http://www.gnu.org/licenses/gpl-2.0.html GNU/GPLv2 only

defined('_JEXEC') or die('Restricted access'); ?> <!DOCTYPE html PUBLIC "//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml 1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="<?php echo $this>language; ?>" lang="<?php echo $this->language; ?>" > <head> <jdoc:include type="head" /> <link rel="stylesheet" href="<?php echo $this>baseurl ?>/templates/system/css/system.css" type="text/css" /> <link rel="stylesheet" href="<?php echo $this>baseurl ?>/templates/system/css/general.css" type="text/css" /> <link rel="stylesheet" href="<?php echo $this>baseurl ?>/templates/<?php echo $this>template ?>/css/template.css" type="text/css" /> </head> <body>

<div id="tva-container"> <div id="tva-header"> <a href="index.php"> <img src="<?php echo $this>baseurl ?>/templates/<?php echo $this->template ?>/images/logo.png" /> </a> <?php if ($this->countModules('user4')):?> <jdoc:include type="modules" name="user4" style="raw" /> <?php endif; ?> </div> <div id="tva-menu_bar"></div> <div id="tva-content"> <?php if ($this->countModules('left')): ?> <div id="tva-left_nav"> <jdoc:include type="modules" name="left" style="xhtml" /> </div> <?php endif; ?> <?php if ($this->countModules('right')): ?> <div id="tva-right_nav"> <jdoc:include type="modules" name="right" style="xhtml" /> </div> <?php endif; ?> <div id="tva-info"> <div id="tvapathway"><strong>You are here:</strong> <jdoc:include type="module" name="bre adcrumbs" /></div> <jdoc:include type="component" /> </div> </div> <div id="tva-footer">&copy 2009 Copyright By TuanVA</div> <jdoc:include type=modules name=debug /> </div> </body> </html>

Gi i thch 1 vi cu l nh trong ny: defined(_JEXEC) or die(Restricted access); o n m ny s m b o r ng bi n _JEXEC c nh ngh a, n u khng, n s hi n th 1 o n thng bo h n ch truy c p. Cu l nh ny r t quan tr ng i v i s an ton c a Joomla. <jdoc:include type = modules name = left style = xhtml/> Cc position c khai bo b ng thu c tnh name c a i t ng jdoc. y cu l nh ny s hi n th cc module t i v tr left. L u v tr ny c nh ngh a dng cho vi c qu n l v tr c a cc module trong admincp, n khng mang ngh a l m t v tr hi n th c a layout, cn vi c module c hi n th t i u trn website l ph thu c v vi c ta b tr cu l nh jdoc g i n ra n u trong file index.php, thng th ng 1 s position nh left hay right c a jdoc, ng i ta s t n t ng ng v i v tr c thi t k trong index.php ti n cho ng i dng s d ng. <?php if($this->countModules(user4)):?> <jdoc:include type = modules name = user4 style = raw/> <?php endif;?> Ph n ny ki m tra v tr user4 c t n t i module no khng, n u c th hi n th , v n u khng th s n i.

T ng t ph ng th c countModules() s ki m tra xem c modules no c g i ra trong ph n c t tri khng, n u khng th c t tri s n i ( n c div left_nav i) B c 3 Tinh ch nh l i cc stylesheet cho ph h pv n thao tc stylesheet n i dung c a Joomla. i m c n ph i quan tm l lm sao thao tc cc v tr b c l y cc module v component, nh ng l i khng th thao tc layout c a chng. V joomla vi t theo c u trc MVC, layout c a cc module v com s n m ring trong th m c c a n, vi c ny s ti n cho vi c thm b t cc module c com m khng lm nh h ng n b c c c a template chnh. C 3 cch Cch 1: S d ng 1 Tool g i firebug trn FireFox, ho c l development tool trn IE. y l cng c h tr cc designer d cc stylesheet trn website khi n hi n th , nh v y s v tv h n chng ta ng i view source v d, tool ny s d gip designer tr chu t vo cc ph n trn website. Sau s vi t stylesheet cho chng trong file css Cch 2: Vo th ng trong th m c ch a template c a com hay module s a. nh ng y l cch khng nn s d ng. V nh v y layout c a module hay com ch nh s a ch ph h p v i template ang s a. s d ng v i cc template khc s b l i. y l 1 cch khng chuyn nghi p. V l i khai lm 1 template, mu n chia s khng c l ph i copy c ci com v module mang theo. Cch 3: Vi t l i template cho cc module v component, y g i l k thu t Template Override, 1 tnh n ng nng cao c a template joomla. S d ng cch 1. Tinh ch nh stylesheet ra sao l ty t ng designer. Mu n p mu n x u l do c m nh n c a t ng ng i. Dng tool d cc style c a module v com c n. Sau vi t thm vo css. V d
a, a:hover, a:active, a:focus { color:#0264B0; text-decoration:none; }

ul { list-style-image:none; list-style-type:none; } td, div { font-size:100%; } h1{ line-height:100%; margin-bottom:5px; } /*============ Custom MODULE AND COM Styles of Joomla =============*/ .componentheading { background:transparent url(../images/seperator.gif) repeat-x scroll center bottom; font-size:200%; font-weight:bold; line-height:normal; margin-bottom:10px; padding-bottom:5px; padding-left:50px; }

.moduletable_menu h3, .moduletable h3 { background:#000000; color:#FFFFFF; padding:10px; } .moduletable li{ margin-left:10px; } .moduletable_menu ul { margin: 0; padding: 0; } .moduletable_menu li { border-bottom: 1px solid #D5E8AD; list-style: none; } .moduletable_menu li a { background: #E8F6BF url(../images/bgnavigation.gif) repeat-x; color: #553; display: block; padding: 8px 4px 8px 12px; text-decoration: none; } .moduletable_menu li a:hover { background: #F0FFC6; color: #553; } .contentheading{ font-size:150%; color:#c8001e; font-weight:bold; line-height:normal; border-bottom: 1px solid black; } .small { color:#999999; } .createdate { color:#999999; line-height:10px; padding:2px 0 10px; } .category { font-weight:bold; } /* Clearfix */ .clearfix:after { clear: both; display: block; content: "."; height: 0;

visibility: hidden; } * html > body .clearfix { width: 100%; display: block; } * html .clearfix { height: 1%; } /* Search */ .search{ float:right; } /* PATHWAY ---------------------------------------------------------*/ #tva-pathway { padding: 7px 6px; display: block; background: url(../images/pathway-bg.gif) repeat-y top left #F2F2F2; font-size: 1em; line-height: normal; text-indent: 15px; } #tva-pathway img { margin: 0 5px 1px; } #tva-pathway strong { margin-right: 5px; } #tva-pathway a { padding-right:4px; } #tva-pathway a:hover, #tva-pathway a:active { text-decoration: none; }

By gi l k t qu c a th ng b n

K thu t nng cao trong thi t k Template


K thu t Template Parameters v Template Overrides Template Parameters: (c th d ch l thng s ) y l m t k thu t s d ng cc thng s param c khai bo trong file templateDetails.xml xy d ng cc ch n l a thng s trong admincp, tc ng thay i tr c ti p n template thng qua cc thu c tnh c a css b ng cch s d ng ph ng th c $this->params->get(thng s name c a param) g i ra s thay i c a thu c tnh n m trong layout template (v d cc id v class c a th div). V d lm m t thng s thay i cch hi n th r ng c a layout template. Tr c tin c n m file templateDetails.xml ra tm n dng. </install> Sau thm o n m <params> <params name = template_width type = radio default = 0 label = Template width description = Change width setting of template> <option value = 0> 800x600 </option> <option value = 0> 1024x756 </option> <option value = 0> Fluid (min/max with FF and IE7, 80% width IE6 </option> </params> </params> Gi i thch o n m. Khai bo thm th param, c u trc nh trn v i: y name l tn c a thng s param ta s s d ng (n s l gi tr ph ng th c $this>params->get() tham chi u n)

type l ki u ch n l a thng s (cc ki u c a n c ng gi ng nh s d ng cc type ch n l a trong th select c a form). y Label l tn c a param ny, n s c hi n th trong admincp y Default l ph n ch n l a m c nh, n tham chi u n gi tr value c a th option bn d i. y Decription l ph n miu t tnh n ng c a param ny n c ng c hi n th trong admincp. y Option l danh sch ch n l a (c ng gi ng nh s d ng trong th select c a form) Sau khi save l i file templateDetails.xml ngay t i th m c g c c a template, ti p t c t o thm m t file l params.ini (file ny template l u tr cc gi tr param c ng i dng thi t l p trong admincp, i u ch l ph i khai bo thm file ny vo trong file templateDetail.xml). Vo th m c admincp\template manager r i click vo template c a y

Ti p t c ta ph i vo trang index.php thi t l p ph ng th c $this->params->get() tham chi u n n trong th <body>. M file index.php ra, thay i <body> thnh <body class = width_<?php echo $this->params->get(template_width);?>> Cu i cng l xem xt thu c tnh div no b c l y ton b n i dung header, content, n footer c a chng ta, trong template c a mnh thi t k , l id tva-container. Thi t l p l i cc r ng khc nhau nh ghi trong thng s param. M file template.css ra, v thm vo o n sau:
/* SCREEN TOOL ---------------------------------------------------------*/ body.width_0 div#tva-container { width: 760px; } body.width_1 div#tva-container { width: 960px; } body.width_2 div#tva-container { min-width:760px; max-width:960px; width:auto !important; width:960px; }

C ch ho t ng:khi ta ch nh thng s trong admincp v save l i hm $this->params>get(template_width) s nh n c gi tr m c nh l 0 v s hi n th n, lc ny th <body class = width_0> v tham chi u n body.width_0 div#tva-container trong css, sau l p ny s thay th gi tr width cho id = tva-container c a template.

Template Overrides: k thu t ny l k thu t n p ch n template, ngh a l chng ta s t o ra cc template t ng ng v i cc templatec a module hay component v s d ng n load ln cc template m c nh, v d nhin cc template m c nh khng h b ch nh s a hay xa i. M c nh, cc template c a module hay component c t trong 1 folder c tn l tmpl, n m trong folder c a com hay module . V i com th c th n n m trong 1 th m c view n a, v cc com c a joomla c vi t theo m hnh MVC (controller model view) V d : chng ta s a template c a module login, u tin vo ng d n root\folder\modules\mod_login\tmpl xem tn c a file template module login, y n l file default.php sau t o ra 1 folder mod_login trong th m c html c a template, v t o 1 file c tn t ng ng l default.php trong th m c ny ch nh s a. Thng th ng ng i ta copy nguyn file template g c mang qua th m c trong html ch nh s a cho nhanh. Ch ph i s d ng k thu t ny thi t k l i 1 template c a com hay module m ch khi s d ng template chng ta, n m i c thay i, cn n u s d ng cc template khc, n v n bnh th ng. M t khi khng mu n s d ng chng, ch c n xa folder file trong th m c c a html template. H th ng joomla s t ng nh n di n l i file template n m trong th m c cc module hay com. V d : ch nh s a l i ph n tc gi (written by Administrator) v ngy thng c a bi vi t cho n hi n th xu ng d i dng last update thay v n m trn u

y xc nh ph n c n s a thu c component content, ph n frontpage. k thu t ny, ph i c 1 cht ki n th c v c u trc component v module c a Joomla c th hi u ph n c n ch nh s al com hay module g. Tr c tin t o m t th m c html trong folder root c a template chng, ti p t c t o cc th m c t ng ng nh sau: html\com_content\frontpage. Sau vo ng d n components\com_content\views\frontpage\tmpl, copy ton b cc file php v th m c html\com_content\frontpage. i u quan tr ng l ph i bi t c c u trc c a template article n s n m trong file no trong cc file copy. (c th dng firebug c a firefox). Sau khi tm ra v nh v c u l file template c a com ny, th m file ra, i n hnh l file default_item.php. N i dung c a file ny s bao g m nh sau:

<?php // no direct access defined('_JEXEC') or die('Restricted access'); $canEdit = ($this->user>authorize('com_content', 'edit', 'content', 'all') || $this->user>authorize('com_content', 'edit', 'content', 'own')); ?> <?php if ($this->item->state == 0) : ?> <div class="system-unpublished"> <?php endif; ?> <?php if ($canEdit || $this->item->params->get('show_title') || $this->item>params->get('show_pdf_icon') || $this->item->params>get('show_print_icon') || $this->item->params->get('show_email_icon')) : ?> <table class="contentpaneopen<?php echo $this->escape($this->item->params>get( 'pageclass_sfx' )); ?>"> <tr> <?php if ($this->item->params->get('show_title')) : ?> <td class="contentheading<?php echo $this->escape($this->item->params>get( 'pageclass_sfx' )); ?>" width="100%"> <?php if ($this->item->params->get('link_titles') && $this->item>readmore_link != '') : ?> <a href="<?php echo $this->item>readmore_link; ?>" class="contentpagetitle<?php echo $this->escape($this>item->params->get( 'pageclass_sfx' )); ?>"> <?php echo $this->escape($this->item->title); ?></a> <?php else : ?> <?php echo $this->escape($this->item->title); ?> <?php endif; ?> </td> <?php endif; ?> <?php if ($this->item->params->get('show_pdf_icon')) : ?> <td align="right" width="100%" class="buttonheading"> <?php echo JHTML::_('icon.pdf', $this->item, $this->item->params, $this>access); ?> </td> <?php endif; ?> <?php if ( $this->item->params->get( 'show_print_icon' )) : ?> <td align="right" width="100%" class="buttonheading"> <?php echo JHTML::_('icon.print_popup', $this->item, $this->item>params, $this->access); ?> </td> <?php endif; ?> <?php if ($this->item->params->get('show_email_icon')) : ?> <td align="right" width="100%" class="buttonheading"> <?php echo JHTML::_('icon.email', $this->item, $this->item>params, $this->access); ?> </td> <?php endif; ?> <?php if ($canEdit) : ?> <td> <?php echo JHTML::_('icon.edit', $this->item, $this->item>params, $this->access); ?> </td>

<?php endif; ?> </tr> </table> <?php endif; ?> <?php if (!$this->item->params->get('show_intro')) : echo $this->item->event->afterDisplayTitle; endif; ?> <?php echo $this->item->event->beforeDisplayContent; ?> <table class="contentpaneopen<?php echo $this->escape($this->item->params>get( 'pageclass_sfx' )); ?>"> <?php if (($this->item->params->get('show_section') && $this->item>sectionid) || ($this->item->params->get('show_category') && $this->item>catid)) : ?> <tr> <td> <?php if ($this->item->params->get('show_section') && $this->item>sectionid && isset($this->item->section)) : ?> <span> <?php if ($this->item->params->get('link_section')) : ?> <?php echo '<a href="'.JRoute::_(ContentHelperRoute::getSecti onRoute($this->item->sectionid)).'">'; ?> <?php endif; ?> <?php echo $this->escape($this->item->section); ?> <?php if ($this->item->params->get('link_section')) : ?> <?php echo '</a>'; ?> <?php endif; ?> <?php if ($this->item->params->get('show_category')) : ?> <?php echo ' - '; ?> <?php endif; ?> </span> <?php endif; ?> <?php if ($this->item->params->get('show_category') && $this->item>catid) : ?> <span> <?php if ($this->item->params->get('link_category')) : ?> <?php echo '<a href="'.JRoute::_(ContentHelperRoute::getCateg oryRoute($this->item->catslug, $this->item->sectionid)).'">'; ?> <?php endif; ?> <?php echo $this->escape($this->item->category); ?> <?php if ($this->item->params->get('link_category')) : ?> <?php echo '</a>'; ?> <?php endif; ?> </span> <?php endif; ?> </td> </tr> <?php endif; ?> <?php if (($this->item->params->get('show_author')) && ($this->item>author != "")) : ?> <tr> <td width="70%" valign="top" colspan="2"> <span class="small"> <?php JText::printf( 'Written by', ($this->escape($this->item>created_by_alias) ? $this->escape($this->item->created_by_alias) : $this>escape($this->item->author)) ); ?> </span>

&nbsp;&nbsp; </td> </tr> <?php endif; ?> <?php if ($this->item->params->get('show_create_date')) : ?> <tr> <td valign="top" colspan="2" class="createdate"> <?php echo JHTML::_('date', $this->item>created, JText::_('DATE_FORMAT_LC2')); ?> </td> </tr> <?php endif; ?> <?php if ($this->item->params->get('show_url') && $this->item->urls) : ?> <tr> <td valign="top" colspan="2"> <a href="http://<?php echo $this->escape($this->item>urls) ; ?>" target="_blank"> <?php echo $this->escape($this->item->urls); ?></a> </td> </tr> <?php endif; ?> <tr> <td valign="top" colspan="2"> <?php if (isset ($this->item->toc)) : ?> <?php echo $this->item->toc; ?> <?php endif; ?> <?php echo $this->item->text; ?> </td> </tr> <?php if ( intval($this->item->modified) != 0 && $this->item->params>get('show_modify_date')) : ?> <tr> <td colspan="2" class="modifydate"> <?php echo JText::sprintf('LAST_UPDATED2', JHTML::_('date', $this>item->modified, JText::_('DATE_FORMAT_LC2'))); ?> </td> </tr> <?php endif; ?> <?php if ($this->item->params->get('show_readmore') && $this->item>readmore) : ?> <tr> <td colspan="2"> <a href="<?php echo $this->item>readmore_link; ?>" class="readon<?php echo $this->escape($this->item>params->get('pageclass_sfx')); ?>"> <?php if ($this->item->readmore_register) : echo JText::_('Register to read more...'); elseif ($readmore = $this->item->params->get('readmore')) : echo $readmore; else : echo JText::sprintf('Read more...'); endif; ?></a>

</td> </tr> <?php endif; ?> </table> <?php if ($this->item->state == 0) : ?> </div> <?php endif; ?> <span class="article_separator">&nbsp;</span> <?php echo $this->item->event->afterDisplayContent; ?>

By gi ch nh s a l i ph n tc gi (written by administrator) v ngy thng c a bi vi t cho n hi n th xu ng d i dng last update thay v n m trn u nh v y th tm n o n code sau Written by Administrator
<?php if (($this->item->params->get('show_author')) && ($this->item>author != "")) : ?> <tr> <td width="70%" valign="top" colspan="2"> <span class="small"> <?php JText::printf( 'Written by', ($this->escape($this->item>created_by_alias) ? $this->escape($this->item->created_by_alias) : $this>escape($this->item->author)) ); ?> </span> &nbsp;&nbsp; </td> </tr> <?php endif; ?>

Ngy thng
<?php if ($this->item->params->get('show_create_date')) : ?> <tr> <td valign="top" colspan="2" class="createdate"> <?php echo JHTML::_('date', $this->item>created, JText::_('DATE_FORMAT_LC2')); ?> </td> </tr> <?php endif; ?>

C t chng v l n l

t paste 2 o n code ny ln trn dng ny

<?php if ($this->item->params->get('show_readmore') && $this->item>readmore) : ?>

Sau l u l i v refesh trang web xem k t qu L u : Cc template c a cc module v component c th t ng thch v i m i template, thng th ng c thi t k b ng cc th table thay v th div, v n u thch c th thi t k b ng th div v nh ngh a n trong file css c a template. trnh g p ph i cc sai st, v ph i tm hi u m t module hay component th t k , nh t l cc component th ng c r t nhi u b ph n template c nh ngh a ring trong cc file khc nhau nh ng l i lin k t r t ch t ch v i nhau tr c khi mu n thay i n.

II. Xy d ng module III. Xy d ng component MVC Component l m t trong nh ng thnh ph n m r ng l n nh t v ph c t p. N g m hai ph n chnh: ph n qu n tr v ph n trn site. Ph n trn site l ph n c s d ng t i v cc trang khi

tri u g i trong qu trnh v n hnh site thng th ng. Ph n qu n tr cung c p giao di n c u hnh v n hnh site. Ph n qu n tr cung c p giao di n c u hnh, qu n l cc kha c nh khc nhau c a component v c truy c p thng qua ng d ng qu n tr joomla. Gi i thi u m hnh MVC (Model View Controller) Model: thnh ph n c a component ng gi d li u c a ng d ng. Thng th ng model s ch a cc ph ng th c nh b sung, lo i b v c p nh t thng tin v nh ng l i cho m ng trong c s d li u. Ngoi ra cn ch a ph ng th c l y danh sch cc l i cho trong CSDL. View: l m t thnh ph n c a component c s d ng tr l i d li u t model theo cch ph h p v i t ng tc. Thng th ng view l cc trang HTML tr l i d li u. View l y d li u t model (d li u ny c chuy n qua n t i controller). V a d li u vo trong template (d li u hi n th v i ng i dng). View khng lm thay i d li u. N ch hi n th d li u l y t model. Controller: ch u trch nhi m ph n h i cch hnh ng c a ng i dng. Trong cc ng d ng web, m t hnh ng c a ng i dng thng th ng l m t yu c u t i trang. Controller s xc nh yu c u g c a ra b i ng i s d ng v ph n h i thch h p b ng vi c yu c u model tnh ton d li u ph h p v chuy n t model vo view. Controller khng th hi n d li u t model, n kch ho t cc ph ng th c trong model hi u ch nh d li u v sau chuy n t model sang view hi n th d li u. M hnh MVC trong Joomla: c th c hi n s d ng 3 l p <classname> JModle</classname>, <classname> JView</classname>, <classname>JController</classname> V d t o m t component i v i component c s c 5 file y Hello.php y l i m vo cho component y Controller.php file ny c ch a controller c b n y Views/hello/view.html.php file ny nh n cc d li u c n thi t v t n ln template y Views/hello/tmpl/default.php file ny l template cho u ra y Hello.xml y l m t file xml ni cho joomla bi t cch ci t component nh th no T o i m vo (create entry point) thng qua m t i m vo n: index.php cho cc ng d ng site v administrator/index.php cho ng d ng qu n tr . Sau ng d ng s t i cc component theo URL nh sau: index.php?option=com_hello&view=hello. Vi c ny s t i file chnh v c th c xem nh m t i m vo n cho component: components/com_hello/hello.php. o n m ny th c s l c th ty theo cc component. <?php //no direct access defined('_JEXEC') or die('Restricted access'); //Require the base controller require_one(JPATH_COMPONENT.DS.'controller.php'); //Require specific controller if requested if($controller = JRequest::getWord('controller')){ $path = JPATH_COMPONENT.DS.'controllers'.DS.$controller.'.php'; if(file_exists($path)){ require_once $path; } else{ $controller = ''; } }

//Create the controller $classname = 'HelloController'.$controller; $controller = new $classname(); //Perform the Resquest task $controller->execute(JRequest::getVar('task')); //Redict if set by the controller $controller->redirect(); ?> Cu l nh u tin l cu l nh ki m tra b o m t. JPATH_COMPONENT l ng d n tuy t i t i component hi n t i, trong tr ng h p c a chng ta components/com_hello. N u b n c n xc nh site component ho c admin component th s d ng JPATH_COMPONENT v JPATH_COMPONENT_ADMINISTRATOR DS l d u phn cch th m c trong h th ng c th l \ ho c /. Sau khi t i controller c b n, n s ki m tra m t controller c th c n n. Trong component ny controller c b n ch l m t controller. <classname> JRequest</classname>:getVar() tm m t bi n trong URL ho c POST d li u. B i v y n u URL l: index.php?option=com_hello>controller=controller_name th c th nh n c controller trong component b ng s d ng cu l nh sau: echo<classname>JRequest</classname>::getVar(controller); By gi c controller c s <classname>HelloController</classname> trong com_hello/controller.php v n u c n thi t b sung thm cc controller d ng nh <classname>HelloControllerController1</classname> com_hello/controllers/controller1.php. S s p x p theo h th ng ny s t o thu n l i v sau: {Componentname}{Controller}{Controllername} Sau khi controller c t o ra, th controller th c hi n nhi m v nh c ch ra trong URL index.php?option=com_hello&task=sometask. N u khng c nhi m v no c thi t l p th nhi m v m c nh display s c gi nh. Khi display c s d ng bi n view s quy t nh ci g s c hi n th . Cc nhi m v khc nh save, edit, new,. Controller cso th quy t nh redirect the page (th c hi n t i l i m t trang), thng th ng l sau khi m t nhi m v nh save c hon thnh, cu l nh cu i cng th c hi n cc nhi m v c t ra trong request. T o controller component ch c m t nhi m v - greet the world (th hi n l i cho). B i v y controller s r t n gi n. Khng c n n vi c tnh ton trn d li u. T t c nh ng g c n bi t l ph i t i view thch h p. Ch c m t ph ng th c trong controller l display(). H u h t cc hm c n thi t c xy d ng trong l p JController b i v y t t c nh ng g chng ta c n l ph ng th c JController::display() <?php //no direct access defined('_JEXEC') or die('Restricted access'); jimport('joomla/application.component.controller'); class HelloController extends JController{ /*@access public function display(){ parent::display(); } }

?> T o view: view l r t n gi n n nh n d li u c th hi n v t n ln template. D li u c t ln template s d ng ph ng th c <classname> JView</classname>::assignRef <?php //no direct access defined('_JEXEC') or die('Restricted access'); jimport('joomla.application.component.view'); /** *HTML View class for the HelloWorld Component * * @package HelloWorld */ class HelloViewHello extends JView { function display($tpl = null) { $greeting = "Hello World!"; $this->assignRef('greeting',$greeting); parent::display($tpl); } } ?> T o template: Joomla template/layout l cc file PHP thng th ng, c s d ng b tr, x p t d li u t view theo m t cch c th no . Cc bi n c gn b i ph ng th c JView:assignRef c th c truy c p t template s d ng $this->{propertyname} Template n gi n l th hi n m t l i cho h p quy t view <?php //no direct access defined('_JEXEC') or die('Restricted access'); ?> <h1><?php echo $this->greeting;?></h1> ng gi t t c - t o ra file hello.xml: C th ci t th cng m t component b ng cch copy t t c cc file b ng FPT client v hi u ch nh CSDL. Nh ng hi u qu h n n u t o ra m t file c ng gi Joomla t Installer th c hi n cho b n. File ng gi g m c cc thng tin nh sau: y Cc miu t chi ti t c b n v component c a b n (v d nh tn), v ty m t s m t thng tin v b n quy n.. y M t danh sch cc file c n copy y M t file PHP th c hi n b sung cc thao tc ci t v g b (file ny l khng b t bu c) y M t file SQL c ch a cc cu truy v n d li u m s c th c hi n vo lc ci t ho c g b (file ny l b t bu c) nh d ng c a file XML nh d i y: <?xml version="1.0" encoding="utf-8"?> <!DOCTYPE install SYSTEM "http://dev.joomla.org/xml/1.5/component-install.dtd"> <install type = "component" version = "1.5.0"> <name> Hello </name>

<!-- The following elements are optional and free of formatting conttraints--> <creationDate>2007 02 2002</creationDate> <author>John Doe</author> <authorEmail>john.doe@example.org</authorEmail> <authorUrl>http://www.example.org</authorUrl> <copyright>Copyright Info</copyright> <!--The version string is recorded in the components table--> <version>Component Version String</version> <!--The description is optional and defaults to the name--> <description>Description of the component...</description> <!--Site Main File Copy Section--> <!--Note the folder attribute: this attribute describes the folder to copy FROM in the package to install therefore files copied in this section are copied from/site/in the package--> <files folder = "site"> <filename> index.html</filename> <filename> hello.php</filename> <filename> controller.php</filename> <filename>views/index.html</filename> <filename>views/hello/index.html</filename> <filename>views/hello/view.html.php</filename> <filename>views/hello/tmpl/index.html</filename> <filename>views/hello/tmpl/default.php</filename> </files> <administrator> <!--Administrator Menu Section--> <menu> Hello World </menu> <!--Administrator Main File Copy Section--> <files folder="admin"> <filename> index.html</filename> <filename> admin.hello.php</filename> </files> </administrator> </install> C m t s file s c copy ch a c p n, l index.html c tc d ng ng n c n nh ng ng i dng t m trong vi c li t k m t danh sch th m c. N u khng c file index.html m t vi my ch web s li t k danh sch n i dung c a th m c. i u ny th ng gy r c r i. Cc file ny c m t dng n nh sau: (n n gi n l th hi n m t trang tr ng) <html><body bgcolor = #FFFFFF></body></html> File khc l admin.hello.php, y l i m vo cho section qu n tr trong component. B i v chng ta ch a c section qu n tr trong component vo th i i m ny nn n s c n i dung t ng t file index.html Lm sao chuy n d li u ra kh i view v a n vo m t model. T o model: s m hnh ha cho m t vi th c th no . Cch t tn cho cc model trong Joomla tn component + model + tn model v d HelloModelHello. v d ny n gi n c

m t ph ng th c c g i l getGreeting(). Ph ng th c ny n gi n l tr l i chu i Hello, Word. D i y l code cho model <?php //Check to ensure this file is included in Joomla defined('_JEXEC') or die(); jimport('joomla.application.component.model'); class HelloModelHello extends JModel { function getGreeting(){ return 'Hello, World'; } } ?> S d ng model: Joomla thi t l p theo cch controller s t ng t i model c tn trng v i view v t n vo trong view. V view c t l hello, nn model hello nn s t ng ct i vo trong view. B i v y s d dng nh n c m t tham chi u n model s d ng ph ng th c JView::getModel(). o n m cho view tr c c ch a dng: $greeting = Hello World!; s d ng model, chuy n i dng trn thnh $model =& $this->getModel(); $greeting = $model->getGreeting(); Code c a view nh sau: <?php //no direct access defined('_JEXEC') or die('Restricted access'); jimport('joomla.application.component.view'); class HelloViewHello extends JView { function display($tpl = null) { $model = & $this->getModel(); $greeting = $model->getGreeting(); $this->assignRef('greeting',$greeting); parent::display($tpl); } } ?> B sung vo package: b sung m t entry vo file XML model m i c a chng ta c copy. Joomla s tm ki m model trong th m c models. <filename> models/hello.php</filenname> File hello.xml m i c a chng ta s nh sau: <?xml version="1.0" encoding="utf-8"?> <!DOCTYPE install SYSTEM "http://dev.joomla.org/xml/1.5/component-install.dtd"> <install type = "component" version = "1.5.0"> <name> Hello </name> <!-- The following elements are optional and free of formatting conttraints--> <creationDate>2007 02 2002</creationDate> <author>John Doe</author>

<authorEmail>john.doe@example.org</authorEmail> <authorUrl>http://www.example.org</authorUrl> <copyright>Copyright Info</copyright> <!--The version string is recorded in the components table--> <version>Component Version String</version> <!--The description is optional and defaults to the name--> <description>Description of the component...</description> <!--Site Main File Copy Section--> <!--Note the folder attribute: this attribute describes the folder to copy FROM in the package to install therefore files copied in this section are copied from/site/in the package--> <files folder = "site"> <filename> index.html</filename> <filename> hello.php</filename> <filename> controller.php</filename> <filename>views/index.html</filename> <filename>views/hello/index.html</filename> <filename>views/hello/view.html.php</filename> <filename>views/hello/tmpl/index.html</filename> <filename>views/hello/tmpl/default.php</filename> <filename>models/index.html</filename> <filename>models/hello.php</filename> </files> <administrator> <!--Administrator Menu Section--> <menu> Hello World </menu> <!--Administrator Main File Copy Section--> <files folder="admin"> <filename> index.html</filename> <filename> admin.hello.php</filename> </files> </administrator> </install> Cch s d ng CSDL trong component MVC Bi t cch lm vi c v i model. Thay v vi c d li u c code tr c ti p trong model, th model s l y d li u t m t b ng trong CSDL. Cch l y d li u: model tr c ch c m t ph ng th c getGreeting() n gi n tr v m t cu cho c code c nh i u c n l ph i t o ra m t file SQL v b sung cc o n m ph h p vo file XML t o ra b ng khi component c ci t. By gi , n gi n thay th cu l nh tr v trong hm getGeeting b ng o n code l y l i cho m ng t CSDL v tr l i l i cho m ng . u tin, c n l y c m t tham chi u n i t ng CSDL. V joomla s d ng CSDL cho cc thao tc thng th ng c a n, nn n c m t k t n i t i CSDL t n t i, b i th khng c n thi t ph i t o ra m t k t n i CSDL ring. C th l y c m t tham chi u t i k t n i b ng cch s d ng cu l nh: $db = JFactory::getDBO();

JFactory l m t l p static c s d ng nh n tham chi u n nhi u i t ng h th ng khc nhau. Cc thng tin thm v l p ny c th tham kh o trong ti li u API Tn hm getDBO l vi t t t cho get Database Object, c th nh m t cch d dng v r t quan tr ng. By gi chng m t tham chi u n i t ng CSDL. C th l y d li u qua hai b c y L u tr cu truy v n vo i t ng CSDL y T i k t qu v Code c a ph ng th c getGreeting() by gi s nh sau function getGreeting(){ $db = &JFactory::getDBO(); $query = 'SELECT greeting FROM #_hello'; $db->setQuery($query); $greeting = $db->loadResult(); return $greeting; } Trong o n code hello l tn b ng trong CSDL m chng ta s t o ra trong ph n ti p. V greeting l tn tr ng l u tr cu cho m ng. Ph ng th c $db->loadResult() s ch y cu l nh truy v n CSDL c l u tr v tr v tr ng u tin c a dng u tin trong k t qu . (Ch JDatabase c a API) T o ra file SQL trong qu trnh ci t v g b component: T t c cc cu truy v n cl u tr trong m t file text. V i 3 cu truy v n trong file ci t, vi c u s th c hi n xa b ng trong tr ng h p n t n t i, ti p theo th c hi n vi c t o ra b ng v i cc tr ng ph h p, v cu i cng l chn d li u vo b ng DROP TABLE IF EXISTS '#_hello'; CREATE TABLE '#_hello'( 'id' int(11) NOT NULL auto_increment, 'greeting' varchar(25) NOT NULL, PRIMAKEY KEY('id') ) ENGINE = MyISAM AUTO_INCREMENT DEFAULT CHARSET=utf-8; INSERT INTO '#_hello' ('greeting') VALUES ('Hello, World!'), ('Bonjour, Monde!'),('Ciao,Mondo!'); T o file SQL g b ci t: Joomla qu n l vi c xa cc file v cc th m c c t o ra trong qu trnh ci t, nh ng ph i b sung th cng cc cu truy v n s lo i b cc b ng d li u s lo i b cc b ng c thm vo CSDL. Trong v d , ta ch t o m t b ng, nn ch c n m t cu truy v n xa n. DROP TABLE IF EXITST _hello; Cu truy v n ny s c l u tr trong file uninstall.utf.sql C p nh t file ci t: c n ph i i m t vi th trong file ci t XML. u tin, c n b sung hai file m i vo danh sch cc file. <?xml version="1.0" encoding="utf-8"?> <!DOCTYPE install SYSTEM "http://dev.joomla.org/xml/1.5/component-install.dtd"> <install type = "component" version = "1.5.0"> <name> Hello </name> <!-- The following elements are optional and free of formatting conttraints--> <creationDate>2007 02 2002</creationDate>

<author>John Doe</author> <authorEmail>john.doe@example.org</authorEmail> <authorUrl>http://www.example.org</authorUrl> <copyright>Copyright Info</copyright> <!--The version string is recorded in the components table--> <version>Component Version String</version> <!--The description is optional and defaults to the name--> <description>Description of the component...</description> <!--Site Main File Copy Section--> <!--Note the folder attribute: this attribute describes the folder to copy FROM in the package to install therefore files copied in this section are copied from/site/in the package--> <files folder = "site"> <filename> index.html</filename> <filename> hello.php</filename> <filename> controller.php</filename> <filename>views/index.html</filename> <filename>views/hello/index.html</filename> <filename>views/hello/view.html.php</filename> <filename>views/hello/tmpl/index.html</filename> <filename>views/hello/tmpl/default.php</filename> <filename>models/index.html</filename> <filename>models/hello.php</filename> </files> <administrator> <!--Administrator Menu Section--> <menu> Hello World </menu> <!--Administrator Main File Copy Section--> <files folder="admin"> <filename> index.html</filename> <filename> admin.hello.php</filename> <filename>install.sql</filename> <filename>uninstall.sql</filename> </files> </administrator> </install> L u : n hai thu c tnh xu t hi n trong cc th <file> n m trong cc ph n <install> v <uninstall>: charset v driver. Charset l ki u charset c s d ng, ch c charset h p l l utf8. N u mu n t o ra cc file ci t cho CSDL khng ph i l utf8 th s b qua thu c tnh ny, thu c tnh driver xc nh CSDL no cc cu truy v n c vi t cho. V i b n 1.5.x, thu c tnh ny ch c th l MySQL K t lu n: xy d ng m t component t n d ng c l p JDatabase, v c th ci t ho c g b m t cch n gi n. Lm sao pht tri n section qu n tr cho component, c kh n ng qu n l cc entry trong CSDL?

T o m t framework c s : c a panel qu n tr c ng t ng t ph n site. i m vo chnh cho ph n qu n tr l file admin.hello.php. File ny gi ng y h t file hello.php c s d ng trong thanh ph n site ngo i tr tn c a controller m n t i s c thay i thnh HellosController. Ngoi ra, controller m c nh c g i l controller.php v file ny gi ng y h t controller m c nh trong ph n site, ngo i tr tn c a controller c g i l HellosController thay g l HelloCotroller. S thay i ny JController theo m c nh s t i hellos view, view ny hi n th m t danh sch cc cu cho m ng <?php //no direct access defined('_JEXEC') or die('Restricted access'); //Require the base controller require_once(JPATH_COMPONENT.DS.'controller.php'); //Require specific controller if requested if($controller = JRequest::getWord('controller')){ $path = JPath_COMPONENT.DS.'controller'.DS.$controller.'.php'; if(file_exits($path)){ require_once $path; }else{ $controller= '' } } //Create the controller $classname = 'HellosController'.$controller; $controller = new $classname(); //Perform the Request task $controller->execute(JRequest::getVar('task')); //Redirect if set by the controller $controller->redirect(); ?> Model Hellos: ban u ch c n truy v n nh n c m t danh sch c a cc cu cho m ng t CSDL. Cng vi c ny c th c hi n b ng cch g i ph ng th c getData() L p JModel c m t ph ng th c protected c xy d ng s n _getList(). Ph ng th c ny c th c s d ng n gi n ha tc v truy v n nh n c m t danh sch cc b n ghi t CSDL. n gi n l c n truy n cho n c u truy v n v n s tr l i danh sch cc b n ghi. Trong t ng lai, c th l i mu n s d ng cu truy v n trong m t ph ng th c khc. V v y, y s l i mu n s d ng cu truy v n trong m t ph ng th c khc. V v y, y s t o ra m t ph ng th c private _buildQuery() tr l i cu truy v n s c truy n vo ph ng th c _getList(). i u ny s t o ra kh n ng thay i cu truy v n khi n c t trong m t tnh hu ng khc. Do trong l p s c hai ph ng th c : getData() v buildQuery(). Ph ng th c _buildQuery() n gi n l tr v cu truy v n, code c a n nh sau: /** *Returns the query *@return string The query to be used to retrieve the rows from the database */ function _buildQuery() {

$query = SELECT * . FROM #_hello; return $query; } Ph ng th c getData() nh n cu truy v n v tr l i danh sch cc b n ghi t CSDL. By gi c th x y ra tnh hu ng l c n nh n c danh sch ny 2 l n trong vi c t i m t trang. Th t l lng ph khi truy v n CSDL 2 l n. B i v y chng ta s ph i lm cho ph ng th c ny l u tr d li u trong m t thu c tnh protected trong cc yu c u ti p theo, n ch n gi n l tr v d li u m n l y c. Thu c tnh ny s c g i l _data. Ph n code n l /** *Retrieves the hello data *@return array Array of objects containing the data from the database */ function getData() { //Let load the data if it doesnt already exist $query = $this->_buildQuery(); $this->_data = $this->_getList($query); } return $this->_data; } Code y c a model sau khi hon thnh nh sau: <?php //Check to ensure this file is included in Joomla Denfined(_JEXEC) or die(); jimport(joomla.application.component.model); class HelloModelHellos extends JModel { var $_data; function _buildQuery(){ $query = SELECT * . FROM #_hello; } Function getData(){ //Lets load the data if it doesnt already exist if(empty($this->_data)) { $query = $this->_buildQuery(); $this->_data = $this->_getList($query); } return $this->_data; } } ?> File ny c l u l i thnh models/hellos.php View hellos: c m t model nh n d li u, v v y gi c n m t view hellos. Model ct ng ci t trn site, b i v y n n m trong ph n qu n tr . Cc ph ng th c b t u b ng get trong model c th c truy c p b ng vi c s d ng ph ng th c get() c a l p JView. B i v y view s c 3 dng. M t l y d li u t model, m t y d li u ln template, v m t g i ph ng th c display hi n th . Do code n nh sau

<?php //Check to ensure this file is included in Joomla defined('_JEXEC') or die(); jimport('joomla.application.component.view'); class HelloViewHellos extend JView { function display($tpl = null){ JToolBarHelper:: title(JText::_('Hello Manager'),'generic.png); JToolBarHelper::deleteList(); JToolBarHelper::editList(); JToolBarHelper::addNewX(); //get data from the model $item = &$this->get('Data'); $this->assignRef('items',$item); parent::display($tpl); } } ?> File ny l u v i tn l: views/hellos/view.html.php. Template hellos: template s l y d li u c a n t view v a d li u ra u ra hi n th . Mu n hi n th k t qu u ra trong m t b ng n gi n. Trong khi template ph n site l r t n gi n, th trong trang qu n tr s c n m r ng m t cht x l vng l p thng qua d li u. D i y l ph n code trong template <?php defined('_JEXEC') or die('Restricted access');?> <form action="index.php" method = "post" name = "adminForm"> <div id = "editcell"> <table class = "adminlist"> <thead> <tr> <th width = "5"> <?php echo JText::_('ID');?> <th> <th> <?php echo JText::_('Greeting');?> </th> <tr> <thead> <?php $k = 0; for($i=0, $n = count($this->items); $i<$n; $i++){ $row = $this->items[$i]; ?> <tr class = "<?php echo "row$k";?>"> <td> <?php echo $row->id;?> </td>

<td> <?php echo $row->greeting;?> </td> <tr> <?php $k = 1 -$k; } ?> </table> </div> <input type = "hidden" name = "option" value = "com_hello"/> <input type = "hidden" name = "task" value = ""/> <input type = "hidden" name = "boxchecked" value = "0"/> <input type = "hidden" name = "controller" value = "hello"/> </form> Template ny c l u thnh views/hellos/tmpl/default.php Ch template c ng gi trong m t form. M c d i u ny l khng c n thi t vo th i i m hi n t i, nh ng trong t ng lai n s c n. By gi c n b sung 5 file vo ph n qu n tr trong componet y Admin.hello.php y Controller.php y Models/hellos.php y Views/hellos/view.html.php y Views/hellos/tmpl/default.php Gi b sung cc file ny vo file ci t XML. V th l i B sung thm ch c n ng: Section qu n tr cho n gi ch a th c hi n nhi u tc v . N th c s ch a lm c g. Nh ng g n th c hi n l hi n th nh ng entry m c trong CSDL. lm h u d ng h n c n b sung button v cc ng link Toolbar: xu t hi n pha trn cc panel khc trong component qu n tr joomla. Component c ng c n m t th t ng t nh th . B sung thm cc nt xa b n ghi, hi u ch nh b n ghi, thm m i b n ghi. Ngoi ra cn b sung thm m t title th hi n trn toolbar. i u ny c th c hi n b ng cch b sung thm code vo view. b sung thm cc button th s d ng cc ph ng th c t nh t l p JtoolbarHepper trong joomla. Code s trng nh sau: JToolBarHelper::title(JTEXT::_(Hello Manager), generic.png); JToolBarHelper::deleteList(); JToolBarHelper::addNewX(); Ba ph ng th c ny s t o ra cc button ph h p. Ph ng th c deleteList() c th ty ch n l y ba tham s : Tham s u tin l m t chu i th c hi n th n ng i dng yu c u h xc nh n vi c xa b n ghi. Tham s th hai l nhi m v s c g i v i yu c u truy v n (m c nh l romove). V tham s th hai l nhi m v s c g i v i cu truy v n (m c nh l remove). V tham s th 3 l o n text s c th hi n d i button. Cc ph ng th c editListX() v addNew() c th ty ch n hai tham s : Tham s u tin l nhi m v (theo m c nh l n l t l edit v add), v tham s th 2 l o n text s c th hi n d i button. Ch r ng cch s d ng ph ng th c JText::_ trong template tr c y v y l t ng t nhau. y l ch c n ng gip cho component c th chuy n i m t cch r t d dng. Ph ng th c JText::_ s tm ki m xu trong file ngn ng c a component v tri l i chu i c

chuy n i. N u khng c s chuy n i no c tm th y n s tr l i chu i truy n vo cho n. N u chng ta mu n chuy n i component sang m t ngn ng khc, t t c nh ng g ph i lm l t o ra m t file ngn ng nh x xu trong d u trch d n v i xu c chuy n i t ng ng. [V d : n u trong file ngn ng chng ta c m t c p Greeting Hello world th ph ng th c JText::_(Greeting) s tr l i chu i Hello World. Khi chng ta c n chuy n qua ti ng vi t trong file ngn ng chng ta ch c n c m t c p Greeting - Xin cho! lc ny ph ng th c JText::_(Greeting) s tr l i chu i Xin cho!.] Checkbox v link: Chng ta c hai button. Hai button ny thao tc trn cc b n ghi t n t i. Nh ng lm th no bi t ang thao tc trn b n ghi? Chng ta ph i yu c u ng i dng ni cho cho l p trnh vin bi t i u . lm i u ny chng ta c n b sung thm cc checkbox vo b ng hi n th trn template ng i dng c th l a ch n cc b n ghi th c s . b sung thm cc checkbox c n b sung thm m t c t vo b ng. Trong ph n u c a c t, s thm m t checkbox c th l a ch n t t c cc checkbox d i n thnh on ho c off <th width= 20> <input type = checkbox name = toggle value= onclick= checkAll(<?php echo count($this->items);?>/> </th> y, hm checkAll trong JS l m t hm c xy d ng trong Joomla d a trn gi JS s cung c p nh ng ch c n ng m l p trnh vin mu n. By gi c n b sung cc checkbox vo cc t ng hng. L p JHTML c a Joomla c m t ph ng th c JHTML::_() t o ra checkbox. Chng ta s b sung dng code d i y vo vng l p. $checked = JHTML::_(grid.id, $i, $row->id); Ngay pha sau dng $row = & $this->items[$i]; Sau s thm cc cell c a c t checkbox n m gi a hai c t c (c t id v c t greeting) <td> <?php echo echo $checked;?> </td> n lc ny, hi u ch nh m t b n ghi, c n ph i check vo box t ng ng v i b n ghi , sau vo nt edit.Vi c h i r m r. V v y gi i quy t b ng cch b sung m t ng link i tr c ti p n form hi u ch nh cu cho m ng. B sung thm dng d i y sau khi g i ph ng th c JHTML::_() t o ra link HTML. $link = JRouter::_(index.php?option=com_hello>controller=hello>task=edit>cid[]= $row->id); Sau a ng link vo trong cell ch a cu cho m ng: <td> <a href = <?php echo $link;?>><?php echo $row->greeting;?></a> </td> Ch r ng ng link ny ch n controller hello. Controller ny s x l vi c thao tc trn d li u. N u xem l i code trong ph ntemplate s th y c 4 tr ng hidden input pha d i form. Tr ng u tin l option, tr ng th hai l nhi m v c n th c hi n, tr ng ny s l y cc thi t l p IV. a

You might also like