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

41 lines
1.0 KiB
Plaintext
Raw Normal View History

2015-12-19 14:52:17 +01:00
title: urlresolver
---
type: method
---
summary: Registers a custom URL resolver function.
---
body:
This is very experimental API and used to register a custom URL resolver
function with the environment. It's invoked whenever a node has matched but
more data is left to parse. This can be used to implement virtual items.
This works in combination with [generator :ref](../generator/) but handles the
URL matching part of the equation.
The registered function is invoked with a source object and a list of
URL path segments.
## Example
```python
from lektor.sourceobj import VirtualSourceObject
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.urlresolver
def match_source_file(node, url_path):
if url_path == ['source.txt'] \
and isinstance(node, Record) \
and not node.is_attachment:
return Source(node)
```