We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 44165
    • 20 Posts
    Hello smart(er than me) people of the Modx community!

    I am a bit stumped here. What I am trying to accomplish is pretty simple in theory:
    If the content of a MIGX inputTV exceeds a specified # of characters, display a link to display the full contents of the TV. If it does not, just display the full contents of the TV without the link.

    I have a MIGX TV with a field called details:
    {"field":"details","caption":"Details","inputTVtype":"richtext"}


    If the contents of this TV is greater than, say 800 characters, I would like to add a 'More Info' link to the end of it. This link will open the full contents of the TV in a modal window. Note that said link, as well as it's target rely on using an output modified TV placeholder to function properly. If the content is less than 800 chars, I don't want the link to appear.

    Right now my getImageList Tpl looks like this:
    <div class="accordion" data-accordion>
    	<div data-control class="data-control">
    		<h2>[[+name]]</h2>
    		<span class="location">[[+location]]</span> - <span class="date">[[+date:strtotime:date=`%m.%d.%Y`]]</span>
    	</div>
    	<div data-content>
    		<div class="poster">  
    			<img src="uploads/[[+image]]" alt="[[+name:htmlentities]]" />
    		</div>
    		<div class="details">
    			[[+details:ellipsis=`800`]] 
    			<a class="more-info-link" data-remodal-target="[[+name:stripString=`,`:replace=` ==-`]]">More Info</a>
    		</div>
    		<div class="more-info">
    			<div class="remodal" data-remodal-id="[[+name:stripString=`,`:replace=` ==-`]]">
    				<button data-remodal-action="close" class="remodal-close"></button>
    				[[+details]]
    			</div>
    		</div>
    	</div>
    </div>
    


    So if the content of the details field is over 800, Yay! But if it is under 800, I get a "More Info" link that just shows the same content again in the modal window when clicked.

    This is tricky I think. I tried creating a simple snippet (my first attempt at making one):
    <?php
    $full = "[[+details]]";
    $ellipsis = "[[+details:ellipsis=`800`]]";
    $more = "[[$readmore]]";
    if (strlen($full) <= 800){
        return $full;
    } 
    else 
    {
        return $ellipsis.= $more;
    }

    But that totally didn't work. I'm not very versed in PHP, and I'm even sure if I can use TV placeholders as variables in a snippet. I found out I can't use Chunks anyway...

    Anyone have any ideas on how I could accomplish this?
    Thanks!

    This question has been answered by Bruno17. See the first response.

    [ed. note: chenrupo last edited this post 7 years, 7 months ago.]
    • discuss.answer
      • 4172
      • 5,888 Posts
      try this:

      <?php
      
      $text = $modx->getOption('text',$scriptProperties,'');
      $len = $modx->getOption('len',$scriptProperties,500);
      $fullTpl = $modx->getOption('fullTpl',$scriptProperties,'');
      $shortTpl = $modx->getOption('shortTpl',$scriptProperties,'');
      
      if (strlen($text) <= $len){
          $output = $modx->getChunk($fullTpl,$scriptProperties);
      } 
      else
      {
          $output = $modx->getChunk($shortTpl,$scriptProperties);
      }
      
      return $output;
      


      call it like that:

      [[switchChunkAtLen?
      &text = `[[+details]]`
      &name = `[[+name]]`
      &fullTpl = `myFullTpl`
      &shortTpl = `myShortTpl`
      &len=`800`
      ]]
      


      myFullTpl:
              <div class="details">
                  [[+text]] 
              </div>
      


      myShortTpl:
              <div class="details">
                  [[+text:ellipsis=`[[+len]]`]] 
                  <a class="more-info-link" data-remodal-target="[[+name:stripString=`,`:replace=` ==-`]]">More Info</a>
              </div>
              <div class="more-info">
                  <div class="remodal" data-remodal-id="[[+name:stripString=`,`:replace=` ==-`]]">
                      <button data-remodal-action="close" class="remodal-close"></button>
                      [[+text]]
                  </div>
              </div>
      






      [ed. note: Bruno17 last edited this post 7 years, 7 months ago.]
        -------------------------------

        you can buy me a beer, if you like MIGX

        http://webcmsolutions.de/migx.html

        Thanks!
        • 44165
        • 20 Posts
        Thank you so much Bruno! This snippet worked perfectly! Exactly what I was looking for. Enjoy the beer!

        In case anyone else is going to use this, note that in Bruno's call, myFullTpl has forward ticks, where it should be back ticks. Change
        &fullTpl = ´myFullTpl´
        to
        &fullTpl = `myFullTpl`


        I look forward to jumping into creating snippets now that your help has given me a much better idea on how to build them. Thanks again!