Search the FirstSpirit Knowledge Base
We are currently on FS 5.2. I have a page where I want to display just the published filename of an image or PDF, without the path. e.g. "sample_image.jpg" instead of "/images/uploaded/sample_image.jpg"
Publishing out the reference $CMS_REF(pt_file, abs:1)$ gives me the full absolute path which I can use some javascript to extract just the filename but I'd rather not have the full path published out, even inside my javascript code.
Published filenames also get altered (spaces are removed, underscores converted to hyphens) so my CMS-end string manipulations haven't been helpful (using .replaceAll() in my $CMS_SET).
Hi,
Using the filename or uid attribute is not a reliable solution. The reason is that the URLs of objects (pages/media files) are calculated by a separate mechanism (so called UrlFactory) of which there are multiple implementations from which you can choose in the generation tasks.
Those implementations calculate the filenames in different ways. For example, the "Default URLs" are in deed based on the filename attribute (and the folders' uids) but the "Advanced URLs" use the display names for the URLs.
So instead of "imitating" the concrete logic of one specific UrlFactory in the template, it is recommended to just "call" it so the filenames always match regardless of the currently used/configured UrlFactory.
In a template, you can output the filename by using
$CMS_VALUE(ref(pt_file).url.split("/").last)$
This way, the ref(...).url statement will simply "call" the same mechanism that is responsible for calculating the URL, whatever it is at the moment.
Michael
$CMS_VALUE(pt_file.media.filename)$ gives you the filename
$CMS_VALUE(pt_file.media.uid)$ gives you the reference name
Sven,
Thank you, that worked.
Could you flag my answer as the correct answer please, so that everybody sees the question and the correct answer directly?
Sorry, got you now. Had the wrong post tagged by accident.
Hi,
Using the filename or uid attribute is not a reliable solution. The reason is that the URLs of objects (pages/media files) are calculated by a separate mechanism (so called UrlFactory) of which there are multiple implementations from which you can choose in the generation tasks.
Those implementations calculate the filenames in different ways. For example, the "Default URLs" are in deed based on the filename attribute (and the folders' uids) but the "Advanced URLs" use the display names for the URLs.
So instead of "imitating" the concrete logic of one specific UrlFactory in the template, it is recommended to just "call" it so the filenames always match regardless of the currently used/configured UrlFactory.
In a template, you can output the filename by using
$CMS_VALUE(ref(pt_file).url.split("/").last)$
This way, the ref(...).url statement will simply "call" the same mechanism that is responsible for calculating the URL, whatever it is at the moment.
Michael