TinyButStrong - the PHP Template Engine
Categories > TinyButStrong general >

File Exists

The forum is closed. Please use Stack Overflow for submitting new questions. Use tags: tinybutstrong , opentbs

By: Anthony
Date: 2014-09-30
Time: 22:09

File Exists

Is there a way to check if a file exists when template is shown and if file does not exist, then to not display a section of the template. For example:

<div id='section' [if fileexists show div]>
   Div content
</div>

The filename is stored in DB along with other data, so when TBS does the merge, I need to grab that filename and check if file actually exists. If not, I want the div to be deleted.
By: Skrol29
Date: 2014-10-01
Time: 01:35

Re: File Exists

Hi,

If you are using a MergeBlock() then you can use an "nodata" custom function in order to build a calculated column saying if the file exists (using PHP function file_exists()).

Otherwise you can use an "onformat" function, or even use a PHP variable  saying if the file exists.
By: Anthony
Date: 2014-10-01
Time: 20:05

Re: File Exists

So, I am getting a field value from the database when page loads, and this field represents a filename that is suppose to be on server. How do I pass that field name to the custom function and either keep or remove html tag. My custom function will fun php file_exists to see if an actual file is on the server and if not, have the div to be removed by TBS. There will or should always be a value for article_block.file but there may or may not be an actual file, so I don't need it to display when not present.

<div id='section'>
[article_block.file;magnet=div]
</div>
By: Skrol29
Date: 2014-10-02
Time: 00:01

Re: File Exists

Hi,

It is described in the documentation here:
http://www.tinybutstrong.com/manual.php#html_field_prm_onformat
By: Anthony Brock
Date: 2014-10-03
Time: 15:02

Re: File Exists

Well, I can get the custom function to be called, but it does not appear that the value I want to return gets returned. Perhaps there is something I still don't understand about how it is suppose to work.

HTML
<div id="divExtra" [onshow.divExtra;magnet=div;strconv=no]>
    <!--div for Showing PDF and Other Articles Links-->
        <div class="extra">
            <p><a class="no-decor" href="http://www.straightwayonline.org/pdf/en/[article_block.file;magnet=p;onformat=fileexists]"><img src="_assets/images/misc/pdf_version.gif" alt="PDF Version" /></a></p>
            <div id="divOtherArticles">
                <div class="window">
                    <p class="title">In This Issue:</p>
                    [onshow.otherarticles;noerr;magnet=div]
                </div>
            </div>
        </div>
    </div>

PHP
function fileexists($Field,&$CurrVal) {
    $filename = SYSTEMPATH . 'pdf/en/' . $CurrVal . '.pdf';
    if (file_exists($filename)) {
        $divExtra = 'style="display: block"';
        return $CurrVal . '.pdf';
    } else {
        $divExtra = 'style="display: none"';
        return '';
    }
}
By: Anthony Brock
Date: 2014-10-03
Time: 17:10

Re: File Exists

Ok, I discovered that I don't use return $CurrVal, but simply use $CurVal = 'desired value'. However, I suppose that TBS will not process any variables in the function and apply to html such as the $divExtra variable.
By: Skrol29
Date: 2014-10-04
Time: 10:52

Re: File Exists

That is correct. It is specified in the documentation.
The character "&" added to the variables in the function declaration (such as &$CurrVal) means that the values are passed by references.
This means when you change the value of $CurrVal the this value still remain outside of the function.
This is the way "onformat" functions works to modify the current value.