Skip to content
General Revolution Evolution Add-ons International
Login | Register | MODX.com
MODX Open Source Content Management System, Framework, Platform and More.
Find a Partner | Hosts + SaaS | Jobs | Donate
  • RegisterSign Up with the MODX Community
  • LoginUse Your MODX.com Account
  • MODX Community Forums
  • Evolution
  • Add-on Development
  •  
  • Conditional Template Variable#

  • 20993
    37

    natedogg3304 Reply #1, 3 years, 9 months ago

    Reply
    • Link to this post#1
    Hello-

    I have TV that is setup for an image. What I want to do is write a conditional statement that checks to see if an image path is present and if it is it displays the image, but if not it doesn't display anything.

    I am not a PHP developer, so I basically made up this code and put it in a snippet called CondImage, although I know it is wrong:


    <?php
    $image = [*sidebarimage*] 
    
    if $image == "" {
    } else {
    <img src="[*sidebarimage*]" alt="" />
    }
    ?>


    Then I would just call the snippet from within my template. Any help straightening me out would be appreciated.

    Thanks



  • 4310
    2,246
    Snippets : eForm 1.4.4.7 with SMTP & PMS's security fixes | PHC Results | EasyNewsletter from CSV

    Wiki Articles : Custom DB table to placeholders | Manager Locked Out?

    Websites : Work - U.K. based MODx development  |  Hobby - Free interactive education resources for teaching 4 - 11 year olds

    Hosting : One Smart Host - Professional MODx hosting 

    bunk58 Reply #2, 3 years, 9 months ago

    Reply
    • Link to this post#2
    One way I've used is have a TV for your image link say named "ImageLink"
    Create your snippet:
    if( $TVarray = $modx->getTemplateVarOutput(array(ImageLink)) ){ $tvOutput = $TVarray[ImageLink];
    if(empty($tvOutput)) return '';
    else return ''.$tvOutput.''; };

    Assign the the TV to your template, add the tag [*ImageLink*] wherever you want the image to appear.
    Add the call to your new snippet in the template.
    If you add a path to an image in the "ImageLink" TV it should appear, if the TV is empty then nothing will.
    EDIT - changed to a snippet


  • 20993
    37

    natedogg3304 Reply #3, 3 years, 9 months ago

    Reply
    • Link to this post#3
    Thanks for the help! but, I haven't gotten it to work yet.

    I have a TV for the image path called: [*sidebarimage*]

    Then I created a snippet and added your code to it called:

    then in my template I have something like:

    <div class="navbottom" >[[CondImage]]<img src="[*sidebarimage*]" alt="" border="0" /></div>


    Can you help/explain to me what I did wrong here.

    Thanks



  • 4310
    2,246
    Snippets : eForm 1.4.4.7 with SMTP & PMS's security fixes | PHC Results | EasyNewsletter from CSV

    Wiki Articles : Custom DB table to placeholders | Manager Locked Out?

    Websites : Work - U.K. based MODx development  |  Hobby - Free interactive education resources for teaching 4 - 11 year olds

    Hosting : One Smart Host - Professional MODx hosting 

    bunk58 Reply #4, 3 years, 9 months ago

    Reply
    • Link to this post#4
    Sorry for the confusion the snippet looks for content in the TV.
    If you have
    <img src="path-to.jpg" alt="" border="0" />

    in the TV and
    <div class="navbottom" >[[CondImage]]</div>
    in the template/page then if there is a path it will display the image.
    If not then the <div> would be empty.


  • 20993
    37

    natedogg3304 Reply #5, 3 years, 9 months ago

    Reply
    • Link to this post#5
    Yep, that did it. Thanks a ton


  • 4310
    2,246
    Snippets : eForm 1.4.4.7 with SMTP & PMS's security fixes | PHC Results | EasyNewsletter from CSV

    Wiki Articles : Custom DB table to placeholders | Manager Locked Out?

    Websites : Work - U.K. based MODx development  |  Hobby - Free interactive education resources for teaching 4 - 11 year olds

    Hosting : One Smart Host - Professional MODx hosting 

    bunk58 Reply #6, 3 years, 9 months ago

    Reply
    • Link to this post#6
    Glad you got it working.





Actions

Login to Post

Other Support Options

To file a bug or make a feature request visit our issue tracker, or you can also purchase commercial support.

Love MODX?

If you build sites for a living with MODX or just love using it, why not give back?

Information

Posted in this thread:
bunk58, natedogg3304

 
Back to Top

MODX Global HQ

1333 N Stemmons Fwy, Ste 110
Dallas, TX 75207
United States

+1 (469) 777-MODX (6639)

The MODX Company

  • Contact
  • Media Center
  • Work at MODX
  • Wall of Fame
  • The MODX Blog

Sponsors

SoftLayer Firehost: Secure Cloud Hosting

Stay Connected

Read our previous email newsletters.

Twitter Facebook Google+ LinkedIn github Feeds

Privacy Policy | Terms of Service | Pixels by AKTA Web Studio© 2005-2012 MODX. All rights reserved. Trademark Policy