Skip to content

Commit

Permalink
Moved alias() method from Template::TT3::Class into Badger::Class.
Browse files Browse the repository at this point in the history
Added some comments on the dire state of hash virtual methods.
  • Loading branch information
abw committed Nov 24, 2009
1 parent 65e250b commit b92340d
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 77 deletions.
2 changes: 1 addition & 1 deletion bin/tt3-html
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ use constant TEMPLATE => 'Template::TT3::Template';

my $file = shift || die usage();
my $template = TEMPLATE->new( file => $file );
my $tokens = $template->tokens->view_HTML;
my $tree = $template->tree;
my $tokens = $template->tokens->view_HTML;
$tree = $tree ? $tree->view_HTML : "ERROR: $@";
my $output = $template->try->html || qq{<div class="error">$@</div>};
my $wrapper = TEMPLATE->new( file => \*DATA );
Expand Down
5 changes: 2 additions & 3 deletions examples/fail.tt3
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
[% a +
b +
c + ^^^ # hello world! %]
x + f * y / g + (c + ^^^) # hello world! %]

This is parsing successfully. I'm not sure why the tag isn't detecting
the unrecognised '^^^' characters.
This should give a parse error.
57 changes: 0 additions & 57 deletions html/fail.html
Original file line number Diff line number Diff line change
@@ -1,57 +0,0 @@
<html>
<head>
<title>TT3 Template Tree: examples/fail.tt3</title>
<link rel="stylesheet" type="text/css" href="tt3.css">
<script type="text/javascript" src="jquery-1.3.2.min.js"></script>
<script type="text/javascript" src="tt3.js"></script>

</head>
<body>
<h1>TT3: examples/fail.tt3</h1>

<div class="tabset" id="tabset">
<ul class="tabs">
<li class="warm"><a href="#tokens">Tokens</a></li>
<li><a href="#tree">Tree</a></li>
<li><a href="#output">Output</a></li>
</ul>
<div class="panels">
<div class="panel warm" id="tokens">
<h2>Source Tokens</h1>
<div class="content">
<span class="tag"><span class="tag_start">[%</span><span class="whitespace"> </span><span class="word">a</span><span class="whitespace"> </span><span class="number">+</span><span class="whitespace">
<span class="nl"></span> </span><span class="word">b</span><span class="whitespace"> </span><span class="number">+</span><span class="whitespace">
<span class="nl"></span> </span><span class="word">c</span><span class="whitespace"> </span><span class="number">+</span><span class="whitespace"> </span><span class="word">^</span><span class="word">^</span><span class="word">^</span><span class="whitespace"> # hello world! </span><span class="tag_end">%]</span></span><span class="text">

This is parsing successfully. I'm not sure why the tag isn't detecting
the unrecognised '^^^' characters.</span><span class="eof">--EOF--</span>
</div>
</div>
<div class="panel" id="tree">
<h2>Parse Tree</h1>
<div class="content">
<div class="block"><div class="binary expr element"><div class="head"><span class="info type">binary expr</span><span class="info posn">@21</span><span class="source">a + b + c + ^</span></div><div class="body"><div class="lhs "><div class="binary expr element"><div class="head"><span class="info type">binary expr</span><span class="info posn">@13</span><span class="source">a + b + c</span></div><div class="body"><div class="lhs "><div class="binary expr element"><div class="head"><span class="info type">binary expr</span><span class="info posn">@5</span><span class="source">a + b</span></div><div class="body"><div class="lhs "><div class="variable element element"><div class="head"><span class="info type">variable element</span><span class="info posn">@3</span><span class="source">a</span></div></div></div><div class="operator element">+</div><div class="rhs "><div class="variable element element"><div class="head"><span class="info type">variable element</span><span class="info posn">@11</span><span class="source">b</span></div></div></div></div></div></div><div class="operator element">+</div><div class="rhs "><div class="variable element element"><div class="head"><span class="info type">variable element</span><span class="info posn">@19</span><span class="source">c</span></div></div></div></div></div></div><div class="operator element">+</div><div class="rhs "><div class="variable element element"><div class="head"><span class="info type">variable element</span><span class="info posn">@23</span><span class="source">^</span></div></div></div></div></div>
<div class="variable element element"><div class="head"><span class="info type">variable element</span><span class="info posn">@24</span><span class="source">^</span></div></div>
<div class="variable element element"><div class="head"><span class="info type">variable element</span><span class="info posn">@25</span><span class="source">^</span></div></div>
<div class="text element"><div class="head"><span class="info type">text</span><span class="info posn">@46</span><span class="source">\n\nThis is parsing successfully. I'm not sure why the tag isn't detecting\nthe unrecognised '^^^' characters.</span></div></div></div>
</div>
</div>
<div class="panel" id="output">
<h2>Generated Output</h1>
<div class="content">
<div class="error">template.tt3.variable.undef error - "a" is undefined</div>
</div>
</div>
</div>
</div>
</body>
<script type="text/javascript">
$(document).ready(
function() {
$('#tabset').tt3_tabset();
$('#tree').tt3_tree();
}
);
</script>
</html>

15 changes: 0 additions & 15 deletions lib/Template/TT3/Class.pm
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ use Badger::Class
patterns => \&patterns,
generate => \&generate,
subclass => \&subclass,
alias => \&alias,
view => \&view,
as => \&as,
};
Expand All @@ -39,20 +38,6 @@ sub patterns {
}


sub alias {
my ($self, $params) = self_params(@_);

while (my ($key, $value) = each %$params) {
my $method = ref $value eq CODE
? $value
: $self->method($value)
|| die "Invalid method specified for '$key' alias: $value";
$self->method( $key => $method );
}
return $self;
}


sub view {
my ($self, $view) = @_;
my $method = sprintf(VIEW_METHOD, $view);
Expand Down
2 changes: 1 addition & 1 deletion lib/Template/TT3/Tag.pm
Original file line number Diff line number Diff line change
Expand Up @@ -573,7 +573,7 @@ sub remaining_text {

sub unexpected {
my ($self, $text) = @_;
my $remain = $text->lookahead(32, '...');
my $remain = $text->lookahead(32, '...'); $remain =~ s/\n.*//s;
my $where = $text->whereabouts;
my $msg = $self->message( unexpected => $remain );

Expand Down
22 changes: 22 additions & 0 deletions lib/Template/TT3/Variable/Hash.pm
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,28 @@ sub dot {
$self->dump_data($args)
) if DEBUG;

# This is NFG. It's just too damn inconvenient to have the virtual
# methods masking data items. Things like hash.text, hash.size, etc
# all resolve to virtual methods, not data. The thing is, resolving
# the VMs first is the more predictable way to do things. VMs change
# rarely, but data changes all the time. If data resolved first then
# you'll never be sure what hash.text resolves to. At least this way,
# hash.text *always* resolves to a virtual method, even if you weren't
# expecting it to. It's also what Javascript does. That doesn't mean
# we should necessarily do it, but there is at least a justification
# there for following a popular language that many of TT's target
# audience will be familiar with. On the plus side, we'll eventually
# support hash{text} (and hash['text'], like JS) which will always
# resolve data items and never virtual methods. Although it's not as
# clean as hash.text it does at least provide a work-around. Another
# partial solution would be to severely limit the default set of hash
# virtual methods. We might be able to hit the sweet spot of having
# few enough VMs to be useful without blocking common data names (like
# 'text' and 'size', to name just two). Another possibility is to have
# data resolve first and provide an explicit operator other than '.'
# to resolve vmethods. I'm not sure what the right thing to do is.


if (my $method = $self->[META]->[METHODS]->{ $name }) {
$self->debug("hash vmethod: $name") if DEBUG;
return $self->[META]->[VARS]->use_var(
Expand Down

0 comments on commit b92340d

Please sign in to comment.