ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

if image file exists...

April 29, 2009 2:29pm

Subscribe [5]
  • #1 / Apr 29, 2009 2:29pm

    Devhead

    22 posts

    Hiyas,

    My site has a typical front page and then every other page falls into sections.  Each section has a common structure, with one area on the page that holds a logo that represents that section.  Not every section has it’s own logo, though.  The logo’s are all named with the same scheme: sectionName_logo.gif. 

    What I’d like to be able to do is use an if block to test for the existence of a particular section logo (whatever section you navigate to), and if that file doesn’t exist (section doesn’t have it’s own logo), then use a ‘default’ logo instead.

    Can that be done via EE tags or is that something I’d have to do with php in a template.  Anyone have any code to do that I could see?

    Thanks,
    DH

  • #2 / Apr 29, 2009 2:42pm

    Mark Bowen

    12637 posts

    Hi Devhead,

    That all really depends on how the images are placed into either an entry or the templates really.

    You mentioned that each section has an image named in a certain way. Where are the file references for these images stored or are they simply uploaded to your server and not in an entry somewhere?

    If for instance the images were stored in an entry in a custom field for example called {section-image} then you could very easily do something like this :

    {if section-image != ""}
    {section-image}
    {if:else}
    standard-image.jpg
    {/if}

    If however you don’t have the file names stored anywhere in the database system then this is going to make things a little harder. It then all really depends on how you are entering the data into your weblog as there are a couple of ways that you could go about this.

    First up you could use categories as your sections although you may already be doing something different if you already have sections?

    If not then you can categorise your entries using the categories and in the setup of the categories you can assign an image URL for that category / section and then you should be able to use that in a conditional. Alternatively if you don’t wish to use categories then you could just make a drop-down menu in the weblog which would hold say the names of the images and then you just choose the image from the drop down for each entry that you create. You could then just do this in your template :

    Drop-down menu choices - Custom Field called {section-name}
    sectionNameOne.jpg
    sectionNameTwo.jpg
    sectionNameThree.jpg

    Template Code

    {if section-name != ""}
    {section-name}
    {if:else}
    standard-section-image.jpg
    {/if}

    Hope that helps a bit.

    Best wishes,

    Mark

  • #3 / Apr 29, 2009 2:48pm

    Bernie_the_one

    31 posts

    Should be easy if you have a field where the path to the images are stored. like:

    {if image}
    {image}
    {else}
    -> do what you want here
    {/if}

    if needed put more conditionals, read here: http://expressionengine.com/docs/templates/globals/conditionals.html

    cheers,

    bernd

  • #4 / Apr 29, 2009 3:31pm

    Devhead

    22 posts

    Thanks for the timely replies.

    I recently took this site over from it’s previous developer.  So, sometimes I have to go check how things are because they came that way to me in many instances…

    One clarification about ‘sections’.  I think the previous developer just renamed the ‘weblogs’ into ‘sections’.  That said, in my prior post, I was actually meaning ‘sections’ in terms of different areas of the website.  For the most part, each section has two weblogs (called ‘sections’ in my cp) associated with it.  One is for the main content area, and one is for some data that goes along with these logo images I’m talking about.  The image and it’s related data appear in the same area of the page, but they apparently aren’t structurally related inside EE because…

    The images are just stored in a folder on the server.  The reference to the section image looks like this:

    {site_url}images/{segment_1}_logo.gif

    The img tag puts up the alt text if/when the image isn’t found. 

    I COULD just make copies of the ‘default’ image for each section that doesn’t have it’s own image, but something about that workaround sickens me, and I’d rather figure out an EE way of handling this.

    It looks like it would have been better if the images, or at least the path to each image, should have been included in a field in one of the weblogs.  I guess I could go back and add the field…

    I really think I’d prefer NOT to enable php in a template..

    Oh well, have to think a little more…

    Thanks again for the replies,
    DH

  • #5 / Apr 29, 2009 5:13pm

    Devhead

    22 posts

    Hi again,

    Ok, so I added a field to the weblog/section that populates the area of my pages where the logo goes, here’s the code:

    {if image_section_path !=""}
    {embed:image_section_path}
    {if:else}
    logo.gif
    {/if}


    The code for the alternative/default image (just below the if:else) works fine, but what am I doing wrong in the first two lines because that isn’t working.

    I also tried:

    image_section_path

    Thanks for any help,
    DH

    PS: It doesn’t matter because it’s the part that’s working, but I modified the path of the img tag after the if:else to remove the domain for posting.  Again, that part of the code works.

  • #6 / Apr 30, 2009 9:57am

    Devhead

    22 posts

    One more follow-up in hopes for some direction.

    When I view source on a page, it’s apparent that the simple address value I have in the field in the weblog, like:

    <a href="http://www.mydomain.com/images/section4_logo.gif">http://www.mydomain.com/images/section4_logo.gif</a>

    is being inserted into my template as a link:

    <a href="http://www.mydomain.com/images/section4_logo.gif">http://www.mydomain.com/images/section4_logo.gif</a>

    Anyone know why it does that?  I just want the plain address put where I have the the tag that references that field.

    Thanks,
    DH

  • #7 / Apr 30, 2009 10:05am

    Mark Bowen

    12637 posts

    Hiya,

    Check :

    Admin->Weblog Administration->Weblog Management->Edit Preferences (Your Weblog)->Weblog Posting Preferences->Automatically turn URLs and email addresses into links?

    Make sure that is set to No and hopefully those problems should go away for you.

    Hope that helps a bit.

    Best wishes,

    Mark

  • #8 / Apr 30, 2009 10:23am

    Devhead

    22 posts

    Bingo!  Thanks Mark!

  • #9 / Apr 30, 2009 10:29am

    dwex

    94 posts

    Hi again,

    Ok, so I added a field to the weblog/section that populates the area of my pages where the logo goes, here’s the code:

    {if image_section_path !=""}
    {embed:image_section_path}
    {if:else}
    logo.gif
    {/if}


    The code for the alternative/default image (just below the if:else) works fine, but what am I doing wrong in the first two lines because that isn’t working.

    Well, the variable you are testing in the conditional isn’t the same one you’re using in the <img> tag. I’m guessing that you need “embed:image_section_path” in the {if}

  • #10 / Apr 30, 2009 11:17am

    Mark Bowen

    12637 posts

    Bingo!  Thanks Mark!

    No problem at all.

    Hope it all goes well for you.

    Best wishes,

    Mark

  • #11 / Apr 30, 2009 1:01pm

    grovberg

    50 posts

    I just want to say that I found this thread immensely useful (for a slightly different purpose) as well, so thanks Mark.

  • #12 / Apr 30, 2009 1:08pm

    Mark Bowen

    12637 posts

    I just want to say that I found this thread immensely useful (for a slightly different purpose) as well, so thanks Mark.

    No problem at all 😉 Thanks for the kind words.

    Best wishes,

    Mark

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases