Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

crap.globals

Global (singleton document) definition and runtime operations.

crap.globals.define(slug, config)

Define a new global. Call this in global definition files (globals/*.lua).

crap.globals.define("site_settings", {
    labels = { singular = "Site Settings" },
    fields = {
        crap.fields.text({ name = "site_name", required = true, default_value = "My Site" }),
        crap.fields.text({ name = "tagline" }),
    },
})

See Globals for the full config reference.

crap.globals.config.get(slug)

Get a global’s current definition as a Lua table. The returned table is round-trip compatible with define() — you can modify it and pass it back.

Returns nil if the global doesn’t exist.

local def = crap.globals.config.get("site_settings")
if def then
    def.fields[#def.fields + 1] = crap.fields.textarea({ name = "footer_text" })
    crap.globals.define("site_settings", def)
end

crap.globals.config.list()

Get all registered globals as a slug-keyed table. Iterate with pairs().

for slug, def in pairs(crap.globals.config.list()) do
    -- Add a "last_updated_by" field to every global
    def.fields[#def.fields + 1] = crap.fields.text({ name = "last_updated_by" })
    crap.globals.define(slug, def)
end

See Plugins for patterns using these functions.

crap.globals.get(slug, opts?)

Get a global’s current value. Returns a document table.

Only available inside hooks with transaction context.

Parameters

ParameterTypeDescription
slugstringGlobal slug
optstable (optional)Options table

Options

KeyTypeDescription
localestringLocale code (e.g., "en", "de"). Fetches locale-specific field values. If omitted, returns the default locale data.
local settings = crap.globals.get("site_settings")
print(settings.site_name)  -- "My Site"
print(settings.id)         -- always "default"

-- Fetch German locale data
local settings_de = crap.globals.get("site_settings", { locale = "de" })

crap.globals.update(slug, data, opts?)

Update a global’s value. Returns the updated document.

Only available inside hooks with transaction context.

Parameters

ParameterTypeDescription
slugstringGlobal slug
datatableFields to update
optstable (optional)Options table

Options

KeyTypeDescription
localestringLocale code (e.g., "en", "de"). Updates locale-specific field values. If omitted, updates the default locale data.
local settings = crap.globals.update("site_settings", {
    site_name = "New Site Name",
    tagline = "A new beginning",
})

-- Update German locale data
crap.globals.update("site_settings", {
    site_name = "Neuer Seitenname",
}, { locale = "de" })