如果您可以使用流,您当然也可以为您的媒体定义流块。这也允许通过管理员中显示的选项进行更多控制。例如,一个非常简单(无功能)的 youtube 流块可能如下所示:
flowblocks/youtube.ini:
[block]
name = Youtube
button_label = Youtube Video
[fields.ytid]
label = Video ID
type = string
width = 1/2
模板/块/youtube.html:
<iframe width="560" height="315" src="https://www.youtube.com/embed/{{this.ytid}}" frameborder="0" allowfullscreen></iframe>
这定义了一个名为“Youtube Video”的新块类型,内部名称为“youtube”。用户必须输入 youtube 视频 ID(视频网址中 ?v= 之后的字母/数字),然后通过 this.ytid 参考在模板中使用。
使用这种技术,还可以添加更多选项。下面是一个几乎功能齐全的示例。
flowblocks/youtube.ini:
[block]
name = Youtube
button_label = Youtube Video
[fields.ytid]
label = Video ID
type = string
width = 1/2
[fields.size]
label = Video size
type = select
choices = 560x315, 640x360, 853x480, 1280x720
choice_labels = 560 x 315, 640 x 360, 853 x 480, 1280 x 720
default = 560x315
width = 1/2
[fields.rel]
label = Show suggested videos when the video finishes
type = boolean
default = true
width = 1/4
[fields.controls]
label = Show player controls
type = boolean
default = true
width = 1/4
[fields.showinfo]
label = Show video title and player actions
type = boolean
default = true
width = 1/4
[fields.nocookie]
label = Enable privacy-enhanced mode
type = boolean
width = 1/4
模板/块/youtube.html:
<iframe width="{{ this.size.split("x")[0] }}" height="{{ this.size.split("x")[1] }}" src="https://www.youtube{{ "-nocookie" if this.nocookie }}.com/embed/{{this.ytid}}?{{ "rel=0&" if not this.rel}}{{ "controls=0&" if not this.controls }}{{ "showinfo=0" if not this.showinfo }}" frameborder="0" allowfullscreen></iframe>
在管理员中编辑时,这将如下所示:
也许可以使用一些智能正则表达式来允许用户粘贴完整的 youtube 网址,这样他们就不需要手动提取视频 ID。但我还没有尝试过。