https://toitlang.org/ logo
How do I add assets to my containers using Jaguar?
# help
k
There does not seem to be a straightforward way to add assets (things like html documents) as additional resources to containers using the Jaguar tooling.
It is possible to add assets to containers using the SDK tooling. It is described here: https://github.com/toitlang/toit?tab=readme-ov-file#adding-container-assets. If we can find a way to do the same with the Jaguar tooling, it should be possible to have an html document associated with a container - and perhaps serve that document from a web server in the container.
Jaguar has a way to add so-called defines for containers. Such defines are accessible from within the container, where they are included in a map available in the
jag.defines
asset.
So if you do
jag run -D mypage.greeting="hello" myapp.toit
, you can use the
"hello"
string from
myapp.toit
like this:
Copy code
import system.assets
main:
  jag-defines := assets.decode["jag.defines"]
  greeting := jag-defines["mypage.greeting"]
  print "greeting = $greeting"
Wishful thinking: If we could just add more seperate assets when installing or running apps using Jaguar, it would be simple to pass an html document like an argument. Something like
jag run --asset file:mypage=mypage.html myapp.toit
, perhaps. That could then be used from Toit code like this:
assets.decode["mypage"]
.
Jaguar supports providing the assets for a program when run or installed using the
--assets
option. To use this, you first have to produce an assets file using
toit tool assets
.
First you create the assets file like this:
Copy code
toit tool assets -e myassets create
and then you add you assets to it like this:
Copy code
toit tool assets -e myassets add mypage mypage.html
This leaves you with an assets file called
myassets
that has a single entry (
mypage
) that contains the content of the
mypage.html
file.
You can look at the contents like this:
Copy code
toit tool assets -e myassets list
You can now pass your assets to your program by using
jag run --assets myassets myapp.toit
and you can access your html like this:
Copy code
import system.assets
main:
  mypage := assets.decode["mypage"]
  print "html = $mypage"
If you've only installed Jaguar, you can find the
toit
executable in
$HOME/.cache/jaguar/sdk/bin/toit
.
7 Views