|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
samjoseph Smarty n00b
Joined: 29 Jun 2004 Posts: 3
|
Posted: Tue Jun 29, 2004 10:57 pm Post subject: complex variables in assign value |
|
|
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 |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Tue Jun 29, 2004 11:05 pm Post subject: |
|
|
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 |
|
samjoseph Smarty n00b
Joined: 29 Jun 2004 Posts: 3
|
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Wed Jun 30, 2004 4:16 pm Post subject: |
|
|
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 |
|
samjoseph Smarty n00b
Joined: 29 Jun 2004 Posts: 3
|
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Thu Jul 01, 2004 6:45 am Post subject: |
|
|
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 |
|
|
|
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
|