lektor-website/content/docs/api/environment/generator/contents.lr

40 lines
996 B
Plaintext
Raw Normal View History

2015-12-19 14:52:17 +01:00
title: generator
---
type: method
---
summary: Registers a custom generator function.
---
body:
This is very experimental API and used to register a function that can
generate source objects relative to another one. This works in combination
with [urlresolver :ref](../urlresolver/) but handles the build-all part of
the equation.
The registered function is invoked for each source after it was build. As
such it's important to only return items if a virtual sub resource actually
exists for a page.
## Example
```python
from lektor.sourceobj import VirtualSourceObject
from lektor.db import Record
2016-01-09 10:24:39 +01:00
from lektor.utils import build_url
2015-12-19 14:52:17 +01:00
class Source(VirtualSourceObject):
2016-01-09 10:24:39 +01:00
@property
def path(self):
return self.record.path + '@source'
2015-12-19 14:52:17 +01:00
@property
def url_path(self):
2016-01-09 10:24:39 +01:00
return build_url([self.record.url_path, 'source.txt'])
2015-12-19 14:52:17 +01:00
@env.generator
def generate_source_file(node):
if isinstance(node, Record) and not node.is_attachment:
yield Source(node)
```