krait.mvc module¶
This module interfaces Krait’s MVC support with your Python backend. MVC support is implemented by having a Python script act as a route target, that creates and specifies a controller object that holds the properties used in the finished page. This page’s code is provided by a template file, that the controller requests. Templates are files with Pyml syntax embedded inside that accesses properties on the controller. The rest of the code is usually HTML, although it can be JSON, JavaScript, or anything else.
Usage¶
See the tutorial (TODO) for a more detailed explanation.
- Create a template (a view):
- Create a file with the
.htmlor.pymlextension in a hidden directory in your site root (typically.view) with your template code.
- Create a controller:
- Create a subclass of
CtrlBaseand override itsCtrlBase.get_viewmethod to return the relative path of a template. To add properties to the controller, simply set them in its__init__method.
- Reference properties on the controller in templates:
- In template files the
ctrlvariable refers to the active controller. Use Pyml syntax to access its members and use them on the page.
- Set a controller to handle a specific URL
- Create a Python script with the appropriate location and name to be reached by the URL.
Import
krait.mvc, then callset_init_ctrl, passing as an argument an object of your controller type. Krait will then call its overriddenCtrlBase.get_viewmethod and render the template.
Reference¶
-
class
krait.mvc.CtrlBase[source]¶ Bases:
objectThe base of a MVC controller. Abstract, implementations have to override the
get_viewmethod.
-
class
krait.mvc.SimpleCtrl(view, members)[source]¶ Bases:
krait.mvc.CtrlBaseA simple controller wrapper. Best not to use, controllers should set their views themselves.
Parameters: - view (str) – The view that this controller renders with.
- members (dict) – The attributes of the controller. These will be set dynamically as instance attributes.
-
view¶ str – The view that this controller renders with.
Apart from the view, other attributes exist, specified in the
membersdictionary.
-
krait.mvc.init_ctrl= None¶ CtrlBase– The controller to be used as a master controller. Do not use directly, useset_init_ctrl. Set from route targets that want to invoke a controller (and render a response using MVC).
-
krait.mvc.set_init_ctrl(ctrl)[source]¶ Invoke a controller after the route target has finished executing.
Parameters: ctrl ( CtrlBase) – The controller object to be used.
-
krait.mvc.ctrl_stack= []¶ list of
CtrlBase– The stack of controllers, used with nested controllers. Semi-deprecated. Do not use directly, usepush_ctrlandpop_ctrl.
-
krait.mvc.curr_ctrl= None¶ CtrlBase– The current controller, used in controller stacking. Semi-deprecated. Do not use directly, usepush_ctrlandpop_ctrl.