Shopify / Shopify/liquid

Custom tags: what’s the preferred method of providing arguments containing quotes

Open
#507 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Question
Dominant language
Ruby
Stars
11.9k
Forks
1.5k
Avg merge
17h 55m
Merged PRs (30d)
3

Description

TL;DR: How should I provide/parse arguments that may contain a single or double quote(s) in a custom tag?


Not sure if this is the correct place to ask a question, but couldn't find documentation elsewhere…

I’ve created a custom liquid tag block, to render a figure with optional caption. Here’s an example:

{% figure class:'class1 class2', caption:''Cloud Gate' by Anish Kapoor' %}
![](/assets/images/cloudgate.jpg)
{% endfigure %}

Firstly, is this the correct method of supplying arguments to a custom block? I see that pipe delimited arguments are for filters, the = symbol for assignments.

I'm using Liquid::TagAttributes, to parse each argument. I’m then having to remove (with gsub) the first and last ' or " else these get output in the HTML. But the generated content is malformed if strings contain quotes. I’ve documented some examples here:

https://gist.github.com/paulrobertlloyd/dff85e8af0f039255760

This is the code for my custom tag:

module Jekyll
  class FigureTag < Liquid::Block
    def initialize(tag_name, markup, tokens)
      super
      @attributes = {}

      markup.scan(Liquid::TagAttributes) do |key, value|
        @attributes[key] = value.gsub(/^'|"/, '').gsub(/'|"$/, '')
      end
    end

    def render(context)
      site = context.registers[:site]
      converter = site.getConverterImpl(::Jekyll::Converters::Markdown)

      figure_classes = @attributes['class'].to_s
      figure_main = converter.convert(super(context))
      figure_caption = converter.convert(@attributes['caption'].to_s)

      # Render <figure>
      if @attributes['class']
        source = "<figure class=\"figure #{figure_classes}\">"
      else
        source = "<figure class=\"figure\">"
      end

      source += "<div class=\"figure__main\">#{figure_main}</div>"

      if @attributes['caption']
        source += "<figcaption class=\"figure__caption\">#{figure_caption}</figcaption>"
        source += "</figure>"
      else
        source += "</figure>"
      end
    end
  end
end

Liquid::Template.register_tag('figure', Jekyll::FigureTag)

Hope this makes sense!

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the custom FigureTag example and the Liquid::TagAttributes parser shown in the issue; no repository file or test is identified. Reproduce the quoted-argument cases from the linked gist and determine whether the intended outcome is parser documentation or a documented usage pattern, then define completion as an agreed answer with supporting documentation.

Written by the indexing model from the issue text.

Assessment

Tech stack
ruby
Domain
documentation
Issue type
Documentation
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
20/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.