Is it possible to append content to a block from multiple included files?

38 views
Skip to first unread message

Lisandro

unread,
Dec 10, 2014, 8:38:29 PM12/10/14
to web...@googlegroups.com
I'm wondering if it's possible to append content to a block from multiple included files
This a resume of what I would like to do:

layout.html
<html>
    {{include}}
   
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script>
        {{block jquery_code}}
       
{{end}}
   
</script>
</html>

index.html
{{extend 'layout.html'}}

<p>This is a paragraph</p>

{{include 'file_1.html'}}
{{include 'file_2.html'}}

And then the two included files looking pretty much the same for this example:
<p>This is file_1.html</p>

{{blockjquery_code}}
  //Some jquery code here
{{end}}

<p>This is file_2.html</p>

{{blockjquery_code}}
  //Some jquery code here
{{end}}

This way it doesn't work. I also tried adding  {{block jquery_code}} {{end}} to the index.html file. And I also tried using {{super}}, but nothing.

I'm trying to use it this way so the jquery.min.js file is included in the bottom of the html (accordingly to what it is the preffered way). Then, I would be able to add some jquery code in every extended view, like the file_1.html and file_2.html examples above. 

I'm trying to figure out how to do it. Any suggestions about achieving the same with a different approach? Thanks in advance!

Massimo Di Pierro

unread,
Dec 11, 2014, 12:59:41 AM12/11/14
to web...@googlegroups.com
If you include both file_1 and file_2 you should have two blocks (one each) with different names within the same script. I would also suggest that you include the <script></script> part in the block. For example

{{block script1}}{{end}}
{{block script2}}{{end}}

then file1 would do

{{block script1}}
<script>
... do something with jquery ...
</script>
{{end}}

Leonel Câmara

unread,
Dec 11, 2014, 1:50:18 PM12/11/14
to web...@googlegroups.com
I find that usually {{super}} is enough for my needs.

You can make something like this

{{extend 'file_1.html'}}
<p>This is file_2.html</p>

{{block jquery_code}}
  {{super}}
  /
/Some jquery code here
{{end}}

Then you just include file_2.html and it will have both its jquery_code and file_1.html jquery_code.

Lisandro Rostagno

unread,
Dec 11, 2014, 3:47:35 PM12/11/14
to web...@googlegroups.com
Thank you very much for your answers.
For what I need, I will have to go with Massimo's approach. That's
because I need to include several files (file1.html, file2.html,
file3.html, etc...) from index.html. The "ugly" thing is that I will
end up with several {{block ..}} in the layout.html file, but that's
not actually a problem to me so its good. Thanks again!
> --
> Resources:
> - http://web2py.com
> - http://web2py.com/book (Documentation)
> - http://github.com/web2py/web2py (Source code)
> - https://code.google.com/p/web2py/issues/list (Report Issues)
> ---
> You received this message because you are subscribed to a topic in the
> Google Groups "web2py-users" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/web2py/vXnx2gp5bpE/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to
> web2py+un...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages