Skip to content

Commit

Permalink
Implemented tuple unpacking! (hope it doesnt blow up!)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jackywathy committed Jan 24, 2017
1 parent 7ae6c8f commit 01f8b57
Showing 5 changed files with 46 additions and 16 deletions.
27 changes: 22 additions & 5 deletions template_engine/nodes.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import html
class TemplateRuntimeException(Exception):
pass



def head(string, n):
return string[:n] if len(string) >= n else string
@@ -29,7 +33,7 @@ def render(self, context):

class ForNode(Node):
def __init__(self, iterator, iterable):
self._iterator = iterator
self._iterators = iterator.split(',') # a list of the iterators
self._iterable = iterable
self._child = None

@@ -40,16 +44,29 @@ def set_child(self, child):
self._child = child

def __repr__(self):
return "<ForNode: '" + self._iterator + '; ' + self._child + "'>"
return "<ForNode: '" + self._iterators + '; ' + self._child + "'>"

def render(self, context):
# Evaluate the iterable so we know what to iterate over
iterable = eval(self._iterable, {}, context)
rendered_children = []
for i in iterable:
# Create a new context to include our variable
new_context = dict(context)
new_context[self._iterator] = i
# i may be a unzipped tuple:
if len(self._iterators) > 1:
if not (len(self._iterators) == len(i)):
raise ValueError("not enough values to unpack (expected %d, got %d)" % (len(self._iterators), len(i)))
# check that the unpacked item's length is equal to the number of iterators suppled

# Create a new context to include our variable(s)
new_context = dict(context)
for upto, variable in enumerate(self._iterators):
new_context[variable] = i[upto]
else:
# there is only one iterator, set it to i
new_context = dict(context)
new_context[self._iterators[0]] = i


# Render the child with our special context
rendered = self._child.render(new_context)
if rendered.strip():
5 changes: 4 additions & 1 deletion template_engine/parser.py
Original file line number Diff line number Diff line change
@@ -151,8 +151,11 @@ def identify_token(token):
elif ' '.join(term_list) == 'end if':
return create_token('end_if', None)
elif keyword == 'for':
print(term_list)
# The contents is a dictionary which contains the iterator and the iterable
return create_token('for', {'iterator': term_list[1], 'iterable': ' '.join(term_list[3:])})
in_loc = term_list.index('in')
# get the location of the in to split the query into iterator and iterable
return create_token('for', {'iterator': "".join(term_list[1:in_loc]), 'iterable': ' '.join(term_list[in_loc+1:])})
elif ' '.join(term_list) == 'end for':
return create_token('end_for', None)
elif keyword == 'comment':
18 changes: 9 additions & 9 deletions templates/index.html
Original file line number Diff line number Diff line change
@@ -10,15 +10,15 @@
<div class = 'post_list'>
{% for post in posts %}
<a href="view/{{post['id'] }}">
<fieldset class="image_fieldset">

<img class='post_image' src= "static/{{ post['image'] }}" alt= "{{ post['question'] }}">
<div class = 'post_text'>
<figcaption>
{{ post['question'] }}
</figcaption>
</div>
</fieldset></a>
<fieldset class="image_fieldset">
<figure>
<img class='post_image' src= "static/{{ post['image'] }}" alt= "{{ post['question'] }}">
<figcaption class="post_text">
{{ post['question'] }}
</figcaption>
</figure>
</fieldset>
</a>
{% end for %}
</div>
</body>
3 changes: 2 additions & 1 deletion templates/test.html
Original file line number Diff line number Diff line change
@@ -6,6 +6,7 @@

<body>
<p>Hello there</p>
<p>{{ fname }} {{ lname }} {{ food }}</p>
{% for i, z in enumerate(test) %}
<p>{{ i }} : {{ z }} : works! </p>
</body>
</html>
9 changes: 9 additions & 0 deletions testing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from template_engine.parser import render
from tornado.ncss import Server, ncssbook_log
def handler(request):
request.write(render("test.html", {'test': enumerate(range(10))}))

server = Server()
server.register(r'/test', handler)

server.run()

4 comments on commit 01f8b57

@tracey-le
Copy link
Collaborator

@tracey-le tracey-le commented on 01f8b57 Jan 24, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

weeeeww that's awesome @Jackywathy!

😂 nah won't go boom 💥 :P

@tracey-le
Copy link
Collaborator

@tracey-le tracey-le commented on 01f8b57 Jan 24, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

linking issue #26 for reference

@Jackywathy
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wow how do you link?

@tracey-le
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:D you just need to put the # symbol and then the number of the issue (the number will be next to the title). (thanks to Julia for showing me hehe 😃)

when you link it, it just mentions that you referenced it, like this:
screen shot 2017-01-24 at 2 17 51 pm

Please sign in to comment.