librelist archives

« back to archive

[Tir] A bug in tir's view render engin

[Tir] A bug in tir's view render engin

From:
Tang Daogang
Date:
2011-03-01 @ 09:26
hi, zed,

I find a bug in  tir's view  rendering engin.

Give the example code first.

in the comment.html:

<div id="page_comment_list">
    <h3>Comment List:</h3>
    <ol>
    {% for i, comment in ipairs(page:getComments()) do %}
        {( 'comment/new.html' )}
    {% end %}
    </ol>
</div>

in the comment/new.html:

<li>
    <div>
        <span></span> 在   {{comment.timestamp}}  说:
    </div>
    <div>
        <a class='reply_to_this_author' style="float:right; text-decoration:
none;">回复</a>
    </div>
    <p>
        {{comment.content}}
    </p>
</li>

Tir report: comment is not declared.

I think this occur because the view function environment doesn't contain
local varialbe 'comment', which is defined in comment.html. 'comment' is not
passed into the deeper view rendering function to render comment/new.html,
so it error.

So, I hacked on the Action function ['{(']:

    ['{('] = function(code)
        return ([[
            if not _children[%s] then
                local View = require 'bamboo.view'
                _children[%s] = View(%s)
            end

            local env = getfenv()
            local kstr, kstr_val = debug.getlocal(1, 6)
            local vstr, vstr_val = debug.getlocal(1, 7)
            if kstr and vstr then
                if type(kstr_val) == 'number' or type(kstr_val) == 'string'
then
                    if type(vstr_val) == 'string' or type(vstr_val) ==
'table' then
                        env[kstr] = kstr_val
                        env[vstr] = vstr_val
                    end
                end
            end
            _result[#_result+1] = _children[%s](env)
        ]]):format(code, code, code, code)
    end,

With it, those page can be rendered correctly.

But I am worried that the magic number in above code will cause troubles.
Can you tell me how to use a safer method to achieve this?

Tang
Thx.

-- 
Nothing is impossible.

Re: [mongrel2] [Tir] A bug in tir's view render engin

From:
Zed A. Shaw
Date:
2011-03-03 @ 06:12
On Tue, Mar 01, 2011 at 05:26:37PM +0800, Tang Daogang wrote:
> hi, zed,
> 
> I find a bug in  tir's view  rendering engin.

Hmm, I usually don't put those in loops, wos that's probably why it
works.  Your solution is pretty complex, so let me see what I can do.

> But I am worried that the magic number in above code will cause troubles.
> Can you tell me how to use a safer method to achieve this?

Let me try, but can you put this bug into the tickets for me?

http://tir.mongrel2.org/tktnew


-- 
Zed A. Shaw
http://zedshaw.com/

Re: [mongrel2] [Tir] A bug in tir's view render engin

From:
Tang Daogang
Date:
2011-03-03 @ 06:36
On Thu, Mar 3, 2011 at 2:12 PM, Zed A. Shaw <zedshaw@zedshaw.com> wrote:

> On Tue, Mar 01, 2011 at 05:26:37PM +0800, Tang Daogang wrote:
> > hi, zed,
> >
> > I find a bug in  tir's view  rendering engin.
>
> Hmm, I usually don't put those in loops, wos that's probably why it
> works.  Your solution is pretty complex, so let me see what I can do.
>
> > But I am worried that the magic number in above code will cause troubles.
> > Can you tell me how to use a safer method to achieve this?
>
> Let me try, but can you put this bug into the tickets for me?
>
> http://tir.mongrel2.org/tktnew
>

OK. Put it.

>
>
> --
> Zed A. Shaw
> http://zedshaw.com/
>



-- 
Nothing is impossible.