Chip Template Engine

Chip Template Engine

Infobox Software
name = Chip Template Engine
developer = Mike A. Leonetti
latest_release_version = 0.31
latest_release_date = 16th August 2006
genre = Template engine
license = LGPL
website = [http://code.divineaspirations.net/chip code.divineaspirations.net/chip]

Chip is a template engine written for use with both PHP and Perl. It is designed to provide both the programmer with a flexible toolset for quick HTML parsing and output, and the designer with a simple parsing control syntax. Instead of completely separating the business logic end of the code from the HTML portion, Chip allows the design end to utilize a tag-like command set to control HTML code output.

With HTML code control tags that more resemble real HTML, Chip "GUI" files can be syntax highlighted with the same parameters as HTML.

GUI files, or rather the files containing the HTML, contain more than one template. Templates are created an included in a "new" tag, and can be linked or embedded in each other to create dependencies. In this way a single GUI file can be used either for an entire section (i.e. all pages associated with a shopping cart) or just a single page (i.e. a homepage). Each template has its own separate cache, to allow for particular code flow.

Looping in Chip can be managed as the programmer sees fit. Since the HTML code is split up into separate templates and each template cached, it is possible to parse the same template over and over again with different variables to produce a list-like output.

The second option is to use the built in "foreach" tag and to set only one GUI variable as an array. The rest will be handled by the GUI code.

How it works

Chip uses Perl regular expressions to parse the GUI template creating another file containing the parsed PHP/Perl output that is then included into the currently running script. However, since a single GUI file needs to be parsed once, if the related GUI file is unmodified its PHP/Perl result will be recycled. This allows for sometimes large speed-ups.

After the file is included, actual HTML code is not parsed, nor are any of the variables replaced until "parse_template(...)" or "output_template(...)" are called.

In essence, each template is in effect like a C function or a Perl sub-routine. None of the code contained in the function or sub-routine is executed until the function is called. Templates behave in the same manner.

Coding examples

"main.gui"

{$title}</head><body> <table width="100%"><tr><td style="text-align: left;"> <parser:foreach array="$menu" value="$menu_item"> {$menu_item ['text'] } </parser:foreach> </td><td style="text-align: left;"></parser:new><p><parser:new name="footer"> </td></tr></table></body></html></parser:new></div></blockquote>"cart.gui"<blockquote style="border: thin solid rgb(224, 224, 224); padding: 3px 15px; float: none;"><div><parser:new name="show_cart"> <parser:link name="header" src="main.gui"> <table><tr><td style="text-align: left;">Name</td><td style="text-align: left;">Quantity</td><td style="text-align: left;">Total Price</td></tr> <parser:new name="cart_item"> <tr> <td style="text-align: left;">{$item ['name'] }</td> <td style="text-align: left;">{$item ['qty'] }</td> <td style="text-align: left;">{$item ['price'] }</td> </tr> </parser:new> </table> <parser:link name="footer"></parser:new></div></blockquote><p>Note in the example above "main.gui" has two templates in it.<p>Some set-up and looping examples. First the "foreach" loop method. Let's start with PHP also.<blockquote style="border: thin solid rgb(224, 224, 224); padding: 3px 15px; float: none;"><div>require_once( "chip_gui.php" );<p>$gui = new Chip;<p>$gui->gui_directory = "gui/";$gui->php_directory = "php/";<p>// .gui is optional$gui->parse_file( "main" );<p>$menu = array( array( "link"=>"http://www.page.com/main.php", "text"=>"Main" ), array( "link"=>"http://www.page.com/cart.php", "text"=>"Cart" ) );$gui->variables ['menu'] = $menu;</div></blockquote><p>Now that the main template variables are set up, the cart gets parsed next using the second loop option that is not "foreach".<blockquote style="border: thin solid rgb(224, 224, 224); padding: 3px 15px; float: none;"><div>$gui->parse_file( "cart" );<p>// Get the cart from the DB$query_result = mysql_query( "SELECT name, qty, price FROM carts WHERE customer_id={$customer_id}" );<p>while( $row = mysql_fetch_assoc( $query_result ) ){ // Set the price to the total price $row ['price'] = $row ['price'] * $row ['qty'] ; // Let the $gui->variables ['item'] array reflect $row $gui->variables ['item'] = $row;<p> // Parse the template appending the newly parsed data to the end of the cache $gui->parse_template( "cart_item" );}<p>mysql_free_result( $query_result );<p>// Show the template, the rest of the linked templates will follow$gui->output_template( "show_cart" );</div></blockquote><p>The same GUI file can be parsed with Perl using a little bit of perl code.<p><blockquote style="border: thin solid rgb(224, 224, 224); padding: 3px 15px; float: none;"><div>require "chip_gui.pm";<p>my $gui = new Chip;<p>$gui->{'gui_directory'} = "gui/";$gui->{'pl_directory'} = "pl/";<p># .gui is optional$gui->parse_file( "main" );<p>my $menu = { { "link"=>"http://www.page.com/main.php", "text"=>"Main" }, { "link"=>"http://www.page.com/cart.php", "text"=>"Cart" } };$gui->{'variables'}->{'menu'} = $menu;</div></blockquote><p>Onto the cart processing file.<blockquote style="border: thin solid rgb(224, 224, 224); padding: 3px 15px; float: none;"><div>$gui->parse_file( "cart" );<p># Get the cart from the DB using DBImy $query_result = $mysql_dbi->prepare( "SELECT name, qty, price FROM carts WHERE customer_id={$customer_id}" );<p>while( my $row = $query_result->fetchrow_hashref() ){ # Set the price to the total price $row->{'price'} = $row->{'price'} * $row->{'qty'}; # Let the $gui->variables ['item'] array reflect $row $gui->{'variables'}->{'item'} = $row;<p> # Parse the template appending the newly parsed data to the end of the cache $gui->parse_template( "cart_item" );}<p>// Show the template, the rest of the linked templates will follow$gui->output_template( "show_cart" );</div></blockquote><p><strong>ee also</strong><p>* <r>Smarty</r>, the more popular template engine for PHP<br>* <p><strong>External links</strong><p>* [<small><i>http://code.divineaspirations.net/chip Chip Home Page</i></small>] <br>* [<small><i>http://code.divineaspirations.net/chip/chip_download.html Download</i></small>] <br>* [<small><i>http://code.divineaspirations.net/chip/chip_guide.html Comprehensive Guide</i></small>] </dd> <br /> <p id="TerminSourceInfo" class="src"> <em><span itemprop="source">Wikimedia Foundation</span>. <span itemprop="source-date">2010</span>.</em> </p> </div> </dl> </div> <div style="padding:5px 0px;"> <a href="https://games.academic.ru" style="color: green; font-size: 16px; margin-right:10px"><b>Игры ⚽</b></a> <b><a title='онлайн-сервис помощи студентам' href='https://homework.academic.ru/?type_id=11'>Нужна курсовая?</a></b></div> <div id="socialBar_h"></div> <ul class="post-nav" id="TerminsNavigationBar"> <li class="prev"><a href="https://en-academic.com/dic.nsf/enwiki/3391422" title="Qooxdoo">Qooxdoo</a></li> <li class="next"><a href="https://en-academic.com/dic.nsf/enwiki/3391427" title="Kalvanin Kadhali">Kalvanin Kadhali</a></li> </ul> <div class="other-info"> <div class="holder"> <h3 class="dictionary">Look at other dictionaries:</h3> <ul class="terms-list"> <li><p><strong><a href="//en-academic.com/dic.nsf/enwiki/960426">Template engine (web)</a></strong> — A (web) template engine is software that is designed to process web templates and content information to produce output web documents. It runs in the context of a template system.pecific types of template engines Template engine is ordinarily… …   <span class="src2">Wikipedia</span></p></li> <li><p><strong><a href="//en-academic.com/dic.nsf/enwiki/3825592">Template processor</a></strong> — A template processor (also known as a template engine or a template parser )is software or a software component that is designed to combine one or more templates with a data model to produceone or more result documents.cite book last = Niemeyer… …   <span class="src2">Wikipedia</span></p></li> <li><p><strong><a href="//en-academic.com/dic.nsf/enwiki/125465">List of file formats</a></strong> — This is an incomplete list, which may never be able to satisfy particular standards for completeness. You can help by expanding it with reliably sourced entries. See also: List of file formats (alphabetical) This is a list of file formats… …   <span class="src2">Wikipedia</span></p></li> <li><p><strong><a href="//en-academic.com/dic.nsf/enwiki/1188930">Cellular neural network</a></strong> — Cellular neural networks (CNN) are a parallel computing paradigm similar to neural networks, with the difference that communication is allowed between neighbouring units only. Typical applications include image processing, analyzing 3D surfaces,… …   <span class="src2">Wikipedia</span></p></li> <li><p><strong><a href="//en-academic.com/dic.nsf/enwiki/2367970">Sound Blaster Audigy</a></strong> — is a sound card series from Creative Technology. It is a PCI add on board for PCs.The Sound Blaster Audigy featured the Audigy processor (EMU10K2), an improved version of the EMU10K1 processor that shipped with the Sound Blaster Live!.First… …   <span class="src2">Wikipedia</span></p></li> <li><p><strong><a href="//en-academic.com/dic.nsf/enwiki/1664054">Multi-core</a></strong> — A multi core processor (or chip level multiprocessor, CMP) combines two or more independent cores into a single package composed of a single integrated circuit (IC), called a die, or more dies packaged together. The individual core is normally a… …   <span class="src2">Wikipedia</span></p></li> <li><p><strong><a href="//en-academic.com/dic.nsf/enwiki/730732">Paul Collingwood</a></strong> — Infobox cricketer biography playername = Paul Collingwood female = country = England fullname = Paul David Collingwood nickname = Colly, Shep living = true dayofbirth = 26 monthofbirth = 5 yearofbirth = 1976 placeofbirth = Shotley Bridge, County… …   <span class="src2">Wikipedia</span></p></li> <li><p><strong><a href="//en-academic.com/dic.nsf/enwiki/2843431">2007 NASCAR Nextel Cup Series</a></strong> — season Previous: 2006 Next: 2008 …   <span class="src2">Wikipedia</span></p></li> <li><p><strong><a href="//universalium.en-academic.com/221544/United_States">United States</a></strong> — a republic in the N Western Hemisphere comprising 48 conterminous states, the District of Columbia, and Alaska in North America, and Hawaii in the N Pacific. 267,954,767; conterminous United States, 3,022,387 sq. mi. (7,827,982 sq. km); with… …   <span class="src2">Universalium</span></p></li> <li><p><strong><a href="//en-academic.com/dic.nsf/enwiki/100337">Parallel computing</a></strong> — Programming paradigms Agent oriented Automata based Component based Flow based Pipelined Concatenative Concurrent computing …   <span class="src2">Wikipedia</span></p></li> </ul> </div> </div> </div> </div> <div id="ad_bottom"> </div> <div id="footer"> <div id="liveinternet"> <!--LiveInternet counter--><a href="https://www.liveinternet.ru/click;academic" target="_blank"><img id="licntC2FD" width="31" height="31" style="border:0" title="LiveInternet" src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAEALAAAAAABAAEAAAIBTAA7" alt=""/></a><script>(function(d,s){d.getElementById("licntC2FD").src= "https://counter.yadro.ru/hit;academic?t44.6;r"+escape(d.referrer)+ ((typeof(s)=="undefined")?"":";s"+s.width+"*"+s.height+"*"+ (s.colorDepth?s.colorDepth:s.pixelDepth))+";u"+escape(d.URL)+ ";h"+escape(d.title.substring(0,150))+";"+Math.random()}) (document,screen)</script><!--/LiveInternet--> </div> <div id="restrictions">18+</div> © Academic, 2000-2024 <ul> <li> Contact us: <a id="m_s" href="#">Technical Support</a>, <a id="m_a" href="#">Advertising</a> </li> </ul> <div> <a href="//partners.academic.ru/partner_proposal.php">Dictionaries export</a>, created on PHP, <div class="img joom"></div> Joomla, <div class="img drp"></div> Drupal, <div class="img wp"></div> WordPress, MODx. </div> </div> <div id="contextmenu"> <ul id="context-actions"> <li><a data-in="masha">Mark and share</a></li> <li class="separator"></li> <!--li><a data-in="dic">Look up in the dictionary</a></li--> <!--li><a data-in="synonyms">Browse synonyms</a></li--> <li><a data-in="academic">Search through all dictionaries</a></li> <li><a data-in="translate">Translate…</a></li> <li><a data-in="internet">Search Internet</a></li> <!--li><a data-in="category">Search in the same category</a></li--> </ul> <ul id="context-suggestions"> </ul> </div> <div id="sharemenu" style="top: 1075px; left: 240px;"> <h3>Share the article and excerpts</h3> <ul class="smm"> <li class="vk"><a onClick="share('vk'); return false;" href="#"></a></li> <li class="ok"><a onClick="share('ok'); return false;" href="#"></a></li> <li class="fb"><a onClick="share('fb'); return false;" href="#"></a></li> <li class="tw"><a onClick="share('tw'); return false;" href="#"></a></li> <li class="gplus"><a onClick="share('g+'); return false;" href="#"></a></li> <li class="mailru"><a onClick="share('mail.ru'); return false;" href="#"></a></li> <li class="lj"><a onClick="share('lj'); return false;" href="#"></a></li> <li class="li"><a onClick="share('li'); return false;" href="#"></a></li> </ul> <div id="share-basement"> <h5>Direct link</h5> <a href="" id="share-directlink"> … </a> <small> Do a right-click on the link above<br/>and select “Copy Link” </small> </div> </div> <script async src="/js/academic.min.js?150324" type="text/javascript"></script> <div id="ad_right"> <div class="banner"> <div class="EAC_ATFR_300n"></div> </div> <div class="banner sticky"> <div class="EAC_MIDR_300c"></div> </div> </div> </div> <!-- /59581491/bottom --> <div id='div-gpt-ad-bottom'> <script> googletag.cmd.push(function() { googletag.display('div-gpt-ad-bottom'); }); </script> </div> <div id='div-gpt-ad-4fullscreen'> </div> <br/><br/><br/><br/><br/><br/> <!-- Yandex.Metrika counter --> <script type="text/javascript" > (function(m,e,t,r,i,k,a){m[i]=m[i]||function(){(m[i].a=m[i].a||[]).push(arguments)}; m[i].l=1*new Date();k=e.createElement(t),a=e.getElementsByTagName(t)[0],k.async=1,k.src=r,a.parentNode.insertBefore(k,a)}) (window, document, "script", "https://mc.yandex.ru/metrika/tag.js", "ym"); ym(70309897, "init", { clickmap:false, trackLinks:true, accurateTrackBounce:false }); </script> <noscript><div><img src="https://mc.yandex.ru/watch/70309897" style="position:absolute; left:-9999px;" alt="" /></div></noscript> <!-- /Yandex.Metrika counter --> <!-- Begin comScore Tag --> <script> var _comscore = _comscore || []; _comscore.push({ c1: "2", c2: "23600742" }); (function() { var s = document.createElement("script"), el = document.getElementsByTagName("script")[0]; s.async = true; s.src = (document.location.protocol == "https:" ? "https://sb" : "http://b") + ".scorecardresearch.com/beacon.js"; el.parentNode.insertBefore(s, el); })(); </script> <noscript> <img src="//b.scorecardresearch.com/p?c1=2&c2=23600742&cv=2.0&cj=1" /> </noscript> <!-- End comScore Tag --> </body> <link rel="stylesheet" href="/css/academic_v2.css?20210501a" type="text/css" /> <link rel="stylesheet" href="/css/mobile-menu.css?20190810" type="text/css" /> </html>