# from pyramid.view import view_config

from ..models import DBSession


class ViewBase(object):
    """
    Base class for all views which provide helpers to manipulate the scope
    and other stuff.
    """

    # the scope is the dict sent to the template engine
    _scope = {}

    def __init__(self, request=None):
        self.request = request
        self.session = DBSession()

    def __call__(self):
        """
        Return the scope to render the template.
        """
        self.render()
        return self._scope

    def scope(self, *arg):
        """
        Getter/setter to the scope.
        """
        # return a value from the scope
        if len(arg) == 1 and isinstance(arg[0], str):
            return self._scope.get(arg[0], '')
        # set only one value to the scope
        elif len(arg) == 2:
            self._scope[arg[0]] = arg[1]
        # set value(s) to the scope
        elif len(arg) == 1 and isinstance(arg[0], dict):
            for key, value in arg[0].iteritems():
                self._scope[key] = value

    def render(self):
        """
        Set variables to the scope before rendering.
        To be extended by the view.
        """
        pass


class View(ViewBase):
    """
    Define the default variables in the scope.
    """
    _scope = {
        'layout': 'layouts/default.html',
        'page_title': 'Sample title'
    }


class Node(ViewBase):
    """
    Define a node which is a ViewBase object which is able to render itself.
    Since no view declaration is done for these objects, every child nodes must
    define a template.
    """
    template = None

    def __call__(self):
        self.render()

        if not self.template:
            return ''

        from pyramid.renderers import render
        return render(self.template, self._scope, self.request)

@@ -131,73 +131,17 @@
 ews 
-which provide helpers to manipulate the scope%0A    and other stuff
+and nodes
 .%0A  
@@ -254,24 +254,128 @@
 uest=None):%0A
+        %22%22%22%0A        Save a reference to the Pyramid request object and :py:class:DBSession.%0A        %22%22%22%0A
         self
@@ -477,47 +477,67 @@
     
-Return the scope to render the template
+Make the class as a callable function witch can be rendered
 .%0A  
@@ -1207,16 +1207,17 @@
 dering.%0A
+%0A
         
@@ -1270,16 +1270,178 @@
   pass%0A%0A
+    def node(self, node):%0A        %22%22%22%0A        Instanciate the :py:class:%60Node%60 %60node%60 and return its html code.%0A        %22%22%22%0A        return node(self.request)()%0A%0A%0A
 %0Aclass V
@@ -1552,16 +1552,26 @@
 yout': '
+templates/
 layouts/
@@ -1668,27 +1668,14 @@
     
-Define a node which
+A node
  is 
@@ -1685,22 +1685,21 @@
 iewBase 
-object
+child
  which i
@@ -1704,132 +1704,427 @@
  is 
-able to ren
+not declared as a view. Therefore, a%0A    no
 de
-r
  i
-tself.%0A    Since no view declaration is done for these objects, every child nodes must%0A    define a template
+s not mapped to an URL.%0A%0A    A node represents a part of the HTML page and implements its own logic since%0A    the request and the DBSession objects are available.%0A%0A    The page is build from several nodes, each ones should be independant and%0A    can be reused in different views.%0A%0A    Since no view and route declaration is done for a node, a template must be%0A    declared
 .%0A  

Update docs.