Problems, need help? Have a tip or advice? Post it here.
8 posts Page 1 of 1
Hi there!

I have an editable region which is type='file'. I would like to be able to then display different things on the webpage depending on the type of file uploaded into this region. I was hoping to catch the two main types, with a fallback 'else' for downloading the file if it doesn't match one of the two main types. Therefore, what I was hoping to code was:

<if extension = .pdf>
<iframe for displaying pdf>
<else />
<if extension = .html>
<iframe for displaying html>
<else />
<href download file>
</if>
</if>

I am pretty confident that I can do all of this, except for the initial step of querying the extension of the file once it is uploaded. I have tried to use regex, but I don't really know how to make it work within a couchcms tag. At its most basic I have tried

Code: Select all
<cms:if uploaded_file = 'regex=.*\.pdf$'>


Simply because I came across a similar use of regex in the documentation for the 'validator' field of the editable tag. I have played around with it, but it just never returns true so I feel like it is simply not using the regex.

Is it possible, with cms:if or cms:set to query the extension? I did also wonder if I could do something like

Code: Select all
<cms:set uploaded_file_extension = (last four characters of uploaded_file string)><cms:if uploaded_file_extension == '.pdf'>


But I am not aware of how I would 'cut' the file string to just the last 4 characters?

Any help on this would be much appreciated.

Best

Anthony
Your problems with uploaded_file = 'regex=.*\.pdf$' are - (1) on the right side of the equation there must be a value or a valid expression. Valid expressions are evaluated only if in double quotes. In single quotes we supply only strings. (2) There is no expression on the right side and <cms:if> will compare uploaded_file directly with righthand side supplied string.

There is a tag <cms:validate>, it supports all validators that we know or can write ourselves. Regex is a valid one, so let's use it e.g.
Code: Select all
<cms:if "<cms:validate value=uploaded_file validator='regex=/php$/' />" >
.. file is supposedly .php
<cms:else_if "<cms:validate value=uploaded_file validator='regex=/pdf$/' />" />
.. file is supposedly .pdf
<cms:else />
! neighter php or pdf
</cms:if>


Why is it said that 'file is supposedly' php or pdf? It is because validator will fail on links with querystrings or anchors like 'file.php?p=1#about-us'
Also, there is no check if file actually exists - you don't want to load an iframe or fire a download on nothing.
Such cases must be handled with some custom coding. Below you'll find an example of custom function 'get_file_extension' (functions explained in details in https://couchcms.com/forum/viewtopic.ph ... =10#p30174 ).

Code: Select all
<cms:set uploaded_file_ext = "<cms:call 'get_file_extension' file=uploaded_file />" />
<cms:if uploaded_file_ext = 'php' >
.. surely php
<cms:else_if uploaded_file_ext = 'pdf' />
.. surely pdf
<cms:else_if uploaded_file_ext = '' />
.. file not found or has no extension
<cms:else />
.. file has some other extension, not php/pdf
</cms:if >


All clear?
To find an extension - split filename by a dot, as is -

Code: Select all
<cms:set file_extension = '' scope='global' />
<cms:each var='http://example.com/myfile.pdf' sep='.'>
    <cms:if k_last_item>
        <cms:set file_extension = item scope='global' />
    </cms:if>
</cms:each>


The simplest.
Hi trendoman!

That's perfect, thank you for taking the time to explain the validator function and the regex side of things.

Since I needed a quick fix, and the file upload is a 'required' field for the page, I have simply adapted your first response so that I now have this:

Code: Select all
<cms:if k_is_page ><!-- below is the HTML when displaying a single process flowchart -->
<cms:if "<cms:validate value=uploaded_file validator='regex=/pdf$/' />" ><!-- if file is pdf -->
    <cms:redirect url="<cms:show uploaded_file />" />
<cms:else />
<cms:if "<cms:validate value=uploaded_file validator='regex=/html$/' />" ><!-- if file is html -->
<div class="container">
    <div class="row">
        <div class="col-12 pt-sm-5">
            <h1><cms:show k_page_title /></h1>
        </div>
        <iframe class="col-12"  id="process_iframe" src="<cms:show uploaded_file />" onload='javascript:(function(o){o.style.height=(o.contentWindow.document.body.scrollHeight*1.05)+"px";}(this));' scrolling="no" style="height:200px;width:100%;border:none;overflow:hidden;"></iframe>
        <div class="col-12 pt-sm-5">
            <p><cms:show process_description /></p>
            <h5>Last updated: <cms:show k_page_modification_date format='d M Y' /></h5>
        </div>
    </div>
</div>
<cms:else />
<div class="container">
    <div class="row">
        <div class="col-12 pt-sm-5">
            <h1><cms:show k_page_title /></h1>
        </div>
        <div class="col-12" style="color: red;">
            <h2>Error displaying process page.</h2>
            <p >Please report to admin.</p>
        </div>
        <div class="col-12 pt-sm-5">
            <p><cms:show process_description /></p>
            <h5>Last updated: <cms:show k_page_modification_date format='d M Y' /></h5>
        </div>
    </div>
</div>
</cms:if>
</cms:if>


It works perfectly - it actually means that a .pdf upload redirects to the .pdf file, so the browser handles how to display it; a .html upload is shown in an iframe, and anything else throws a simple error message so that we know that someone has uploaded something incorrectly. I'd love to devote the time to making it more complicated than this, but this particular project just does not deserve that level of work! :lol:

I couldn't use <cms:else_if /> so is that a custom function?? Could you point me to any documentation on it?

Thanks

Anthony
AnthonyW90 wrote: I couldn't use <cms:else_if /> so is that a custom function?? Could you point me to any documentation on it?

Thanks

Anthony


Are you kidding?! Available since I was born on this forum - viewtopic.php?f=5&t=8581 Section 3. Some new tags
Hi trendoman!

Thank you for the link. My knowledge of couchcms is primarily based on the documentation provided, and on what I can find by googling "couchcms keyword". Sometimes I use the forum search function, but it tends to return a lot of results that are not relevant.

I have searched google for "couchcms else_if" and it doesn't really return anything useful, other than further evidence that the tag may be in use. I also couldn't find it easily in the forum search because it has been mentioned so many times since the original post announcing it.

Perhaps this means it ought to be documented? "if" and "else" are both documented, and both link to one another as 'related' tags. Adding the "else_if" tag to the documentation would likely be helpful to others too. I've been using couchcms for a few years now on projects and this is the first I've heard of it!

Regardless, when I tried to use it (in the same manner as you showed) it returned an error that there was a missing closing if tag. I may investigate further, but for now I can probably cope with one nested if tag.

Thanks again

Anthony
I advise to search using Google's hinting option which always works better than native forum search:

site:couchcms.com/forum keyword
Hi trendoman

Thanks - will use that in future! :D

However, I just tried that method with "else if" as the keyword, and across the 6 pages of results I did not find the guidance on how to use it, or indeed that it is available to use. I truly think that people are missing out on how good couchcms is, because they are not aware that it can do all of this stuff that has been added and not documented in the 'documentation' area. As I said, I have been using couch for a few years now (and have used nested if statements in the past), and have never previously come across any posts showing or explaining that else_if was an option :? Only thanks to you pointing me directly to it do I now know that it is available.

Thank you for the support! :D

Anthony
8 posts Page 1 of 1
cron