Smarty and Javascript variables

Hi, I have been wracking my head trying to see if I can do this without adding extra OJS functions and DAO code.

I have a function which is returning an int variable. e.g.

{assign var="total" value=$someObject->getTotal($item->getTotal())} 

I want to sum up the $total’s and use them in another place on the same tpl page. I have been trying to do it in javacript and forms.

<form method="POST">
<script type="text/javascript">

function countTotals() {
var elements =;
var sum = 0;
for (var i=0; i < elements.length; i++) sum++; = sum;    


{assign var="total" value=$someObject->getTotal($item->getTotal())} 

<input type="hidden" name="total" id="total" value="{$total}" onchange="countTotals();" /> 

<input type="text" name="sum" value="" class="textFieldReadonly" readonly />

Any help appreciated or any suggestions or ideas on doing it a different way appreciated.

Hi @darko,

I think you’re mixing server-side (Smarty) variables with client-side (Javascript) ones – the two live totally separate lives. I suspect you can do what you’re looking for using purely Smarty. For example…

{assign var=total value=0}
{foreach from=$list item=val}
    {assign var=total value=$total+$val}
The total is... {$total}

(This is untested, but should give you an idea.)

Alec Smecher
Public Knowledge Project Team

thanks. that makes sense and a much easier solution.