Forum

Thread tagged as: Question, Suggestions, Field-Types

Is it possible to include a default alt text based on the uploaded file name?

I have it at the moment that alt text is entered for an uploaded image using the following code:

alt="<perch:content type="text" id="alt" label="Description" required="true" help="Description of this image" title="true"/>"

However, I try to name my image files sensibly and most of the time the alt text will be the same as the file name.

e.g.

Filename Electronics portfolio item

alt text Electronics portfolio item

Most of the time I find myself just copying the file name and pasting it into the alt text field that I created in the Perch admin.

Bearing this in mind is there a way to use the file name for the alt text by default (which could then be overriden if necessary)?

The closest solution I've come up with is to output the filename in the alt tag, doing some replace work like this:

alt="<perch:content id="image" replace="/perch/resources/|,.jpg|,.png|,.gif|" />"

However, this is less than perfect because using the replace function I cannot replace the "-" in the file name with a space, and it it's still all lowercase.

Any ideas or solutions would be appreciated.

Jay George

Jay George 2 points

  • 5 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

I think what you want is the value we store as resourceTitle, but I'm not sure the field type has a way to output that from a tag.

Thanks for your reply. Ok, I'll give up for now!

It would certainly be handy for if this was possible.

FYI I tried:

alt="<perch:content id="image" output="resourceTitle"/>"

and it simply outputs the file path e.g.

alt="/perch/resources/something.jpg"