Passing and using variables in SMARTY tags (among other things)

I’m posting this because I could find NO answers to this question anywhere.

I wanted to pass a Smarty variable (for example {$hello}) through to the PHP side in Serendipity, get an extended property from the Serendipity database, run a query on the SMF database based on that property, and echo the output.

Comments are ***, which should be removed when copying and pasting.

In entries.tpl:

***Checks if the entry is one its own page or not.
{if !$is_single_entry}

***Assigns a new variable from a Smarty variable  {$entry.id}.  Note the backticks (``) around the variable name.
   {assign var='entryid' value=`$entry.id`}

***Capture the output of the Smarty function, within which is passed the variable $entryid
   {capture name=plural}
        {numbersmfcomments entryid="$entryid"}
   {/capture}

***Outputs some HTML with the Smarty function.  Distinguishes "Comment" or "Comments" based on the capture output.
   {numbersmfcomments entryid="$entryid"}
   {if $smarty.capture.plural == 1}
        Comment
   {else}
 Comments
   {/if}
   --Add yours!
{/if}

 

In config.inc.php:

 

 

***Makes a new Smarty function
$serendipity['smarty']->register_function('numbersmfcomments', 'numbersmfcomments');
function numbersmfcomments($params, &$smarty)  {

***Gets access to the SMF database and Serendipity globals.
require_once('/home/pmp6nl/public_html/talk/SSI.php');
global $serendipity;

***Gets entry id out of passed variable in Smarty function.
$id=$params['entryid'];

***Gets extended info about entries, which is where the SMF topic ID is stored.
$props = serendipity_fetchEntryProperties($id);
$SMFTopic =  $props['ep_SMFTopic'];

***Run a query to count the number of messages in that SMF topic.
$result = mysql_query("SELECT * FROM pmp6nl_talk.smf_messages WHERE ID_TOPIC = '$SMFTopic'");
$numbercomments = mysql_num_rows($result);

***Output that number, minus the original message.
echo $numbercomments-1;
}

I hope this may be helpful to someone in the future.

References:
http://www.s9y.org/78.html#A4
http://www.smarty.net/manual/en/language.custom.functions.php#language.function.assign
http://www.programmingtalk.com/showthread.php?t=11586
http://www.smarty.net/manual/en/language.builtin.functions.php#language.function.capture

 

One thought on “Passing and using variables in SMARTY tags (among other things)”

Leave a Reply

Your email address will not be published. Required fields are marked *