1

I'm new to using smarty templates for my projects. I'm having a tough time getting this to work. This code works in my ".php" file:

echo $categories[$topics[9]['category_id']]['category_id'];

But both these (and other variations) fail in my ".tpl" file:

{$categories[$topics[9].category_id]['category_id']}
{$categories[$topics[9].category_id].category_id}

What syntax error am I making, how to get this to work?

3
  • have you tried {$categories[$topics[9]['category_id']].category_id} ? probably would make things easier in .tpl if you assign the variable in control anyway Commented Jul 10, 2014 at 17:31
  • Or {$categories[$topics[9].category_id].category_id}. Are you sure the right variables are given to the smarty template? Commented Jul 10, 2014 at 17:35
  • check smarty.net/docs/en/language.variables.tpl for reference Commented Jul 10, 2014 at 17:37

2 Answers 2

1

Smarty uses a different syntax to PHP. See this page on Smarty variable syntax.

However, as you can see, Smarty also allows PHP style syntax. So, your PHP code should work as is, simply removing echo and the semicolon at the end and replacing with curly brackets.

When I have multi-dimensional and nested arrays, sometimes I like to assign each element a variable to make it easier to read. So I might rewrite your variable:

{$categories[$topics[9]['category_id']]['category_id']}

To be:

{assign var="topic" value=$topics[9].category_id}
{$categories.$topic.category_id}

This will help you to reduce repetition and increase readability. Subsequently, it will be easier to debug.

Sign up to request clarification or add additional context in comments.

Comments

0

Smarty as almost all template engines has its own syntax.

As far as I remember (someone can correct me if I'm wrong) it was impossible to use some syntax in Smarty 2 and it was harder to code some obvious things from PHP. But now we have Smarty 3.1

In your question, you probably haven't assigned both categories and topics to Smarty so it couldn't work. The simple rule is that you need to assign each variable that you want to use in Smarty from PHP using $smarty->assign construct.

In your case you can simple do in PHP:

$topics = [];
$topics[9]['category_id'] = 'something';
$categories = [];
$categories['something']['category_id'] = 798897;


$smarty->assign('topics', $topics);
$smarty->assign('categories', $categories);

to assign both arrays from PHP to Smarty and in Smarty you can simple do:

{$categories[$topics[9]['category_id']]['category_id']}

As you see you display this value almost the same as in PHP (instead of echo you simple use { at the beginning and } at the end) ant that's it.

You can however use also a bit simpler syntax in Smarty:

{$categories[$topics.9.category_id].category_id}

Instead of using [..] you can use . but when using as above other variable as index you need to still use [..] syntax

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.