Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

complex variables in assign value

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Tips and Tricks
View previous topic :: View next topic  
Author Message
samjoseph
Smarty n00b


Joined: 29 Jun 2004
Posts: 3

PostPosted: Tue Jun 29, 2004 10:57 pm    Post subject: complex variables in assign value Reply with quote

Hi All,

So I was struggling to get the smarty assign function to accept complex variables like calling methods on objects, and even things like $smarty.foreach.questionnaire.iteration

I've managed to get round this with a total hack on the compiler.assign.php function. Specifically I added this code:

$match = array("(\$this",").\"",")\"","'])");
$replace = array("\$this","",")","']");
$_params['value'] = str_replace($match,$replace,$_params['value']);

before the final return, i.e.

return "\$this->assign({$_params['var']}, {$_params['value']});";

Now I'm not a php or smarty veteran so I'm sure this is a real hack and not the right way to do things, but it allows me to handle a nested template scheme that I'm using for dynamic building of a web questionnaire:

Here's an example of some smarty code I can now use:

{foreach name=questionnaire from=$questionnaire_questions item=questionnaire_question}
{assign var="number" value="`$smarty.foreach.questionnaire.iteration`"}
{assign var="question" value="$questionnaire_question->getQuestion()"}
{assign var="question_answers" value="$question->getQuestionAnswers()"}
{assign var="question_template" value="$questionnaire_question->getQuestionTemplate()"}
{include file="$question_template"}
{/foreach}

What this means is that I can loop through all the questions in my questionnaire and then pass through variables to the individual question templates like the following:

<tr>
<td class="evaluationnumber">{$number}</td>
<td class="evaluationitem">{$question->question}</td>
</tr>
<tr>
<td class="evaluationitem">&</td>
<td class="evaluationitem">
<select name="{$question->_id}" size="5">
{foreach from=$question_answers item=question_answer}
<option value="{$question_answer->getAnswerId()}">{$question_answer->getAnswerText()}</option>
{/foreach}
</select>
</td>
</tr>

which then in turn loop through the answers for each question. This allows me to flexibly incorporate different questions and question templates into my questionnaire by making full use of the class structure I designed to help me make sense of things.

I would be very interested on any comments on this approach. Perhaps there is a simple way to achieve what I have here without a nasty hack - if so I would be very grateful to hear about it. In addition I would be very interested to hear any reasons why I perhaps shouldn't be taking this approach, or how I can modify my hack to make it more generic and not such a hack.

BTW I am using php 4.3 and smarty 2.6.2

Many thanks in advance.

CHEERS> SAM


Last edited by samjoseph on Wed May 05, 2010 10:53 pm; edited 1 time in total
Back to top
View user's profile Send private message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Tue Jun 29, 2004 11:05 pm    Post subject: Reply with quote

You shouldn't need to change Smarty if you drop the quotes:

{foreach name=questionnaire from=$questionnaire_questions item=questionnaire_question}
{assign var="number" value=$smarty.foreach.questionnaire.iteration}
{assign var="question" value=$questionnaire_question->getQuestion()}
{assign var="question_answers" value=$question->getQuestionAnswers()}
{assign var="question_template" value=$questionnaire_question->getQuestionTemplate()}
{include file=$question_template}
{/foreach}
Back to top
View user's profile Send private message
samjoseph
Smarty n00b


Joined: 29 Jun 2004
Posts: 3

PostPosted: Wed Jun 30, 2004 11:50 am    Post subject: Reply with quote

Ah yes that works - many thanks. Any chance we could add info to that effect to the manual, or did I overlook something obvious?
At least I gave myself a short intro intro into smarty plugins.

CHEERS> SAM


Last edited by samjoseph on Wed May 05, 2010 10:53 pm; edited 1 time in total
Back to top
View user's profile Send private message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Wed Jun 30, 2004 4:16 pm    Post subject: Reply with quote

How would you suggest something like that be included in the manual? Generally, you quote static text and not variables. To allow some insertion of variable text into your static text, backticks were introduced (around 2.5.0) so that you can embed vars into double quoted text. AFAIK, backticks do not allow all variable and function derefrences that are normally available, but for most purposes work well. (Another thing to do is use the "cat" modifier).

Generally, variable dereferences shouldn't be quoted and IMO, that is the typical way that variables are treated in most languages/systems.

Greetings
Back to top
View user's profile Send private message
samjoseph
Smarty n00b


Joined: 29 Jun 2004
Posts: 3

PostPosted: Thu Jul 01, 2004 2:57 am    Post subject: Reply with quote

I would suggest adding an example to show that it is possible to use the assign function without using quotes on the value attribute. I have no particular interest in achieving any additional embedding in quotes. I believe the current example in the 2.6.2 manual is as follows:

-----------------------

Example 8-1. assign

{assign var="name" value="Bob"}

The value of $name is {$name}.

OUTPUT:

The value of $name is Bob.

-------------------------

I wasn't able to infer from this that I could leave off the quotes to handle variables - I didn't know I could do that till you told me about it - so how about adding another example like this:

-----------------------

Example 8-1.1 assign

{assign var="name" value=$person->name}
{assign var="email" value=$person->email}

name: {$name}<br>
email: {$email}<br>

OUTPUT:

name: Zaphod Beeblebrox<br>
email:
zaphod@slartibartfast.com<br>

-------------------------

If this example had been present I think I probably would have come to the solution you suggested much faster. I'm just thinking that if something like this was added then it might help other people trying to do the same thing in future.

Many thanks again.

CHEERS> SAM


Last edited by samjoseph on Wed May 05, 2010 10:53 pm; edited 1 time in total
Back to top
View user's profile Send private message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Thu Jul 01, 2004 6:45 am    Post subject: Reply with quote

Thanks for your comments and examples. I think I need to read the manual again myself--as a long-time Smarty user, I sometimes forget what is "obvious" and what is not.

Cheers!
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Tips and Tricks All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP