Loop_Node::compile()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 12
nc 2
nop 1
dl 0
loc 18
rs 9.8666
c 0
b 0
f 0
1
<?php
2
3
namespace Rarst\Meadow;
4
5
use Twig_Node;
6
use Twig_Compiler;
7
8
/**
9
 * Compiles loop nodes into WP loop with optional query node for secondary loops.
10
 */
11
class Loop_Node extends Twig_Node {
12
13
	/**
14
	 * @param Twig_Compiler $compiler
15
	 */
16
	public function compile( Twig_Compiler $compiler ) {
17
18
		$compiler->addDebugInfo( $this );
19
20
		if ( $this->hasNode( 'query' ) ) {
21
			$compiler
22
					->write( '$loop = new WP_Query(' )
23
					->subcompile( $this->getNode( 'query' ) )
24
					->raw( ");\n" )
25
					->write( 'while( $loop->have_posts() ) : $loop->the_post();' . "\n" ); // TODO nested loops
26
		}
27
		else {
28
			$compiler->write( 'while( have_posts() ) : the_post();' . "\n" );
29
		}
30
31
		$compiler
32
				->subcompile( $this->getNode( 'body' ) )
33
				->write( 'endwhile;' . "\n" );
34
	}
35
}