Custom Variables in Jekyll Front Matter

前端 未结 2 1888
盖世英雄少女心
盖世英雄少女心 2021-02-07 15:28

New to Jekyll and wondering if it\'s possible to include custom variables in Jekyll Front Matter. It would be useful for nested layouts, for example something like:

layo

相关标签:
2条回答
  • 2021-02-07 15:51

    At the moment, Jekyll do not support Liquid variables in the front matter, and the only way to do that would be through a plugin, such as jekyll-conrefifier.


    Alternatively, what you can do, though, is create variables that you re-use on the same file:

    {% assign new_title = page.title | append: " (Artist)" %}
    <h1>{{ new_title }}</h1>
    

    and you can also pass variables to files that get included. For example, including a file from _includes\display-post.html passing the modified title as an argument:

    {% assign new_title = page.title | append: " (Artist)" %}
    {% include display-post.html post_title=new_title %}
    

    And then getting the value of the value passed in (example content of _includes\display-post.html):

    {% assign title_received = include.post_title %}
    
    <h1>Title that as passed in: {{ title_received }}</h1>
    
    0 讨论(0)
  • 2021-02-07 15:56

    I'm not sure if there is a way to do this properly (i.e. server side), but a stop-gap measure could be to have a small snippet of Javascript that sets the correct title in the users browser. e.g.

    ---
    title: Default title blah blah
    ---
    
    [... content ...]
    
    <span id="pagetitle" style="display: none">{{ page.artist | escape }} (Artist)</span>
    
    <script type="text/javascript">
        var pagetitle = document.getElementById("pagetitle");
        if (pagetitle) {
            document.title = pagetitle.textContent;
        }
    </script>
    

    Notes:

    The substitution of page.artist is performed in HTML rather than in the Javascript because it is easier to quote any HTML special characters (via escape) rather than the Javascript special characters ' or " or \ (there isn't a built-in filter to do this).

    One could also move the pagetitle span to the top of the page so that it is near the other YAML front matter.

    Unfortunately, this is a very poor way of achieving this, but it looks like it might be the only way other than writing a plugin.

    0 讨论(0)
提交回复
热议问题