I’m trying to create a survey module, which is working well, however, the template parser always skips the first replacement. If I have this:
...
<dl>
{exp:survey:questions surveyId="1" }
<dt>{text}</dt>
<dd>
<ul>
{choices}<li>{choice_id} {choice_text}</li>{/choices}
</ul>
</dd>
{/exp:survey:questions}
...
what I end up with is this:
...
<dl>
<dt>{text}</dt>
<dd>
<ul>
{choices}<li>{choice_id} {choice_text}</li>{/choices}
</ul>
</dd>
<dt>How much wood could a woodchuck chuck?</dt>
<dd>
<ul>
<li>Foo Lorem ipsum quare id faciam fortasse requiris.</li>
</ul>
</dd>
</dl>
...
it appears that during the first iteration, the variables don’t get replaced at all, but during the second iteration things work just fine. It should also be noted that the loop goes through n + 1 times, where n is the number of questions, and all of the questions and their choices appear like they are supposed to; I just have this extra unparsed code snippet. Here is the function I use to parse:
I was unable to post the code in the main post, so here it is in three parts:
public function questions( $group = 0 ) {
global $TMPL, $OUT, $IN, $FNS;
// Get Questions
$questions = SurveyQuestion::listing( $this->id, 0, 0, $group );
// Grab the question ids
$qids = array();
foreach( $questions as $q )
$qids[] = $q->get( 'id' );
// Get Response Choices
$choices = SurveyChoice::listing( 0, 0, 0, $qids );
foreach( $questions as $q ) {
// Copy question's choices
$cs = array();
foreach( $choices as $c )
if( $c->get( 'questionId' ) == $q->get( 'id' ) )
$cs[] = $c;
// Copy responses
$responses = SurveyResponse::listing( $this->id, $q->get( 'id' ), 0, 0, 0 );
$rs = array();
foreach( $responses as $r ) {
$cid = ( $r->get( 'choiceId' ) == 1 ) ? "free" : $r->get( 'choiceId' );
if( !isset( $rs[ $cid ] ) ) {
$rs[ $cid ] = array();
}
array_push( $rs[ $cid ], $r );
}
// Get the tag data
$tagdata = $TMPL->tagdata;
// Copy the question's single variables
foreach( $TMPL->var_single as $key => $value ) {
if( $key == "question_id" )
$tagdata = $TMPL->swap_var_single( $key, $q->get( 'id' ), $tagdata );
if( in_array( $key, array( 'text', 'id' ) ) )
if( $q->get( $key ) !== null )
$tagdata = $TMPL->swap_var_single( $key, $q->get( $key ), $tagdata );
}
// Copy the question's choices
foreach( $TMPL->var_pair as $key => $value ) {
// Get the data from between the tag pairs
$matches = $TMPL->fetch_data_between_var_pairs( $tagdata, $key );
$matches = $FNS->prep_conditionals( $matches, array( 'choice_type' ) );
$output = '';
if( $key == "choices" ) {
foreach( $cs as $c ) {
if( $c->get( 'questionId' ) != $q->get( 'id' ) ) continue;
$pair = $matches;
$pair = $TMPL->swap_var_single( 'choice_id', $c->get( 'id' ), $pair );
$pair = $TMPL->swap_var_single( 'choice_text', $c->get( 'text' ), $pair );
$pair = $TMPL->swap_var_single( 'choice_extended', $c->get( 'extended' ), $pair );
$pair = $TMPL->swap_var_single( 'question_id', $q->get( 'id' ), $pair );
switch( $q->get( 'type' ) ) {
case 'mcr':
$type = 'radio';
break;
case 'mck':
$type = 'checkbox';
break;
case 'free':
default:
$type = 'text';
break;
}
$output .= $TMPL->swap_var_single( 'choice_type', $type, $pair );
}
// What if this is freeform text?
if( sizeof( $cs ) == 0 ) {
$matches = $TMPL->swap_var_single( 'choice_id', '1', $matches );
$matches = $TMPL->swap_var_single( 'question_id', $q->get( 'id' ), $matches );
$output .= $TMPL->swap_var_single( 'choice_type', 'text', $matches );
}
}
$tagdata = preg_replace( "/".LD.preg_quote($key).RD."(.*?)".LD.SLASH.$key.RD."/s", $output, $tagdata );
}
$this->return_data .= $tagdata;
}
return $this->return_data;
}
That was a bug in the copy/pasted code, but that isn’t the cause of the problem.
The code does actually replace the tags, but for some reason it skips the first iteration dumping one html snippet with the {tag} tags unparsed, and then picks up and parses the next iterations just fine. So if I have n replacements to perform between the tag pairs, I get n + 1 html snippets, the first of which fails to parse the inner tags.
Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.