Linking images and files in hash Last updated: 09. Mar 2023

How to link files and images from #JS code.

Link from current document

Image in document:

If image is stored in a field called Image1:

<img src="#linkimage(Image1,1024,768,2)#" alt="An image" />

File:

If the file is stored in a field called File1:

<a href="#request.Url#/#File1#/#File1-Filename#">Download file</a>

When looping:

#for (let item of Items) {#
<div>
    Just address the field name for each item:
    <img src="#linkimage(item.Image,1024,768,2)#" alt="An image" />
    
    Same for file:
    <a href="#request.Url#/#item.File1#/#item.File1_Filename#">Download file</a>
</div>
#}#

Link image from another document

<div>
    <!-- Get data from other page (file or document type) -->
    #let p = docly.getFile("image.jpg")#
    
    <!-- Link image from other page and scale -->
    <img src="#p.Url#/#p.File#/1024x768x1/virtual-name.jpg" alt="An image" />
</div>

Link images from folders

This code will link images from folders that contain images such as the "Image" field from "Published folder".

#let folders = docly.getFolders("/")#
#for (let item of folders) {#
<div>
    <h3>Folder: #item.Name#</h3>
    
     <!-- Links the image from the folder URL and the attachment name and scaling as suffix -->
    <img src="#item.Url#/#item.Image#/100x100x2/image.jpg" alt="" />
</div>
#}#

Link background image from another document

Note that the ~ is inserted in front of the URL:

#let p = docly.getFile("image.jpg")#

<div style="background-image: url('~#p.Url#/#p.File#/1024x768x1/virtual-name.jpg'); width:1024px;height:768px">
</div>

Virtual name

The virtual name can be anything you desire. Use this to give the file a SEO friendly name.

Scaling parameters

Images will be rescaled according to parameter 2, 3 and 4.

Parameters
Image field (required)
Width in pixels (optional)
Height in pixels (optional)
Scale mode (optional)

Scale modes are:

Mode Description
0 Scale to fit
1 Scale to fill
2 No ratio rescale
3 Scale with white margin
4 Scale with black margin
5 No scaling

Read more about the different scale modes here:
https://developers.docly.net/JavaScript/Filesystem-functions/Image-scaling