We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 708
    • 144 Posts
    hi

    i’ve this in my template :
    <img class="img_right" src="[*image*]" alt=""/>

    but i want to have the choice to insert the image or not in my page,because if i’dont choose an image i’ve the css border displayed alltime...
    so how can i do a tv which i can choose to insert code or not ? or perhaps make a chunk...

    thx
      AipHesBlOg
      Serveur Local Linux Ubuntu - Apache 2.2.9 - Php 5.2.6 - MySQL 5.0.67
      ---------------------------------------------------------------
      French webdesigner
      • 18374
      • 69 Posts
      You can put your existing code into a chunk, and insert something like this into your template:

      [+phx:if=`[*image*]`:isnt=``:then=`{ {chunkName} }`+]


      This will check if the image TV has a value, and include the chunk if it does. There may be a more efficient way of writing this but I’m still learning PHX. smiley
        • 7231
        • 4,205 Posts
        If you dont want to use PHx then build a snippet to check it the TV has a value or not.

        <?php
        $tv = isset($tv) ? $tv : "defaultTvNameHere"; 
        $val = $modx->getTemplateVarOutput($tv);
        if($val != ''){
            $output = '<img class="img_right" src="'.$tv.'" alt=""/>';
            return $output;
        }
        ?>


        You can set the default tv and use it like [[snippetName]] or you can assign the tv in the call [[snippetname? &tv=`tvName`]]
          [font=Verdana]Shane Sponagle | [wiki] Snippet Call Anatomy | MODx Developer Blog | [nettuts] Working With a Content Management Framework: MODx

          Something is happening here, but you don&#39;t know what it is.
          Do you, Mr. Jones? - [bob dylan]
          • 708
          • 144 Posts
          i’m learning php so i’ll take the second solution..but can i add some php to have a alt and title balises dynamically with the image name ?
          what’s i’ve done:

          <div id="contenu_txt">
          			[[imageTest]]
          			
          			
          			[*#content*]
          			
          			
          			</div>

          whith the snippet [[imageTest]] :

          <?php
          $tv = isset($tv) ? $tv : "[*image*]"; 
          $val = $modx->getTemplateVarOutput($tv);
          if($val != ''){
              $output = '<img class="img_right" src="'.$tv.'" alt=""/>';
              return $output;
          }
          ?>


          i’ve not any image now ! why ?
            AipHesBlOg
            Serveur Local Linux Ubuntu - Apache 2.2.9 - Php 5.2.6 - MySQL 5.0.67
            ---------------------------------------------------------------
            French webdesigner
            • 28042 ☆ A M B ☆
            • 24,524 Posts
            Don’t confuse the TV name with the TV tags. The tags are only for use in HTML code. In a snippet, just use the TV’s name without the tags.
              Studying MODX in the desert - http://sottwell.com
              Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
              Join the Slack Community - http://modx.org
              • 708
              • 144 Posts
              i’ve tried this too :

              <?php
              $tv = isset($tv) ? $tv : "image"; 
              $val = $modx->getTemplateVarOutput($tv);
              if($val != ''){
                  $output = '<img class="img_right" src="'.$tv.'" alt=""/>';
                  return $output;
              }
              ?>

              and is not work...i’dont understand what is the problem so...
                AipHesBlOg
                Serveur Local Linux Ubuntu - Apache 2.2.9 - Php 5.2.6 - MySQL 5.0.67
                ---------------------------------------------------------------
                French webdesigner
                • 7231
                • 4,205 Posts
                The snippet is not working because I am an idiot grin There are some errors in it that are careless. Here is a working version (tested):

                <?php
                $tv = isset($tv) ? $tv : "sbImage"; 
                $tvVal = $modx->getTemplateVarOutput($tv);
                if($tvVal != ''){
                    $output = '<img class="img_right" src="'.$tvVal[$tv].'" alt=""/>';
                    return $output;
                } 
                ?>


                The errors:
                1) I had the $tv var as the TV value and it was supposed to be the $val (I changed the $val name to $tvVal to make it more clear).
                2) $modx->getTemplateVarOutput($tv) returns an array so you need to access the value with array syntax, so rather than using the simple variable mane $tvVal we need to append the array key, which is the name of the TV $tvVal[$tv]

                You can add as many parameters as you want to add title/alt data. To simply use the path you can just add title="’.$tvVal[$tv].’" to the output string. You can use some php to isolate the file name using the basename() php function. Here is an example:

                <?php
                $tv = isset($tv) ? $tv : "sbImage"; 
                $tvVal = $modx->getTemplateVarOutput($tv);
                if($tvVal != ''){
                    $fileName = basename($tvVal[$tv]);
                    $output = '<img class="img_right" src="'.$tvVal[$tv].'" alt="'.$fileName.'"/>';
                    return $output;
                } 
                ?>
                  [font=Verdana]Shane Sponagle | [wiki] Snippet Call Anatomy | MODx Developer Blog | [nettuts] Working With a Content Management Framework: MODx

                  Something is happening here, but you don&#39;t know what it is.
                  Do you, Mr. Jones? - [bob dylan]
                  • 708
                  • 144 Posts
                  it’s working now with an image but not if the tv is empty...i’ve my css border empty... sad
                    AipHesBlOg
                    Serveur Local Linux Ubuntu - Apache 2.2.9 - Php 5.2.6 - MySQL 5.0.67
                    ---------------------------------------------------------------
                    French webdesigner
                    • 16034
                    • 107 Posts
                    Does this fix it? (adding [$tv] in if(...))

                    <?php
                    $tv = isset($tv) ? $tv : "sbImage"; 
                    $tvVal = $modx->getTemplateVarOutput($tv);
                    if($tvVal[$tv] != ''){
                        $output = '<img class="img_right" src="'.$tvVal[$tv].'" alt=""/>';
                        return $output;
                    } 
                    ?>
                      MODx snippet-glossary 101:
                      Ditto = Content Lister -- Wayfinder == Menu Builder -- Jot = Comment Control