View previous topic :: View next topic |
Author |
Message |
toesofrubber Smarty n00b
Joined: 15 Oct 2003 Posts: 1
|
Posted: Wed Oct 15, 2003 12:02 pm Post subject: file exists using multiple parameters |
|
|
Hi everyone,
I have a question which i am hoping someone can help me out with here. I have spent hours trawling around and trying to figure this out, but it's got me flummoxed. I suspect it will turn out to be trivial and show me up for the goon i suspect i may be!
anyhow.. i have a template which displays product information from a mySQL database, in which i wish to conditionally show an image, if the file exists in a particular directory. The file path needs to be a concatenation of a static path, and the product partnumber, which is a smarty variable. e.g
"/images/products/photos/"{$PartNo}".jpg"
that works fine for showing the image unconditionally, but i can't for the life of me work out how to do it conditionally..
any advice gratefully received chaps.. keep up the good work! |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Wed Oct 15, 2003 1:38 pm Post subject: |
|
|
One way would be to assign $PartNo if the image exists (do that logic in your app.) Then in the template:
{if $PartNo ne ""}
<img src="/images/products/photos/{$PartNo}.jpg">
{/if}
That said, using the emptiness of $PartNo to determine whether or not to show the image is a bit sloppy IMHO. To go a step toward clean app/view logic separation, leave $PartNo out of the template and assign the image name (or the whole path.) Then assign a boolean value that the template uses to determine whether or not to show an image.
{if $show_part_image}
<img src="{$part_image_path}">
{/if}
The template is now working off info you specifically assigned for the task, and if you ever changed the path to your part images, the templates wouldn't need to be edited.
For ultimate flexability and template cleanliness, try a custom function:
{show_image file=$part_image_path}
This would do the work of determining if the file exists and what to display.
Now your image display logic is centralized in one function in your application, and the template syntax is reduced to one line. |
|
Back to top |
|
pt2002 Smarty Regular
Joined: 05 May 2003 Posts: 89 Location: Porto, Portugal
|
Posted: Wed Oct 15, 2003 10:31 pm Post subject: |
|
|
Hi Mohrt
I had the same problem some time ago.
Could you explain or provide the las tip you gave?
Quote: | For ultimate flexability and template cleanliness, try a custom function:
{show_image file=$part_image_path}
This would do the work of determining if the file exists and what to display.
Now your image display logic is centralized in one function in your application, and the template syntax is reduced to one line. |
Thank you very much |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Wed Oct 15, 2003 11:21 pm Post subject: |
|
|
pt2002: are you familiar with Chapter 16. Extending Smarty With Plugins?
Monte is suggesting writing such a plugin. It is easy to do and would allow your template designer to include the functionality with little fuss. One thing, though, is that the typical way to write plugin functions is to hardcode html in your plugin code. That is not my favourite choice, though it is not a bad compromise, particularly if you keep your plugin functions limited to produce very specific and short segments of HTML. For example, the HTML_* plugin functions do a good job of abstracting the HTML code to produce tables, options, etc. Unfortunately, it hides the actual HTML from your designer (and your designer's tools). This is okay, though, since the HTML for things like option boxes is canned anyhow.
In general, I think it is better to pre-compute your needs in your application and provide the correct data to your template. If it doesn't make sense to do that, then I would use either the {if} strategy or perhaps a modifier. My last choice would be to encapsulate in a plugin UNLESS the item being encapsulated is a "canned" segment. |
|
Back to top |
|
pt2002 Smarty Regular
Joined: 05 May 2003 Posts: 89 Location: Porto, Portugal
|
Posted: Thu Oct 16, 2003 9:04 am Post subject: |
|
|
Hello boots
I know a little about plugins.
I have some modifiers to format dates in 'a Portuguese' way and some plugins with the new cacheabilty fetaures but I was wondering what kind of plugin should be used in this situation.
Sorry for this questions and thank you very much Monte & Boots.
Greetings |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Thu Oct 16, 2003 1:38 pm Post subject: |
|
|
boots wrote: | One thing, though, is that the typical way to write plugin functions is to hardcode html in your plugin code. That is not my favourite choice, though it is not a bad compromise, particularly if you keep your plugin functions limited to produce very specific and short segments of HTML. |
Boots has a good point. The show_image function should have "canned" enough output to justify the hard-coded HTML. About the only thing you would need control of is parameters, which you can just pass through to the output. Another way around it is to pass a template to your plugin:
{show_image file=$image_path tpl="path/to/image.tpl"}
I've done this with {insert} functions too. |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Thu Oct 16, 2003 6:35 pm Post subject: |
|
|
mohrt wrote: | Another way around it is to pass a template to your plugin:
{show_image file=$image_path tpl="path/to/image.tpl"}
|
Yes, that is a great method, particularly since it means that the raw html is still editable by your designer. Probably best to have a default template so to ease your designer's writing burden (eg: {show_image file=$image_path}) while still allowing for the default/custom templates to be redesigned and specified without programmer interference.
pt2002, I'm not sure if your question was answered: do you require something more specific? |
|
Back to top |
|
pt2002 Smarty Regular
Joined: 05 May 2003 Posts: 89 Location: Porto, Portugal
|
Posted: Thu Oct 16, 2003 9:34 pm Post subject: |
|
|
Hello
I've got all the answers I need for this problem.
Thank you very much again.
Greetings |
|
Back to top |
|
|