可以在 Middleman 中截断 YAML 文件中的字符串吗?

Possible to truncate strings from a YAML file in Middleman?

我正在生成 YAML 文件,我正在迭代这些文件以在页面上创建卡片。

YAML 文件中的一个字段是长度可变的文本字段。

为了在视图中显示可预测数量的文本(我正在创建高度相同的卡片),我想将字符串截断为 x 个字符。

在博客中工作时,可以访问 .summary method,但在我的 Middleman 网站的博客部分之外尝试使用它时出现无方法错误。

是否有另一种简单的方法来实现这一点?也尝试过截断方法,但这似乎不起作用。

这里是循环以防它有用。

        <% data.jobs.each do |j| %>
        <div class="col job-card">
            <div class="job-card-heading">
                <img src="/assets/images/latest-jobs/<%= j.image %>" alt="Company Logo">
                <div class="job-heading-text">
                    <h3><%= j.company %></h3>
                    <span><%= j.location %></span>
                </div>
            </div>
            <div class="job-card-body">
                <h3><%= j.name %></h3>
                <p><%= j.description.summary(125) %></p>
            </div>
            <div class="job-card-body">
                <span class="job-type"><%= j.jobtype %></span>
                <span class="clinician-type"><%= j.cliniciantype %></span>
            </div>
        </div>
        <% end %>

如果您的 description 变量是字符串,请尝试使用:

<%= j.description.first(125) %>

Truncate 应该适用于此:

<%= truncate(j.description, :length => 125) %>