Parser documentation - Lesson 2
Parser documentation - Lesson 2Skin: StandartGrayGreen Home / Portfolio / Translations / Parser documentation - Lesson 2 Remember ╣а2:
аParser documentation - Lesson 2Published:
аNovember 12, 2002Last change:
аJune 3, 2003 | Lesson 2.аNavigation menu and pages structureWeаhave finished theаprevious lesson pointing out theаdefects ofаtheаcertain menu. Let's deal with eliminating these defects. Our menu has unnecessary link toаtheаcurrent page that are not good for future site. It's necessary toаcheck whether aаsection ofаour menu, isаtheаcurrent toаavoid it. Inаother words weаhave toаcompare menu's section URI with theаcurrent URI. Ifаthey are theаsame, weаneedn't toаhave theаlink. Additionally we'll change theаcolor ofаcolumn ofаcurrent section for user's convenience.Open file auto.p and change its contents to:@navigation[]
$sections[^table::load[/sections.cfg]]
<table width="100%" border="0" bgcolor="#000000" cellspacing="1">
<tr bgcolor="#FFFFFF">
^sections.menu{
^navigation_cell[]
}
</tr>
</table>
<br>
@navigation_cell[]
$cell_width[^eval(100\$sections)%]
^if($sections.uri eq $request:uri){
<td width="$cell_width" align="middle" bgcolor="#A2D0F2">
<nobr>$sections.name</nobr>
</td>
}{
<td width="$cell_width" align="middle">
<a href="$sections.uri"><nobr>$sections.name</nobr></a>
</td>
}Are there any changes? There are noаmany atаfirst sight but theаfunctionality ofаour module has greatly increased. Weаdefined another methodаЧ navigation_cell that are called from navigation method. Aаnew structure has appeared inаit:^if(condition){code if true}{code if false}It's not difficult toаunderstand its destination. Theаcondition are set within round brackets and depending onаtheаresult ofаtheаconditionаЧ true orаfalse, theаstructure will return different result. Also, ifаinаcondition's place there isаanаexpression that returns zero, theаresult isаfalse orаtrue instead. Weаuse if operator soаasаtoаinsert link inаone case and not toаdoаsoаinаanother. Weаhave toаdeal with condition yet. We'll compare two text where one ofаthem isаsection's URI from sections table and another one isаcurrent URI ($request:uri returns string with URI ofаcurrent page). Theаquestion about equivalent strings occurs here. Theаanswer isаonly those that are exactly equal byаlength and symbol content.There are following operators for string's comparing inаParser:eqаЧ strings are equal: parser eqаparserneаЧ strings aren't equal: parser neаparser3ltаЧ theаfirst string less then theаsecond one: parser ltаparser3gtаЧ theаfirst string greater then theаsecond one: parser3 gtаparserleаЧ theаfirst string less orаequal then theаsecond onegeаЧ theаfirst string greater orаequal then theаsecond oneWeаhave finished with theаcondition: ifа$sections.uri equal $request:uri weаdon't insert link (and taint theаcolumn toаanother colorаЧ weаthink about our usersаЧ it'll beаmore convenient for them) elsewhere insert link.Let's goаon. Theаfirst lesson's menu made columns ofаdifferent width. There isаnothing wrong but ugly inаit. Theаproblem's solution isаquite simple: weаdivide all width (100%) ofаmenu byаtheаnumber ofаsections equivalents theаnumber ofаrows inаsections table. Soаasаtoаdoаitаweаuse ^eval() operator and possibility ofаusing table class object inаmathematical expressions. Their number value equivalents theаnumber ofаrows inаaаtable. Take into account that weаuse integer division byаusing back slash instead ofаforward slash.Let's analyze ^eval() more carefully. Itаreturns theаresult ofаmathematical expressions without additional variables, inаother words ifаweаwant toаcount something weаwrite:^eval(expression)[format]Using [format] gives anаopportunity toаhave anаoutput inаnecessary format. Format string [%d] removes fractional part, [%.2f] gives two digits after comma, and [%04d] provides 4аdigits for integer part, expanding unavailable symbols byаzeros. There isаnecessity ofаformatted output when you need toаhave aаnumber inаnecessary format (for example $12.44 instead ofа$12.44373434501).That isаall about menu. Itаhas all functions and are ready toаuse.Theаfirst part ofаour site isаready. Let's deal with theаpage's structure now. Weаdivide them for next parts: headerаЧ upper part ofаaаpage, bodyаЧ theаmain information block that includes our menu and footerаЧ lower part ofаaаpage. Many sites have theаsimilar structure.Footer will beаtheаsame for all pages, header isаtheаsame inаstyle but with different text strings that are aаpage's titles and body will beаdifferent for all pages keeping only theаcommon structure (let's suppose that weаhave two information blocks with width ratio 3:7). Our menu will beаtheаpart ofаbody.Every page will have theаfollowing structure:headernavigation body_additional (30%)body_main (70%)footerAsаinаtheаcase ofаour menu we'll make parser's method(function) for every block. Let's carefully look into every block.Everything isаeasy with theаfooterаЧ just add theаfollowing code into auto.p:@footer[]
<table width="100%" border="0" bgcolor="#000000" cellspacing="0">
<tr>
<td></td>
</tr>
</table>
$now[^date::now[]]
<font size="-3">
<center>Powered by Parser3<br>1997-$now.year</center>
</font>
</body>
</html>There isаnothing new here unless weаused class date with now constructor for getting current date and then got theаfield year from theаobject ofаtheаclass date. ifаit's impossible toаunderstand for you you have toаreturn toаtheаfirst lesson where explained how toаwork with objects byаtheаexample ofаclass table. Everything isаequally but weаjust have theаobject ofаanother class.Header module isаmore complex. Onаtheаone hand weаhave toаform unique title for every page onаanother hand it'll beаtheаsame inаstyleаЧ theаdifference isаonly inаtext. What should weаhave toаdo? Weаoffer toаdoаtheаfollowing thing: define theаnew function header inаour auto.p that will call another functionаЧ greeting. Greeting function will beаdefined onаsite's pages and contain only theаdifference ofаpage's titles (title string inаour case).Add theаfollowing code into auto.p:@header[]
<html>
<head>
<title>Parser3 test site</title>
</head>
<body bgcolor="#FAEBD7">
<table width="100%" border="0" bgcolor="#000000" cellspacing="1">
<tr bgcolor="#FFFFFF" height="60">
<td align="middle">
<font size="+2"> <b>^greeting[]</b></font>
</td>
</tr>
</table>
<br>Attention please,аЧ weаreached culmination. Parser allows toаmake very interesting trickаЧ toаdefine common pages structure once inаfile auto.p i.e. make framework and then have different pages that are vary byаcontent but theаsame byаstyle using functions similar greeting that are defined within pages code. How does itаwork?Atаtheаbeginning ofаfile auto.p we'll define @main[] function that always run theаfirst automatically. We'll include calls ofаfunctions for forming pages parts inаit.Atаtheаbeginning ofаfile auto.p weаwrite:
@main[]
^header[]
^body[]
^footer[]
We'll define greeting function within every page for getting unique title ofаtheаpage and theаfunction are called from header:for main page:
@greeting[]
Welcome!
for guest book:
@greeting[]
write down something hereЕ
etc.Now while loading theаmain page for example it'll beаdone theаfollowing:It'll begin automatically run MAIN from file auto.p.Header function are called first and greeting function are called from it.Due toаdefinition ofаofаfunction greeting within theаpage's body it'll beаrun theаvery function despite ofаits definition within auto.p (theаcase ofаfunction overriding)Then body and footer functions are run from MAIN.Asаaаresult weаget theаpage with all necessary elements and with our unique greeting onаtheаtop part. Overridden functions are named virtual functions. From file auto.p weаcall aа function that can beаoverridden atаany moment and each ofаthem will run theаown code. Atаtheаsame time common pages' structure will beаtheаsame and style and logic will beаkept.Weаonly remain toаdefine theаmain blockаЧ body. Asаweаagreed it'll consist ofаtwo parts which ofаthem we'll create byаown function, body_main and body_additional for example and due toаnavigation menu are logically taken toаtheаmain part ofаpages weаcall itаfrom body too. We'll use theаmechanism ofаvirtual functions again. Edit auto.pаЧ add theаfollowing:
@body[]
^navigation[]
<table width="100%" height="65%" border="0" bgcolor="#000000" cellspacing="1">
<tr bgcolor="#ffffff" height="100%">
<td width="30%" valign="top" bgcolor="#EFEFEF">
<b>^body_additional[]</b>
</td>
<td width="70%" valign="top">
^body_main[]
</td>
</tr>
</table>
<br>
We'll insert into pages theаdefining ofаfunctions body_main and body_additional asаinаcase ofаgreeting function:
@body_additional[]
Home page
@body_main[]
Main content
This text isаanаexample for index.html. Excellent! Theаstructure was finally formed. Weаdefined all necessary modules inаauto.p file, formed theаcommon structure and weаcan easily generate pages now. Weаneedn't toаwrite theаsame pieces ofаHTML code for many times any more. Usual HTML pages are transformed into theаfollowing (approximate content ofаfile index.html for home page):
@greeting[]
Welcome!
@body_additional[]
Home page
@body_main[]
Main content
It's clear and easy, isn't it? Everything isаsepareted into constituent parts and can beаeasily accessed. Atаtheаsame time after processing ofаtheаcode parser will create page's HTML with unique title, menu, main information block with given structure and footer that are theаsame for every page. Actually weаhave already created theаsite and weаhave toаonly filling itаwith anаinformation. This isаtheаready solution for elegant visiting card site that can beаcreated before ones present. Naturally this isn't theаonly solution, but this approach gives theаnice structure ofаour site. Some intellectual energies while developing structure isаmore then compensated byа easy maintenance and modernization. Theаframework isаkept within auto.p and everything related toаaаpage isаkept within theаpage.Unlimited freedom for fantasy opens later. Let usаsuppose, weаhave toаchange theаappearance ofаsite pages header. Weаopen auto.p, once edit function @header[] and get theаnew header onаevery page that are theаsame for them all. Inаtheаcase ofаtraditional HTML weаhad toаmanually edit theаcode ofаevery site page. Theаsame situation isаwith other modules. Ifаyou wish orаneed toаchange theаcommon structure ofаpagesаЧ toаadd any block for example, itаisаenough toаdefine itаbyаnew function and expand MAIN function within auto.p byаcalling it.Such site pages organization add another power tool into theаproject. Let usаsuppose, onаone ofаpages weаmust have footer that are different from theаstuff onаother pages (remember that atаfirst weаsupposed toаhave theаsame footer everywhere). Theаonly thing weаhave toаdoаisаoverload footer function onаnecessary page. For example this content ofа/contacts/index.html:
@greeting[]
Our contact information
@body_additional[]
Home page of test site
@body_main[]
Main content
@footer[]
We have contacts herewill change usual footer for designated above, i.e. ifаParser find within page's text theаcode ofаfunction that are called from auto.p this only code will beаrun, even theаfunction defined within auto.p. But ifаaаfunction isn't overloaded onаaаpage theаcode from auto.p will beаused.Inаsummary some theory for curious. We'll give such information for those who wants toаharder understand Parser logic.Doаyou remember our using consruction $request:uri inаour code? Itаisаdifferent byаsyntax from everything weаdealt with before. What isаit? Itаisаlike $object.property (lesson 1)аЧ object fields value, but there isаcolon instead ofаcomma. Asаaаmatter ofаfact, this isаtheаvalue ofаfield only not ofаtheаobject but request class itself. There are noаconstructors for creating this class objects inаParser. Parser itself forms fields ofаsuch classes and weаcan directly call them. Inаtechnical terms this isаstatic variable (field) uri ofаrequest class. Itаkeeps URI ofаcurrent page. There are static methods along with static variables that we'll run into asаearly asаonаtheаnext lesson. Atаtheаsame time they can beаcalled directly without any objects' creating byаits constructiors. Remember that there isаalways colon sign inаsyntax ofаstatic fields and methods. Ifаtheаconstruction like $class:field occurs weаget theаvalue ofаfield ofаtheаclass itself and theаrecord ^class:method isаtheаcall ofаstatic method ofаclass. For example there isаclass math for working with mathematic functions inаParser. Only static methods and variables are used there:$math:PIаЧ returns piаnumber. This isаstatic variable ofаclass math.^math:random(100)аЧ returns pseodurandom number from 0аtoа100. This isаstatic method ofаclass math.Theаdifference between writing methods and fields ofаobjects isаonly inаcolon.Let's summarize theаsecond lesson.What have weаdone?:Weаcorrect navigation menu shortcomings that weаcreated onаtheаlatest lesson and besides defined new blocks header, footer and body forming theаappearance ofаpages onаour site. Now weаhave theаready solution for quick basic site creating.
What have weаlearned?:Weаmade anаacquaintance with code's forking, learn how toаinsert theаresults ofаmathematic calculations into pages text, compare strings and get URI current page. Besides weаgot toаknow new methods ofаclass table and class date and found out about power engine ofаvirtual functions inаParser.What must beаremembered:Theаfirst method that can beаdefined inаfile auto.p isаmain function which are run automatically. Any function can keep calls ofаother functions. All functions are called from main must beаdefined either inаauto.p orаwithin text ofаpages. Inаcase ofаdefining function inаboth places, theаfunction that defined within text ofаaаpage has greater priority. Itаoverloads theаfunction ofаtheаsame name from main (so-called virtual function) and are run instead ofаit.What shall weаdoаlater?:There isаnoаlimit ofаperfection! From basic site weаgoаtoаmore complex stuffаЧ theаsame asаworking with forms and databases onаpurpose ofаcreating theаreally interactive site. Concurrently we'll get toаknow about new features ofаParser language that are intended toаmake site's creators life simpler.<< ╣ 1 | ContentThe latest publications╣ 2аParser documentation - Lesson 2 12.11.2002╣ 1аParser documentation - Lesson 1 29.10.2002LinksParser 3Parser.ruHome / Portfolio / Translations / Parser documentation - Lesson 2 Remember Server InfoAuthorRegistration/settings
ёюфхЁцрэшх | 2 | Navigator D6
Используются технологии
uCoz