2

我有一个主题,对于一个帖子页面,如果有的话,我会展示它的image价值。

我还有一个渲染 YouTube 视频的插件,用法是这样的:{% youtube id_12345 %}.

在我的模板文件中,是否可以引用video帖子前面的值并使用我可以在我的内容中使用的相同插件来呈现它?

就像是:

{% if post.video %}
  {% youtube {{post.video}} %}    # <-- this does not work
{% elsif post.image %}
  <img src="{{ post.image }}" class="post-image" alt="{{ post.title }}">
{% endif %}

谢谢!

4

2 回答 2

1

Since variables are not compatible with {% %} calls, I ended up reproducing much of the plugin's functionality in this one line. I started with the BetterTube and modified it for my needs.

{% if post.video %}
  {% capture video_id %}{{ post.video | 
        replace: 'https://www.youtube.com/watch?v=','' | 
        replace: 'https://youtu.be/', '' }}{% endcapture %}
  <figure class='BetterTube' data-youtube-id='{{video_id}}' data-player-width='' 
  data-player-height='' id='{{video_id}}' style='padding-bottom: 56.25%'>
  <a class='BetterTubePlayer' href='http://www.youtube.com/watch?v={{video_id}}' 
  style='background: url(http://img.youtube.com/vi/{{video_id}}/hqdefault.jpg) 50% 
  50% no-repeat rgb(0, 0, 0);'></a><div class='BetterTube-playBtn'></div>&nbsp;
  </figure>
{% elsif post.image %}
  <img src="{{ post.image }}" class="post-image" alt="{{ post.title }}">
{% endif %}

I wished I could simply call the plugin, but sadly I don't think it's possible.

于 2015-11-24T13:31:27.310 回答
0

我认为您正在使用此插件。我对编写插件知之甚少,但在我看来你不能在其中使用变量。

也许试试这个?似乎它支持变量

话虽如此,我什至认为您不需要为此运行插件的麻烦。以下代码应该可以正常工作:

{% if post.video %}
  <iframe width="560" height="420" src="http://www.youtube.com/embed/{{ post.video }}?color=white&theme=light"></iframe>
{% elsif post.image %}
  <img src="{{ post.image }}" class="post-image" alt="{{ post.title }}">
{% endif %}
于 2015-11-23T22:37:17.373 回答