From cfc7015355848629cdc7f7d9fde795c929eec093 Mon Sep 17 00:00:00 2001 From: Xavier Morel Date: Fri, 7 Feb 2014 11:11:57 +0100 Subject: [PATCH] [FIX] templating section, some wording --- doc/howto/howto_website.rst | 57 +++++++++++++++------- doc/howto/howto_website/templates-basic | 22 ++++++--- doc/howto/howto_website/website-dependency | 13 +++-- doc/howto/howto_website/website-layoutify | 8 +-- 4 files changed, 68 insertions(+), 32 deletions(-) diff --git a/doc/howto/howto_website.rst b/doc/howto/howto_website.rst index 452d83d1aca..65a6ed18327 100644 --- a/doc/howto/howto_website.rst +++ b/doc/howto/howto_website.rst @@ -31,13 +31,29 @@ create a module: .. patch:: :hidden: -This builds a basic module for you, ignore anything in the ``models`` and -``security`` directories for now. +This builds a basic module for you: + +.. code-block:: text + + academy + ├── __init__.py + ├── __openerp__.py + ├── controllers + │   ├── __init__.py + │   └── my_controller.py + ├── models + │   ├── __init__.py + │   └── my_model.py + └── security + └── ir.model.access.csv + +Ignore anything in the ``models`` and ``security`` directories for now. .. todo:: * instructions for start & install * db handling + - if existing db, automatically selected - if no existing db, nodb -> login -> login of first db - dbfilter @@ -46,6 +62,8 @@ Now start your OpenERP server and install your module in it, open a web browser and navigate to http://localhost:8069. A page should appear with just the words "Hello, world!" on it. +.. todo:: screenshot? + Let's prettify things a bit: instead of returning just a bit of text, we can return a page, and use a tool like bootstrap_ to get a nicer rendering than the default. @@ -57,21 +75,28 @@ returned by the ``index`` method to get a more page-ish output: .. note:: - this example requires internet access at all time, as we're accessing a - :abbr:`CDN (Content Delivery Network, large distributed networks hosting - static files and trying to provide high-performance and high-availability - of these files)`-hosted file. + this example requires internet access as we're accessing a :abbr:`CDN + (Content Delivery Network, large distributed networks hosting static files + and trying to provide high-performance and high-availability of these + files)`-hosted file. + +.. todo:: screenshot Data input: URL and query ========================= Being able to build a static page in code is nice, but makes for limited -usefulness (you could do that with static files in the first place). +usefulness (you could do that with static files). -You can also create controllers which use data provided in the access URL, -for instance so you have a single controller generating multiple pages. Any -query parameter (``?name=value``) is passed as a parameter to the controller -function, and is a string. +You can also create dynamic pages which use data provided in the URL, +for instance so a single controller generates multiple pages. Any +query parameter (``?name=value``) is passed as a string parameter to the +controller method. + +For instance, the index page can display a list of teaching assistants linking +to a page for each assistant through their index in a global array. Each +assistant's page will simply print their name by applying the index to the +array: .. patch:: @@ -89,16 +114,14 @@ This can be done by adding `converter patterns`_ to the URL in These patterns can perform conversions directly (in this case the conversion from a string URL section to a python integer) and will perform a some validation (if the ``id`` is not a valid integer, the converter will return a -``404 Not Found`` instead of generating a server error when the conversion -fails). +``404 Not Found`` instead of a 500 server error when the conversion fails). Templating: better experience in editing ======================================== -So far we've created HTML output by munging together Python strings using -string concatenation and formatting. It works, but is not exactly fun to edit -(and somewhat unsafe to boot) as even advanced text editors have a hard time -understanding they're dealing with HTML embedded in Python code. +So far we've output HTML by munging strings. It works, but is not exactly fun +to edit (and somewhat unsafe to boot) as even advanced text editors have a +hard time understanding they're dealing with HTML embedded in Python code. The usual solution is to use templates_, documents with placeholders which can be "rendered" to produce final pages (or others). OpenERP lets you use any diff --git a/doc/howto/howto_website/templates-basic b/doc/howto/howto_website/templates-basic index b0e5d85c487..e3475046b2d 100644 --- a/doc/howto/howto_website/templates-basic +++ b/doc/howto/howto_website/templates-basic @@ -1,5 +1,5 @@ # HG changeset patch -# Parent 170cc87a26983d34df5cd3bb27d3259722c586e5 +# Parent 0792d59a4a456e1ce70d8aa4cb1784632883d714 diff --git a/__openerp__.py b/__openerp__.py --- a/__openerp__.py @@ -19,7 +19,13 @@ diff --git a/__openerp__.py b/__openerp__.py diff --git a/controllers/my_controller.py b/controllers/my_controller.py --- a/controllers/my_controller.py +++ b/controllers/my_controller.py -@@ -19,36 +19,10 @@ class my_controller(main.Home): +@@ -14,41 +14,18 @@ teaching_assistants = [ + class my_controller(main.Home): + @http.route('/', auth='none') + def index(self): ++ cr, uid, context = http.request.cr, http.request.uid, http.request.context + tas = [ + '
  • %s
  • ' % (i, ta['name']) for i, ta in enumerate(teaching_assistants) ] @@ -42,9 +48,9 @@ diff --git a/controllers/my_controller.py b/controllers/my_controller.py -""" % { - 'tas': '\n'.join(tas) - } -+ return "" % { ++ return http.request.registry['ir.ui.view'].render(cr, uid, 'academy.index', { + 'tas': '\n'.join(tas) -+ } ++ }, context=context) @http.route('/tas//', auth='none') def ta(self, id): @@ -59,7 +65,9 @@ diff --git a/controllers/my_controller.py b/controllers/my_controller.py - - -""" % teaching_assistants[id] -+ return "" % teaching_assistants[id] ++ cr, uid, context = http.request.cr, http.request.uid, http.request.context ++ return http.request.registry['ir.ui.view'].render( ++ cr, uid, "academy.tas", teaching_assistants[id], context=context) diff --git a/views/templates.xml b/views/templates.xml new file mode 100644 --- /dev/null @@ -67,7 +75,7 @@ new file mode 100644 @@ -0,0 +1,39 @@ + + -+