Service Resources == Description == This tutorial shows how to access resources (css, hss, script, images, etc.) associated with a service. This tutorial used server side functions described in the ,( "Service" "service.wiki") API page. == Accessing Resources == Usually a weblet has to access //resources// associated with the implementation. These resources might either be a cascading style sheet (see the ,( "HSS tutorial" "02-hss.wiki")), a Hop library, an image, a text, etc. The weblet needs to //refer// to these files in order to build a complete HTML tree. The references might occur from the server, in which case //server file names//, are needed or from the client, in which case //URI// are needed. For the sake of the example, let us assume a simple weblet ++example.hop++ which provide the following service: (define-service (example) ( ( :src "img.png"))) Let us assume that the image ++img.png++ is installed in the directory ++/etc/icon++ where ++++ is the installation directory of ++example.hop++. Because the image is not //inlined// (see ,( "Img Markup" "img.wiki")) the value of the ++++ ++:src++ attribute is a remote URI. There are four ways, in the implementation of ++example++, for expression this URI. - Using an absolute path: (define-service (example) ( ( :src "/etc/icon/img.png"))) This is the weakest solution because it requires the installation path fixed. This solution should be **avoided** as much as possible. - Relying on ++the-loading-file++: (define-parameter example-dir (dirname (the-loading-file))) (define-service (example) ( ( :src (string-append (example-dir) "/etc/icon/img.png")))) This solution gets rid of the absolute path because the value ++example-dir++ is automatically computed when the implementation of ++example++ is loaded. The path is thus ensured to be correct. - Using ++service-resource++: (define-service (example) ( ( :src (service-resource example "etc/icons/img.png")))) This solution prevents for explicitly storing the //loading directory// into a variable. The ,( "services" "service.wiki") can be used to compute relative paths. - Using ++service-base-url++: (define-service (example) ( ( :base (service-base-url example (current-request))) ( :src "etc/icons/img.png"))) This last solution uses the HTML ++:base++ argument that globally changes the URL base of the whole document. With this solution all URI are expressed relatively to this new base.