-
-
Notifications
You must be signed in to change notification settings - Fork 275
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
175 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
package pongo2 | ||
|
||
import ( | ||
"bytes" | ||
) | ||
|
||
type tagIfchangedNode struct { | ||
watched_expr []INodeEvaluator | ||
last_values []*Value | ||
last_content []byte | ||
thenWrapper *NodeWrapper | ||
elseWrapper *NodeWrapper | ||
} | ||
|
||
func (node *tagIfchangedNode) Execute(ctx *ExecutionContext, buffer *bytes.Buffer) error { | ||
|
||
if len(node.watched_expr) == 0 { | ||
// Check against own rendered body | ||
|
||
buf := bytes.NewBuffer(make([]byte, 0, 1024)) // 1 KiB | ||
err := node.thenWrapper.Execute(ctx, buf) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
buf_bytes := buf.Bytes() | ||
if !bytes.Equal(node.last_content, buf_bytes) { | ||
// Rendered content changed, output it | ||
buffer.Write(buf_bytes) | ||
node.last_content = buf_bytes | ||
} | ||
} else { | ||
now_values := make([]*Value, 0, len(node.watched_expr)) | ||
for _, expr := range node.watched_expr { | ||
val, err := expr.Evaluate(ctx) | ||
if err != nil { | ||
return err | ||
} | ||
now_values = append(now_values, val) | ||
} | ||
|
||
// Compare old to new values now | ||
changed := len(node.last_values) == 0 | ||
|
||
for idx, old_val := range node.last_values { | ||
if !old_val.EqualValueTo(now_values[idx]) { | ||
changed = true | ||
break // we can stop here because ONE value changed | ||
} | ||
} | ||
|
||
node.last_values = now_values | ||
|
||
if changed { | ||
// Render thenWrapper | ||
err := node.thenWrapper.Execute(ctx, buffer) | ||
if err != nil { | ||
return err | ||
} | ||
} else { | ||
// Render elseWrapper | ||
err := node.elseWrapper.Execute(ctx, buffer) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func tagIfchangedParser(doc *Parser, start *Token, arguments *Parser) (INodeTag, error) { | ||
ifchanged_node := &tagIfchangedNode{} | ||
|
||
for arguments.Remaining() > 0 { | ||
// Parse condition | ||
expr, err := arguments.ParseExpression() | ||
if err != nil { | ||
return nil, err | ||
} | ||
ifchanged_node.watched_expr = append(ifchanged_node.watched_expr, expr) | ||
} | ||
|
||
if arguments.Remaining() > 0 { | ||
return nil, arguments.Error("Ifchanged-arguments are malformed.", nil) | ||
} | ||
|
||
// Wrap then/else-blocks | ||
wrapper, endargs, err := doc.WrapUntilTag("else", "endifchanged") | ||
if err != nil { | ||
return nil, err | ||
} | ||
ifchanged_node.thenWrapper = wrapper | ||
|
||
if endargs.Count() > 0 { | ||
return nil, endargs.Error("Arguments not allowed here.", nil) | ||
} | ||
|
||
if wrapper.Endtag == "else" { | ||
// if there's an else in the if-statement, we need the else-Block as well | ||
wrapper, endargs, err = doc.WrapUntilTag("endifchanged") | ||
if err != nil { | ||
return nil, err | ||
} | ||
ifchanged_node.elseWrapper = wrapper | ||
|
||
if endargs.Count() > 0 { | ||
return nil, endargs.Error("Arguments not allowed here.", nil) | ||
} | ||
} | ||
|
||
return ifchanged_node, nil | ||
} | ||
|
||
func init() { | ||
RegisterTag("ifchanged", tagIfchangedParser) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{% for comment in complex.comments2 %} | ||
{% ifchanged %}New comment from another user {{ comment.Author.Name }}{% endifchanged %} | ||
{% ifchanged comment.Author.Validated %} | ||
Validated changed to {{ comment.Author.Validated }} | ||
{% else %} | ||
Validated value not changed | ||
{% endifchanged %} | ||
{% ifchanged comment.Author.Name comment.Date %}Comment's author name or date changed{% endifchanged %} | ||
{% endfor %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
|
||
New comment from another user user1 | ||
|
||
Validated changed to True | ||
|
||
Comment's author name or date changed | ||
|
||
|
||
|
||
Validated value not changed | ||
|
||
Comment's author name or date changed | ||
|
||
New comment from another user user3 | ||
|
||
Validated changed to False | ||
|
||
Comment's author name or date changed |