...

Source file src/github.com/noirbizarre/gonja/ext/django/statements/firstof.go

Documentation: github.com/noirbizarre/gonja/ext/django/statements

     1  package statements
     2  
     3  import (
     4  	// "github.com/noirbizarre/gonja/exec"
     5  	"fmt"
     6  
     7  	"github.com/noirbizarre/gonja/exec"
     8  	"github.com/noirbizarre/gonja/nodes"
     9  	"github.com/noirbizarre/gonja/parser"
    10  	"github.com/noirbizarre/gonja/tokens"
    11  )
    12  
    13  type FirstofStmt struct {
    14  	Location *tokens.Token
    15  	Args     []nodes.Expression
    16  }
    17  
    18  func (stmt *FirstofStmt) Position() *tokens.Token { return stmt.Location }
    19  func (stmt *FirstofStmt) String() string {
    20  	t := stmt.Position()
    21  	return fmt.Sprintf("FirstofStmt(Args=%s, Line=%d Col=%d)", stmt.Args, t.Line, t.Col)
    22  }
    23  
    24  func (stmt *FirstofStmt) Execute(r *exec.Renderer, tag *nodes.StatementBlock) error {
    25  	for _, arg := range stmt.Args {
    26  		val := r.Eval(arg)
    27  		if val.IsError() {
    28  			return val
    29  		}
    30  
    31  		if val.IsTrue() {
    32  			r.RenderValue(val)
    33  			return nil
    34  		}
    35  	}
    36  
    37  	return nil
    38  }
    39  
    40  func firstofParser(p *parser.Parser, args *parser.Parser) (nodes.Statement, error) {
    41  	stmt := &FirstofStmt{
    42  		Location: p.Current(),
    43  	}
    44  
    45  	for !args.End() {
    46  		node, err := args.ParseExpression()
    47  		if err != nil {
    48  			return nil, err
    49  		}
    50  		stmt.Args = append(stmt.Args, node)
    51  	}
    52  
    53  	return stmt, nil
    54  }
    55  
    56  func init() {
    57  	All.Register("firstof", firstofParser)
    58  }
    59  

View as plain text