# Command in space-lua to set page attributes

**URL:** <https://community.silverbullet.md/t/command-in-space-lua-to-set-page-attributes/1696>\
**Category:** Lua\
**Created:** [January 25, 2025, 11:08pm UTC](https://community.silverbullet.md/t/command-in-space-lua-to-set-page-attributes/1696 "2025-01-25T23:08:09Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![yggi](https://community.silverbullet.md/user_avatar/community.silverbullet.md/yggi/32/956_2.png) [@yggi](https://community.silverbullet.md/u/yggi)\
**Post date:** [January 25, 2025, 11:08pm UTC](https://community.silverbullet.md/t/command-in-space-lua-to-set-page-attributes/1696/1 "2025-01-25T23:08:09Z")

</div>

Hi, I discovered silverbullet a few weeks ago and it was love on first sight ❤

Currently I am exploring space-lua, and I came up with a command to modify page attributes:

````lua
```space-lua
command.define {
  name = "Page: Set Attribute",
  run = function(a)
    local data = a[1]
    local mypath = data.page or _CTX.currentPage.name
    local myattr = data.attribute or "foo"
    local myvalue = data.value or 0
    local mychange = data.change or 0
    local mypage = space.read_page(mypath)
    local oldline = "[%[]%s*" .. myattr .. "%s*:%s*%w+%s*]"
    local oldval = nil
    local newline = "[" .. myattr .. ":" .. myvalue .. "]"
    local newpage = ""
    local matches = string.match(mypage,oldline).values
    if matches[1] then
      local oldval = tonumber(string.match(matches[1],"%d+").values[1])
      if oldval and mychange != 0 then
        newline = "["..myattr .. ":" .. oldval+mychange .. "]"
      end 
      newpage = string.gsub(mypage,oldline,newline,1)
    else
      newpage = mypage.."\n"..newline
    end
    space.write_page(mypath,newpage)
    codeWidget.refreshAll()
    if mypath == _CTX.currentPage.name then
      editor.set_text(newpage)
    end
  end
}

````

This can then be used as a command button to set or change attributes of the current page:

```auto
{[Page: Set Attribute|Reset Stars]({"attribute": "stars","value": 0})}
{[Page: Set Attribute|Add Star]({"attribute": "stars","change":1})}

```

or any other page:

```auto
{[Page: Set Attribute|Reset Stars on test]({"page": "test","attribute": "stars","value": 0})}
{[Page: Set Attribute|Add Star on test]({"page": "test","attribute": "stars","change": 1})}

```

It really shines when parametrized in a template:

````auto
```template
{{#each {page where stars and stars>=0} }}
* [[{{name}}]]: ⭐{{stars}} {[Page: Set Attribute|Add Star]({"page": "{{name}}","attribute": "stars","change": 1})}
{{/each}}

````

this renders to:

 ![2025-01-25T23:51:35,343051356+01:00](https://community.silverbullet.md/uploads/default/original/1X/719b6b78fa3e4082b1bd692d30f25109768cf19a.png)

Maybe this is useful for someone. Have fun!
