Login

Refactor template files to use {% block %} tags.

Author:
rodrigoc
Posted:
August 8, 2008
Language:
Python
Version:
.96
Tags:
template refactor
Score:
0 (after 2 ratings)

Refactors two similar django template files to use {% block %} tags.

Takes two template files, where one is a modified version of the other, and looks for differences and similiarities between the two.

It then generates refactored (and renamed) versions of each file and a third 'base' file, adding an {% extends %} tag as appropriate.

Block tags are named with matching numbers in all 3 files, making it easy to do a search and replace with more meaningful labels.

sample_data_in_base controls whether or not the content from file 1 is copied into place in the base file as an example.

Problems: it doesn't identify open {% for %} or {% if %} tags, so this needs some manual fixing (moving the {% endfor %} and {% endif %} tags into the proper blocks). It doesn't add the correct path for the {% extends %} tag either (ie. "app/base.html"). collapse_whitespace is not working at all.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import difflib
import os.path
import Tkinter, tkFileDialog
root = Tkinter.Tk()
root.withdraw()

sample_data_in_base = False
collapse_whitespace = True
base_file_name = "base.html"

def ignore_ws(c):
    return c in " \t\n\r"

filename1 = tkFileDialog.askopenfilename(title= "First file?")
f1 = file(filename1,'r').readlines()
filename2 = tkFileDialog.askopenfilename(title= "Second file?")
f2 = file(filename2,'r').readlines()

d = difflib.ndiff(f1,f2, charjunk=ignore_ws)

base = ""
r1 = r2 = '{% extends "'+base_file_name+'" %}'

block_open = False
block = 0

for line in d:
    # equal line, goes into base, not r1 or r2
    if line[:2] == "  ":
        #block tag open? close it
        if block_open:
            tag = "{% endblock " + str(block) + " %}\n"
            base += tag
            r1 += tag
            r2 += tag
            block += 1
            block_open = False
        base += line[2:]
        continue
    # else: different line
    # block tag closed? open it
    if not block_open:
        tag = "{% block " + str(block) + " %}\n"
        base += tag
        r1 += tag
        r2 += tag
        block_open = True
    # line from f1? put into r1 (and maybe base)
    if line[:2] == "- ":
        r1 += line[2:]
        if sample_data_in_base:
            base += line[2:]
    # line from f2? put into r2
    if line[:2] == "+ ":
        r2 += line[2:]

for name,text in [(base_file_name,base),(filename1,r1),(filename2,r2)]:
    root,ext = os.path.splitext(name)
    if text!=base:
        name = root+"_refactored"+ext
    f=file(name,'w')
    f.writelines(text)
    f.close()

More like this

  1. Decorate Template Tag (In-Line include and extend with local context) by rhomber 5 years, 3 months ago
  2. Repeat blocks with new context / simple Jinja-like macro system by miracle2k 7 years, 9 months ago
  3. Render specific blocks from templates (useful for AJAX, alternative) by zbyte64 6 years, 8 months ago
  4. escapejs block tag by baumer1122 7 years, 2 months ago
  5. Markdown and Syntax Highlighting in Django by blinks 8 years, 1 month ago

Comments

andybak (on August 13, 2008):

WARNING

I already had a base.html and it overwrote it without a prompt. Now that will teach me about reading the instructions and remembering to use version control!

#

rodrigoc (on August 15, 2008):

Woops. Sorry!

#

Please login first before commenting.