We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 10313
    • 375 Posts
    Hello,

    I’m wondering how I could access the values of a TV directly from the PHP in my template.

    Example: I hvae two TVs for my template, both of type image. The second can be left blank. I now want to output these two images directly in my template. That’s no problem if both TVs are filled with values. But I get an HTML warning when I leave the second one blank, because the src attribute has no value. So I want to check the TV value by a line of PHP code. And only if there is a value, the HTML should be output.

    I tried
    <?php
    if("[*rechtesBild*]" != ""){
    ?>
                  <img src="[*rechtesBild*]" alt="." />
    <?php
    } //  Ende optionales Bild
    ?>
    

    but this doesn’t seam to work. Can I access the TV without using the $modx object?

    Thanks and greetings
    Martin

      • 28042 ☆ A M B ☆
      • 24,524 Posts
      You can’t use MODx tags in snippet code. You have to use the documentObject and other API objects and functions.

      You might be able to use phx to check for a value in the TV. Or, have a default image that gets used, a 1-px transparent image or something.
        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
        • 9941
        • 65 Posts
        phx does this with the help of a custom modifier:

        http://wiki.modxcms.com/index.php/PHx/CustomModifiers#phx:ifnotempty


          • 7231
          • 4,205 Posts
          Here is how you could do it in a snippet:

          <?php
          
          $tv = isset( $tv ) ? $tv : "rechtesBild";
          $id = isset( $id )? $id: $modx->documentIdentifier;
          $tvValue = $modx -> getTemplateVarOutput( $tv, $id, $published = 1 );
          
          if($tvValue[$tv]){
          
          	$output = "<img src=\"$tvValue[$tv]\" alt=\".\" />";
          	return $output;
          }
          
          ?>


          If you want to use a chunk in order to reuse the same snippet with different output you could do that by changing it like this:
          <?php
          $chunk = isset($chunk) ? $chunk : '';
          $tv = isset( $tv ) ? $tv : "rechtesBild";
          $id = isset( $id )? $id: $modx->documentIdentifier;
          $tvValue = $modx -> getTemplateVarOutput( $tv, $id, $published = 1 );
          
          if($tvValue[$tv]){
          
          	return $modx->getChunk($chunk);
          }
          
          ?>


          You would call it like this:

          [[SnippetName? &chunk=`chunkName`]]
            [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]
            • 10313
            • 375 Posts
            Hello!

            Thanks for all the tipps. But -- my problem is how to access the TV directly from within the template. Without using a snippet for that. I have coded some small snippets and I know that it works all very fine. Is this possible at all?

            Greetings
            Martin
              • 10313
              • 375 Posts
              Sorry, dev_cw,

              the code of you does exactly what I was searching for. Directly from within the template. It works perfect for me.

              Thank you
              Martin
                • 7231
                • 4,205 Posts
                Sorry, dev_cw
                If it works I am happy grin
                  [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]