Javascript load order and templates

20 views
Skip to first unread message

David Orme

unread,
Mar 31, 2017, 10:45:34 AM3/31/17
to web2py-users
Hello,

I'm using wavesurfer in a site, so have included it at the bottom of layout.html.

<!-- The javascript -->
<script src="{{=URL('static','js/bootstrap.min.js')}}"></script>
<script src="{{=URL('static','js/web2py-bootstrap3.js')}}"></script>

But then one of my views has javascript code to setup a wavesurfer player. This is currently included at the bottom of the view - note that it includes some web2py expressions to populate variables.

<!-- The javascript =============================== -->
<script type="text/javascript">
   
var wavesurfer = WaveSurfer.create({
     container
: '#waveform',
   
});
  
wavesurfer.load("{{=URL('static', record.static_filepath)}}");
</script>

It doesn't work, but I think for the simple reason that the script in the view is included ahead of the scripts loaded in the layout (the contents of the view all gets stuck inside {{include}} in the layout, right?). If I put copy those layout javascript lines into the view ahead of my local inline script, then it works. However, I don't think I want pages loading the same javascript twice, so now I have to load those javascript files at the end of every view.

Is there a canonical way to handle this? Do blocks allow me to split insert the contents of a view into different parts of the layout? Or am I better off turning the script into a static JS file that all pages load and adding a simple order-agnostic javascript to define the java variables.

Many thanks,
David

Carlos Costa

unread,
Mar 31, 2017, 10:56:32 AM3/31/17
to web...@googlegroups.com
A proper way to do this is to put the page javascript in the page_js block
It is the declared in the default layout.html.

To use it just do this in your view

{{block page_js}}
 ...put your js here...
{{end page_js}}

This way it will be inserted in the right place into the rendered page.


--
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 the Google Groups "web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to web2py+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--


Carlos J. Costa
Cientista da Computação  | BS Computer Science
Esp. Gestão em Telecom   |
PgC Telecom Mangement
<º))><

David Orme

unread,
Mar 31, 2017, 11:04:42 AM3/31/17
to web...@googlegroups.com
Ha, I was right:

Do blocks allow me to split insert the contents of a view into different parts of the layout? 

It’s a good thing that it is a really complex mechanism, so I don’t look like a total idiot ;-)

Many thanks - fantastic to get such a quick answer.

Cheers,
David



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/k0xtY1Jz9-s/unsubscribe.
To unsubscribe from this group and all its topics, send an email to web2py+un...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages