2

We have a model defined with a StreamField that allows a DocumentChooserBlock as follows:

class CustomPage(Page):
    body = StreamField(
        [("document", DocumentChooserBlock()),]
    ...

Our page template renders the CustomPage.body field as follows:

{% block content %}
    ...
    {% include_block page.body %}
    ...
{% endblock %}

By default, the PDF document uploaded via the StreamField renders as a link.

How can I override the rendered output for the document link in the CustomPage.body StreamField, so that I can render the PDF in line with a JavaScript PDF widget?

If this is a common need, I would be interested in publishing a Wagtail extension for other projects to use.

Brylie Christopher Oxley
  • 1,684
  • 1
  • 17
  • 34

1 Answers1

2

Pass a template argument to DocumentChooserBlock:

class CustomPage(Page):
    body = StreamField(
        [("document", DocumentChooserBlock(template='myapp/blocks/document.html')),]

Alternatively, if you're going to be using this in multiple places, subclass DocumentChooserBlock and set template in its inner Meta class, then use that new class in place of DocumentChooserBlock:

class DocumentEmbedBlock(DocumentChooserBlock):
    class Meta:
        template = 'myapp/blocks/document.html'


class CustomPage(Page):
    body = StreamField(
        [("document", DocumentEmbedBlock()),]

Within the template, value will refer to the Document object, so you can refer to the document's properties via {{ value.url }} or {{ value.title }}:

<h2>{{ value.title }}</h2>
<iframe src="{{ value.url }}"></iframe>
gasman
  • 23,691
  • 1
  • 38
  • 56