Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7593345
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T21:08:45+00:00 2026-05-30T21:08:45+00:00

Is it possible to nest custom Liquid tags written in ruby if one class

  • 0

Is it possible to nest custom Liquid tags written in ruby if one class has multiple optional tokens passed in as parameters? This question is rather hard for me to describe without providing the relevant example. Please excuse me if this question appears to be too specific a use case.

Given the following ruby code, sourced from Octopress (a jekyll fork), which creates a custom Liquid tag to parse tags.

# Title: Simple Image tag for Jekyll
# Authors: Brandon Mathis http://brandonmathis.com
#          Felix Schäfer, Frederic Hemberger
# Description: Easily output images with optional class names, width, height, title and alt attributes
#
# Syntax {% img [class name(s)] [http[s]:/]/path/to/image [width [height]] [title text | "title text" ["alt text"]] %}
#
# Examples:
# {% img /images/ninja.png Ninja Attack! %}
# {% img left half http://site.com/images/ninja.png Ninja Attack! %}
# {% img left half http://site.com/images/ninja.png 150 150 "Ninja Attack!" "Ninja in attack posture" %}
#
# Output:
# <img src="/images/ninja.png">
# <img class="left half" src="http://site.com/images/ninja.png" title="Ninja Attack!" alt="Ninja Attack!">
# <img class="left half" src="http://site.com/images/ninja.png" width="150" height="150" title="Ninja Attack!" alt="Ninja in attack posture">
#

module Jekyll

class ImageTag < Liquid::Tag
  @img = nil

  def initialize(tag_name, markup, tokens)
    attributes = ['class', 'src', 'width', 'height', 'title']

    if markup =~ /(?<class>\S.*\s+)?(?<src>(?:https?:\/\/|\/|\S+\/)\S+)(?:\s+(?<width>\d+))?(?:\s+(?<height>\d+))?(?<title>\s+.+)?/i
      @img = attributes.reduce({}) { |img, attr| img[attr] = $~[attr].strip if $~[attr]; img }
      if /(?:"|')(?<title>[^"']+)?(?:"|')\s+(?:"|')(?<alt>[^"']+)?(?:"|')/ =~ @img['title']
        @img['title']  = title
        @img['alt']    = alt
      else
        @img['alt']    = @img['title'].gsub!(/"/, '&#34;') if @img['title']
      end
      @img['class'].gsub!(/"/, '') if @img['class']
    end
    super
  end

  def render(context)
    if @img
      "<img #{@img.collect {|k,v| "#{k}=\"#{v}\"" if v}.join(" ")}>"
    else
      "Error processing input, expected syntax: {% img [class name(s)] [http[s]:/]/path/to/image [width [height]] [title text | \"title text\" [\"alt text\"]] %}"
    end
  end
end
end
Liquid::Template.register_tag('img', Jekyll::ImageTag)

What is the best way to create another custom tag for exhibiting the same functionality for the [<img>] element, but nested within a [<figure>] element and perhaps displaying the image alt description or an additional token as a [<figcaption>] element which could potentially include its own link? Or possibly even a series of class names for the element that say whether or not it should be centered or not.

In other words, I might want the output to be something like:

<figure class=center>
  <img src="/contra.jpg" alt="One of the greatest nintendo games of all time">
  <figcaption>Up Up Down Down Left Right Left Right B A B A <a href="http://www.youtube.com/contramoves/">Watch on Youtube</a></figcaption>
</figure>

Am I wrong to assume that it is possible to nest custom Liquid tags? I’m sure I could rewrite the existing code a second time and modify it slightly to handle the additional attribute for [<figcaption>], but this seems rather redundant and against DRY principles. And as it currently stands, I’m rather confused as to how I might account for a possible, additional token given that the existing class takes optional tokens itself.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T21:08:47+00:00Added an answer on May 30, 2026 at 9:08 pm

    What I needed to do was to create a Liquid Block, not a Liquid Tag. This solution allows one to nest other Liquid tags and even other Liquid blocks in theory, within the figure which is exactly what one would expect for a [<figure>] tag.

    Since Markdown does not currently support HTML5, this Liquid based solution is a nice compromise.

     # Example:
     #
     # {% fig This is my caption! http://site.com/link.html Link Caption %}
     #   {% img center http://site.com/images/mylinks.png A collection of my favorite links %}
     # {% endfig %}
     #
     # Output:
     #
     # <figure class='center'>
     #    <img class="center" src="http://site.com/images/mylinks.png" title="A collection of my favorite links" >
     #    <figcaption>This is my caption!<a href='http://site.com/link.html'>Link Caption </a></figcaption>
     #</figure>
     #
     #
    
     module Jekyll
    
       class FigureTag < Liquid::Block
         include TemplateWrapper
         CaptionUrl = /(\S[\S\s]*)\s+(https?:\/\/\S+)\s+(.+)/i
         Caption = /(\S[\S\s]*)/
         def initialize(tag_name, markup, tokens)
           @title = nil
           @caption = nil
           if markup =~ CaptionUrl
             @caption = "\n\t\t<figcaption>#{$1}<a href='#{$2}'>#{$3}</a></figcaption>\n\t"
           elsif markup =~ Caption
             @caption = "\n\t\t<figcaption>#{$1}</figcaption>\n\t"
           end
           super
         end
    
         def render(context)
           output = super
           fig = super.join
           source = "\t<figure class='center'>\n\t\t"
           markdown = RDiscount.new(fig.lstrip).to_html[/<p>(.+)<\/p>/i]
           source += $1
           source += @caption if @caption
           source += "</figure>"
           source = safe_wrap(source)
           source
         end
       end
     end
    
     Liquid::Template.register_tag('fig', Jekyll::FigureTag)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using a C# 3.5 Winforms - Is it possible to nest one listview inside
Is it possible to nest a singleton class inside a non-singleton class in C#,
Is it possible to nest HTML forms like this <form name="mainForm"> <form name="subForm"> </form>
Is it possible to nest simple programs within a Google Sheets, similar to how
I'd like to know if it is possible in Java to nest Enums. Here
Possible Duplicate: NAnt or MSBuild, which one to choose and when? What is the
Possible Duplicate: What Ruby IDE do you prefer? I've generally been doing stuff on
Is it possible and valid to nest a <div> tag inside an <li> tag
Possible duplicate: Populating HtmlTable Control with HtmlTable object in ASP/C# I want to nest
I was wondering if it is possible to have double nest linq statemate. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.