Skip to content

Support using ivars in stylesheets#38

Merged
pnorman merged 3 commits intosystemed:masterfrom
gravitystorm:ivars
Mar 23, 2026
Merged

Support using ivars in stylesheets#38
pnorman merged 3 commits intosystemed:masterfrom
gravitystorm:ivars

Conversation

@gravitystorm
Copy link
Collaborator

@gravitystorm gravitystorm commented Jan 13, 2026

This allows you to define and use instance variables in both the stylesheet context and also within layers. They operate as if the
glug files were all part of one large ruby class.

This is similar behaviour to "@" variables in LESS, so will feel familiar to anyone coming from a CartoCSS background.

The tests show how it works, but here's a copy of the example stylesheet to give you a good idea:

version 8
name 'My first stylesheet'
source :shortbread, type: 'vector', url: 'https://vector.openstreetmap.org/shortbread_v1/tilejson.json'

# set at stylesheet level
@width = 6

layer(:roads, zoom: 10..13, source: :shortbread) do
  # get at layer level
  line_width @width
  line_color 0x888888
end

layer(:water, source: :shortbread) do
  # set at layer level
  @water = 'blue'
  fill_color @water
end

layer(:water_line, source: :shortbread) do
  # get from other layer
  fill_color @water
end

The main complexity here is that although ruby allows you to get and set instance variables programmatically, there's no hook for intercepting code that's trying to do so with "@foo = bar" statements, like you would find in the stylesheet. Or in other works, there's no equivalent of method_missing but for ivars. Instead, we shuffle the ivars back and forward between the LayerDSL and StylesheetDSL, as each layer is evaluated. Slightly clunky but it works.

Feedback welcome!

@pnorman
Copy link
Collaborator

pnorman commented Feb 9, 2026

I don't feel qualified to evaluate the code behind this. @systemed, any thoughts?

This allows you to define and use instance variables in both the
stylesheet context and also within layers. They operate as if the
glug files were all part of one large ruby class.

This is similar behaviour to "@" variables in LESS, so will feel
familiar to anyone coming from a CartoCSS background.
@pnorman
Copy link
Collaborator

pnorman commented Mar 23, 2026

This doesn't seem to be breaking anything and it looks good, so I'm going to merge it.

@pnorman pnorman merged commit b416588 into systemed:master Mar 23, 2026
8 checks passed
@gravitystorm gravitystorm deleted the ivars branch March 24, 2026 10:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants