To combine zerkms's and Didozavar's answers, here's a reusable modifier function:
<?php
function jsify($object, $name = 'foo'){
$json_object = json_encode($object);
return "<script type=\"text/javascript\"> var $name = $json_object; </script>";
}
$smarty->register_modifier("jsify", "jsify");
$smarty->assign("foo", $foo);
$smarty->assign("bar", $bar);
?>
{* In Template *}
{$foo|jsify}
{$bar|jsify:bar}
{* Verify *}
<script type="text/javascript">
// assuming you're using a browser that supports console
console.log(foo);
console.log(bar);
</script>
Edit:
After thinking about this, it might be more consistent to make it mirror the template function {assign} with a custom function.
<?php
function assign_to_javascript($params, $template){
// validate params values
$name = $params['var'];
$json_object = json_encode($params['value']);
echo "<script type=\"text/javascript\"> var $name = $json_object; </script>";
}
$smarty->register_function("assign_to_javascript", "assign_to_javascript");
$smarty->assign("foo", $foo);
$smarty->assign("bar", $bar);
?>
{* In Template *}
{assign_to_javascript var="foo" value=$foo}
{assign_to_javascript var="bar" value=$bar}
{* Verify *}
<script type="text/javascript">
// assuming you're using a browser that supports console
console.log(foo);
console.log(bar);
</script>