使用Liquid剥离双重换行符

时间:2013-07-12 07:13:51

标签: jekyll liquid

在Liquid中,您可以捕获变量:

{% capture header %}

<!-- My header content -->

{% endcapture %}

然后可以使用过滤器转换此变量中的任何内容:

{{ header | strip_newlines }}

现在,假设您在网页上的<head>中有一些引用/元标记:

<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1.0">

{% if page.demo %} <link href="/css/demo.css" rel="stylesheet" type="text/css" media="screen">{% endif %}
<link href="/css/hello.css" rel="stylesheet" type="text/css" media="print">

你如何只删除双重换行符?我最终想要的是一个干净的<head>,每行有一个“参考”。 demo.css文件的“if”结构将使得非演示页面的源代码看起来像这样:

<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1.0">


<link href="/css/hello.css" rel="stylesheet" type="text/css" media="print">

我不希望行之间有额外的空格 - 在某些情况下,在较大的网站上,它可能是10行以上的空白。寻找关于如何通过过滤内容来摆脱这个空白的建议。

2 个答案:

答案 0 :(得分:3)

Jekyll plugin删除空格。

  

Aucor的Jekyll插件:例如插件。修剪不需要的   newlines / whitespace和按重量属性排序页面。

您可以直接从Github repository开始。所以基本上你用{% strip %}{% endstrip %}包装你的代码。即使这不符合您的需求,您也可以轻松更改ruby脚本。

例如:

{% strip %}
     <meta charset="utf-8">
     <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
     <meta name="viewport" content="width=device-width, initial-scale=1.0">
     {% if page.demo %} <link href="/css/demo.css" rel="stylesheet" type="text/css"     media="screen">{% endif %}
     <link href="/css/hello.css" rel="stylesheet" type="text/css" media="print">
{% endstrip %}

但是,请记住Jekyll插件的性质,你不能在Github Pages服务器上运行它们。

引自documentation

GitHub Pages is powered by Jekyll, however all Pages sites are generated using the --safe option to disable custom plugins for security reasons. Unfortunately, this means your plugins won’t work if you’re deploying to GitHub Pages.

You can still use GitHub Pages to publish your site, but you'll need to convert the site locally and push the generated static files to your GitHub repository instead of the Jekyll source files.

答案 1 :(得分:0)

Liquid的最新版本有a built-in way of controlling whitespace.如果您的项目使用Liquid 4.0或更高版本,那么您将能够在大括号内使用连字符以避免创建新行。

{%- capture random -%}
Here's a cool example
{%- endcapture -%}
相关问题