|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
FSSmariani Smarty n00b
Joined: 26 Jan 2021 Posts: 2
|
Posted: Mon Apr 19, 2021 9:09 pm Post subject: my first plugin function - does file exist? |
|
|
Good day all.
I have written my *first* Smarty plugin function. Unfortunately, it doesn't work (yet!).
I created smarty_plugins/function.file_exists.php to return a boolean if the product image file exists.
Code: |
<?php
/*
* Smarty plugin
* -------------------------------------------------------------
* File: function.file_exists.php
* Type: function
* Name: file_exists
* Purpose: returns boolean based on existance of product image file
* -------------------------------------------------------------
* Date Who Why
* 2021-04-19 MEM Per Eventum #3060.
* -------------------------------------------------------------
*/
function smarty_function_file_exists($filename, $smarty)
{
$smarty->assign($filefound,0);
if (file_exists('/images/products/$filename'))
{
$smarty->assign($filefound,1);
}
}
?>
|
I tried calling it from .tpl as:
Code: |
{if file_exists('/images/products/{$product->mProduct[k].image}')}
<div><img src='/images/products/{$product->mProduct[k].image}' alt="FSS product {$product->mProduct[k].code}" width="80" height="80" onError="this.visible=false;" /></div>
{else}
<div><img src='/images/not-available.jpg' alt="FSS product {$product->mProduct[k].code}" width="80" height="80" onError="this.visible=false;" /></div>
{/if}
|
I wasn't sure if I needed to register this, since I believe I used the correct directory and naming convention. Do I?
Is the syntax wrong?
When I execute, I get the default image for all products.
I added the $smarty argument and $smarty->assign statement, but see other examples that don't seem to have these. So, I am little confused.
Thanks for your advice. |
|
Back to top |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Tue Apr 20, 2021 4:11 pm Post subject: |
|
|
You've called PHP native function.
What you created is a template function {file_exists}.
Which is rather useless. You shouldn't check that in your template. More than that, you shouldn't ever make tests like that in your PHP code.
What if static files are served from a different server?
Just don't do that. |
|
Back to top |
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|