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

    I’m not really sure how to phrase this... is there a way to hide/show template variables, depending on whether or not a different one is filled in?

    Let me explain in detail:

    I would like a single TV to appear (standard text input box). If the user enters something into that box, the TV is duplicated beneath it, allowing the client to enter a different lot of text. I don’t want this to be infinite, it should be limited to around 5 text inputs.

    My project is going to have a "related articles" tab, and within that will be a list of around 10 related articles. What I want to do, is give the client the ability to "override" these related articles. The first idea (and one I shall go with for now) is that I will have 5 separate TVs. This will allow the client to add their own links, whether it’s a link to a resource within MODx, a video, or a podcast. This will then appear instead of the automatically generated link.

    The problem with this method is that it takes up more space than needed, since 90% of the time it won’t be used at all.
    So I was thinking, is there a way to hide/show a template variable, depending on whether or not a different one is filled in?

    Any help with this would be brilliant smiley - if not a direct solution, maybe an alternative route for something similar?

      Tom Dyer

      Designer/Animator/Web Developer - England, UK.
      http://www.bespokepixels.co.uk
      • 7914
      • 137 Posts
      If I understand you correctly, you want to display something if there is something to display in any given TV. Otherwise don’t show.

      This is how I set things up like that, if there is a better way please chime in. I’m all for improvement!
      I am trying to grow my knowledge and better my work with each project I work on. So now I like to call my snippets externally rather than pasting inside of MODx. I like this only because I can update the php in my editor and not have to copy/paste back into MODx.

      Here is what my include snippet looks like. This is the only snippet I have that is pasted inside of MODx. Call it "include"
      switch(getenv('HTTP_HOST'))
      	{
      		case 'local.yoursite.com': // LOCAL Environment
      			$path = "/path/to/your/site/assets/snippets/";			
      		break;
      		case 'dev.yoursite.com': // DEV Environment
      			$path = "/path/to/your/site/assets/snippets/";
      		break;
      		default:  // PRODUCTION Environment
      			$path = "/path/to/your/site/assets/snippets/";
      		break;
      	}
      	
      	if ( file_exists($path.$file) )
      	{
      	   $o = include $path.$file;
      	}
      	else
      	{ 
      		$o = 'File not found at: '.$path.$file;
      	}
      	
      	return $o;
      


      I like to use a switch since I work in several environments. This is very handy once the site starts moving through the development phases. I don’t have to maintain multiple versions for each environment this way.


      This is what the snippet call looks like on my page template:
      [[!include? &file=`name_of_your_snippet.php`]]
      



      This is the TV snippet I use to display the TV.
      $output = "";
      $resource_id = $modx->resource->get('id'); // get the resource id we are working with
      
      // check if there is an image available
      $image = $modx->getObject('modTemplateVar',array('name'=>'your_tv_name'));   // get the tv object
      $image_value = $image->getValue($resource_id); // get the value from the tv
      
      if ($image_value != "")
      {
          $output .= '<img src="'.$image_value.'" width="151" alt="" />'."\n";
      }
      else
      {
          // you could provide a fallback if you wanted here 
      }
      
      return $output;
      


      So as you can see, I look for the TV, get the value of the TV, then check if the value is null or not. If it is NOT null, I display the value. If it IS null, I can either do nothing, or display a default/fallback image.

      Hope this helps!

        • 30703
        • 51 Posts
        Hi,

        Thanks for your reply - I’m not sure this is exactly what I want though (unless I’m misunderstanding what your code does).

        I’ve attached a screen shot to hopefully explain my situation better, I would just like ONE text input to begin with, then if they enter some text into it, display a second. If the second is populated, display the 3rd, and so on.

        Ideally there will a total of about 10 text inputs but that’s only if I can make this method work, otherwise I think it’s too many to display all of the time. For now I’m just displaying 4 text inputs, but considering they won’t get used all the time it would be nice to hide fields that are not needed.

        regards,
        trymedo

          Tom Dyer

          Designer/Animator/Web Developer - England, UK.
          http://www.bespokepixels.co.uk
          • 7914
          • 137 Posts
          I understand now. I’ll be the first to admit that’s going to be way outta my league embarrassed
            • 15083
            • 697 Posts
            Hi there,

            Your getting into the land of the plugins.
            There are several events that fire when a page is loaded in the backend.

            U would need to add some JS code tho to the page to check if the textbox is filled out etc, and do the show/hide stuff.
            And this....is out of my league too smiley

            There are plugins that make custom TV’s, so I guess u might look here for a start.
            I’ve only written very simple plugins for MODx Evo, and I see your using Revo so it may be different.

            J
              • 30703
              • 51 Posts
              @dowhatnow - thanks for your input anyway smiley if/when I get my head around this one I’ll post the code here.

              @jusmeig - Thanks for pointing me in the right direction, I’ll look into plugins. I’ve been working with jQuery a bit lately so I might be able to accomplish this myself now.

              Of course, if anyone else has the solution in the meantime please let me know ! wink

              Thanks

                Tom Dyer

                Designer/Animator/Web Developer - England, UK.
                http://www.bespokepixels.co.uk
                • 4172
                • 5,888 Posts
                on revo you can try to use the multiItemsGridTv where you can add multiple items with one or multiple tv-input-types per item.
                  -------------------------------

                  you can buy me a beer, if you like MIGX

                  http://webcmsolutions.de/migx.html

                  Thanks!
                  • 30703
                  • 51 Posts
                  @Bruno17 - Sounds interesting! where can I find this? It doesn’t appear to be in the downloadable packages or anything?
                  Thanks

                    Tom Dyer

                    Designer/Animator/Web Developer - England, UK.
                    http://www.bespokepixels.co.uk
                    • 4172
                    • 5,888 Posts
                    Hadn’t the time to package it together, but you can download it from github.
                    You can find the links on this forum-thread, see my signature.
                    There is also a wiki-page on github, which describes in short how to install and to use.
                      -------------------------------

                      you can buy me a beer, if you like MIGX

                      http://webcmsolutions.de/migx.html

                      Thanks!
                      • 30703
                      • 51 Posts
                      @Bruno17 - After a few initial hiccups, your multiItemsGrid is working brilliantly! I do however have one slight problem with it (which isn’t too bad but seems like a ’bug’ nonetheless...

                      I have set up my MultiItemGrid TV to display 2 text inputs for each item (Title and URL). By default you can drag and drop a page from the site tree on the left into the URL field. The only problem is that where is should place [[~2]], it actually inserts [[~2]][[~2]].
                      So it appears it’s picking up the drop handler twice for this? I’m not that experienced with MODx revo so I don’t know how to fix this problem.

                      For now I’ll tell the client to just copy and paste the url from the browser address bar but if you can find a solution to this issue that would be awesome! smiley


                      good work on this one though - I didn’t realise at first how customisable this snippet was - I’m sure I’ll be using it again and again!

                        Tom Dyer

                        Designer/Animator/Web Developer - England, UK.
                        http://www.bespokepixels.co.uk