From df31197eace00a33288b47e205f8ed501077e0ec Mon Sep 17 00:00:00 2001 From: Ben Lawrence Date: Wed, 8 Feb 2017 14:39:33 +0800 Subject: [PATCH] added Javascript in views as written by Andrew in https://groups.google.com/d/topic/web2py/ozQ1zh5Cot0/discussion --- sources/29-web2py-english/05.markmin | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/sources/29-web2py-english/05.markmin b/sources/29-web2py-english/05.markmin index 70cbad8f..d6b335b0 100644 --- a/sources/29-web2py-english/05.markmin +++ b/sources/29-web2py-english/05.markmin @@ -1889,8 +1889,31 @@ we get: Hello World!!! ``:code + + +``javascript``:inxx +### Javascript in views + +Helpers can be used within external code by placing it in a template and then including the template where needed. For example, if some javascript code is in a file "/views/my.js", then it can be included in a view file: +`` + +``:code + +However, this will be inefficient if there are many lines of javascript code but only few lines of dynamically generated web2py content such as helpers. An alternative is to define the dynamically generated web2py variables in one block of javascript in the template, and then load a static javascript file that simply refers to those variables (this is how "web2py_ajax.html" works -- it defines several JS variables, which are then used by "web2py.js"). So, in the view file: + +`` + +``:code + +then in "my.js", ``someVar`` and ``someURL`` can be used as normal javascript variables. +