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  {$}.  Note the backticks (``) around the variable name.
   {assign var='entryid' value=`$`}          

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

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





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

***Gets access to the SMF database and Serendipity globals.
global $serendipity;

***Gets entry id out of passed variable in Smarty function.

***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.



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

  1. Nick says:

    Thanks a lot. You are a savior)

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.