Bundle is basically collection of views in the class.
You create new class, inherit it from the Bundle class and expose some of the methods as views. Then, you instantiate your class and all your exposed methods will be accessible from the Web.
Let’s check simple example:
from svarga.shortcuts import as_html
from svarga.shortcuts.bundle import Bundle, expose
class MyBundle(Bundle):
@expose('/')
@as_html('entry.html')
def entry(self):
return {}
@expose('/info/<int:id>/')
@as_html('info.html')
def info(self, id):
return dict(id=id)
Code above defines absolutely valid bundle with two view functions: entry and info. Now, you can add your bundle to your url mapping, like this:
url_map = Map(
MyBundle('b1', '/bundle1/'),
MyBundle('b2', '/bundle2/'),
MyBundle('b3', '/bundle3/'),
MyBundle('b4', '/bundle4/')
)
We, basically, created 4 instances of MyBundle class which have different names (first parameter) and have different mounting points (second parameter).
If you will start development server and will navigate to the http://localhost:8000/bundle1/, you will end up in the entry view of the first MyBundle class instance.
Name is required to distinguish different instances of same bundle. To create reverse link, use bundle name and method name. Example:
url = reverse('b1.entry')
url = reverse('b1.info', id=10)
url = reverse('b2.info', id=20)
In templates, you can easily generate links by exporting bundle name to the template. Consider following example:
class MyBundle(Bundle):
@expose('/')
@as_html('entry.html')
def entry(self):
return dict(name=self.name)
And following template:
<html>
<body>
<a href="{% url name+'.entry' %}">Click me!</a>
</body>
</html>
Example above will render HTML link which points to the entry view.
Alternative way to link to local view:
class MyBundle(Bundle):
@expose('/')
@as_html('entry.html')
def entry(self):
return dict(bundle=self)
And use following template:
<html>
<body>
<a href="{{ bundle.reverse('entry') }}">Click me!</a>
</body>
</html>