Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consider grandfather-father-son inheritance. #21

Closed
xiangnanscu opened this issue Jun 16, 2016 · 11 comments
Closed

Consider grandfather-father-son inheritance. #21

xiangnanscu opened this issue Jun 16, 2016 · 11 comments
Labels

Comments

@xiangnanscu
Copy link
Contributor

Dude, I can't use google groups now. So let's talk here. the original post is here.

Actually I am using your lua-resty-template. It does support father-son inheritance. But note my example, it's actually grandfather-father-son inheritance which is not supported if I'm not wrong. for example:

base.html

<html lang='zh'>
   <head>
   <link href="css/bootstrap.min.css" rel="stylesheet">
   </head>
   <body>
   {* blocks.content *}
   <script src="js/jquery.js"></script>
   <script src="js/bootstrap.min.js"></script>
   </body>
</html>

home.html

{% layout = "base.html" %}
{-content-}This is Home!{-content-}
{* blocks.addition *}

page.html

{% layout = "home.html" %}
{-addition-}This is addtion..{-addition-}

test.lua

local res = require"resty.template".compile("home.html"){} --this will work as expected
local res = require"resty.template".compile("page.html"){} -- this will not work. additon block will be ignored.

在 2016年6月14日星期二 UTC+8上午5:17:52,Aapo Talvensaari写道:

On Monday, 13 June 2016 04:33:09 UTC+3, 项楠 wrote:

    I'm looking for something similar to the directive `{% extends "base.html" %}` in Jinja2 template engine. I've read the docs of tt2, but `block`,`process`, `include` and `wrapper` are all seems not to be for template inheritance.


I don't mean to hijack this thread, but everything you describe is supported in lua-resty-template:
 https://github.com/bungle/lua-resty-template

Syntax os a bit different though:

1.

{% extends "layout2.html" %}
 =>
{% layout = "layout2.html" %}


2.

{% block nav %}<div class="nav"></div>{% endblock %}
=>
{-nav-}<div class="nav"></div>{-nav-}


3.

{% block nav %} {% endblock %}
=>
{* blocks.nav *}

etc.
@bungle
Copy link
Owner

bungle commented Jun 16, 2016

It should work. There is no limit. As long as you have layout defined it will get used. Let me check it out.

@bungle
Copy link
Owner

bungle commented Jun 16, 2016

Yes, your base should have {* view *}, e.g.:

<html lang='zh'>
   <head>
   <link href="css/bootstrap.min.css" rel="stylesheet">
   </head>
   <body>
   {* blocks.content *}
   {* view *}
   <script src="js/jquery.js"></script>
   <script src="js/bootstrap.min.js"></script>
   </body>
</html>

view will catch everything else that is not catched by blocks.

@bungle
Copy link
Owner

bungle commented Jun 16, 2016

Alternatively you could have these (there are probable a few more ways to do it still, but I think you get the idea):

base.html:

<html lang='zh'>
   <head>
   <link href="css/bootstrap.min.css" rel="stylesheet">
   </head>
   <body>
   {* blocks.content *}
   {* blocks.addition *}
   <script src="js/jquery.js"></script>
   <script src="js/bootstrap.min.js"></script>
   </body>
</html>

home.html:

{% layout = "base.html" %}
{-content-}This is Home!{-content-}

page.html:

{% layout = "home.html" %}
{-addition-}This is addtion..{-addition-}

@bungle
Copy link
Owner

bungle commented Jun 16, 2016

So I consider that this is not an issue, but maybe issue of understarding lua-resty-template.

@bungle
Copy link
Owner

bungle commented Jun 16, 2016

Here is another way:

base.html:

<html lang='zh'>
   <head>
   <link href="css/bootstrap.min.css" rel="stylesheet">
   </head>
   <body>
   {* blocks.content *}
   <script src="js/jquery.js"></script>
   <script src="js/bootstrap.min.js"></script>
   </body>
</html>

home.html:

{% layout = "base.html" %}
{-content-}
This is Home!
{* blocks.addition *}
{-content-}

page.html:

{% layout = "home.html" %}
{-addition-}This is addtion..{-addition-}

@bungle
Copy link
Owner

bungle commented Jun 16, 2016

And here is yet another:

base.html:

<html lang='zh'>
   <head>
   <link href="css/bootstrap.min.css" rel="stylesheet">
   </head>
   <body>
   {* view *}
   <script src="js/jquery.js"></script>
   <script src="js/bootstrap.min.js"></script>
   </body>
</html>

home.html:

{% layout = "base.html" %}
This is Home!
{* view *}

page.html:

{% layout = "home.html" %}
This is addtion..

@xiangnanscu
Copy link
Contributor Author

awesome! It does works and in fact more powerful than jinja2. Btw, maybe we should add this post to your readme.md for better explaination of grandfather-father-son inheritance.

@xiangnanscu
Copy link
Contributor Author

I make a benchmark comparing lua-resty-template's grandfather-father-son inheritance rendering with nginx try_files directive and it turns out that your module is three times faster!! Consider that try_files is simply read a text file and the lua way is wrapped not only your module but my url router!!

try_files

    location = / {
        try_files /base.html =404;
    }

lua-resty-temlate

ngx.print(require"resty.template".compile("page.html"){})

Environment is win10 64. Does this surprise you?

@xiangnanscu
Copy link
Contributor Author

Test output:

C:\projects\op\http_load>http_load -p 10 -s 3 url
4711 fetches, 10 max parallel, 2.10629e+07 bytes, in 3.00003 seconds
4471 mean bytes/connection
1570.32 fetches/sec, 7.02088e+06 bytes/sec
msecs/connect: 0.284262 mean, 3.18 max, 0.198 min
msecs/first-response: 6.06989 mean, 16.073 max, 0.632 min
HTTP response codes:
  code 200 -- 4711

C:\projects\op\http_load>http_load -p 10 -s 3 url
13871 fetches, 10 max parallel, 5.81472e+07 bytes, in 3.00022 seconds
4192 mean bytes/connection
4623.33 fetches/sec, 1.9381e+07 bytes/sec
msecs/connect: 0.547321 mean, 3.62 max, 0.167 min
msecs/first-response: 1.08073 mean, 6.627 max, 0.733 min
HTTP response codes:
  code 200 -- 13871

@xiangnanscu
Copy link
Contributor Author

@bungle hey dude, have a look at this!

@bungle
Copy link
Owner

bungle commented Jul 5, 2016

@Pronan, it feels a bit strange for template+routing to be faster than serving a static file. But I'm not complaining. You have not presented your configs and code, so it is hard for me to judge anything. Have you used open_file_cache on Nginx? Using cached lua-resty-template you are not using any file io, and that can explain something. You could put the file on memory backed tmpfs also.

But yes, sounds great! 👍

Do you have any other problems with this? If you are willing to send pull-request for better documentation, that would be great! I will at some point revisit the docs, and make it better based on your comments.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants