This topic will contain an expanding list of examples of snippets of Smarty code.

Example 1 - New format for report formatter

Note. To access current index in loop you have to name it and use the following syntax to get value:


{foreach $results as $r name=mainloop}

 {if (isset($heurist))}{$r = $heurist->getRecord($r)}{/if}

 {if $mainloop.index eq 0}

....................

 {/if}

{/foreach}


From now the query for smarty returns only list of record IDs. Consequently, all relations and pointer fields contain record ID only. As a result, the performance has been increased significantly. Thus, we need to obtain all records data in code of report template. To achieve this goal we provide $heurist object. This object has three public methods:

  • getRecord - returns a record by recID or reload record if record array is given as parameter
  • getRelatedRecords - returns an array of related record for given recID or record array
  • getWootText  - returns text related with given record ID


So the correct code is:


{$artist = $r.f403}

{if (isset($heurist))}{$artist = $heurist->getRecord($r.f403)}{/if}  {* check $heurist presence for backward capability *}

             <a href={$artworklink1|cat:$artist.recID|cat:$artworklink2}>

             <h2 class="Artwork">{$artist.recTitle}</h2></a><br/>

 

Main loop and other loops now looks like:


  {foreach $results as $r} {* Start records loop, do not remove *}

     {if (isset($heurist))}{$r = $heurist->getRecord($r)}{/if}


     {foreach $r.f333s as $s name=ploop} {* loop for resource field *}

            {if (isset($heurist))}{$s = $heurist->getRecord($s)}{/if}


     {/foreach}        


{/foreach}


To show thumbnails instead of full size images (see "collection_artist_narrative_artem.tpl"):


show first thumbnail

{if ($s.f8_originalvalue[0]['thumbURL'])}

   thumb 1:<img src={$s.f8_originalvalue[0]['thumbURL']} width="150"/>

 {/if}


show all thumbnails for image:

{wrap var=$s.f8_originalvalue dt="file" width="150" height="auto" mode="thumbnail"}

Example 2

Here's a really useful example of more advanced Smarty use, specifically:

  1. Pulling in all related or pointed-to or pointed-at records.
  2. Counting records and field values.
  3. Formatting.
  4. Adding a counter.

#1 combined with #2 is particularly powerful in terms of users being able to produce analytical results (e.g. for excavations).


This is a CSV output with counting of linked features:


<b>No., SiteName, Country, X, Y, Excavators,Linked Features,All linked</b><br/>


{* use to count any records which are not output, to be reported at the end *}

{$nonsite=0}


{*------------------------------------------------------------*}

{foreach $results as $r} {* Start records loop, do not remove *}

{$r = $heurist->getRecord($r)}

{*------------------------------------------------------------*}


    {if ($r.recTypeID=="12")}{* Site *}


     {* populate arrays with a list of linked Heurist records IDS *}      

     {$all_linked_records = $heurist->getLinkedRecords($r)}  {* get all linked records *}

     {$linked_features = $heurist->getLinkedRecords($r,24)}  {* get all linked Features (type 24) *}


     {$r.recID},{*Record ID*}

     {$r.f1},{*Site Name*}

     {$r.f26.term},{*Country >> Term*}


     {* example of formatting with Smarty functions *}

     {$r.f28|replace:"POINT(":""|replace:")":""|replace:" ":", "},{*Site Location*}


     {* example of counting elements of an array through formatting and through counting *}

     {$r.f139s|@count} {*Number of Excavators*},

     {count($linked_features.linkedfrom)},

     {count($all_linked_records.linkedfrom)}

     

<br/> {* line breaks between each record *}

{else}

{$nonsite=$nonsite+1}

   

{/if}

       


{*------------------------------------------------------------*}

{/foreach} {* end records loop, do not remove *}

{*------------------------------------------------------------*}


{if ($nonsite>0)}

 

  <br/>There are {$nonsite} records in the results which are not site records

 

{/if}

Created with the Personal Edition of HelpNDoc: Free help authoring tool