Page 1 of 1

Advice on how to read contents of .txt file to template

Posted: Thu Oct 10, 2019 11:05 am
by Sendlingur
Hi

I'm looping through an image folder and displaying the images in a bootstrap carousel in the news detail template.
That is working very well

My problem is that there should be a caption and a text for each image.

My solution is to put a .txt file which contains caption contents for each image in the folder. So I have img1.jpg then there is a file called img1.txt in the same folder etc. (each image has a corresponding .txt file which contains contents that have to be read to the caption section in the code below)

I'm not sure how I would implement that solution to the {foreach} loop.

Can someone advise me about best practice for that solution.
I'm still not that advanced in CMSMS

Below is a copy of the Carousel code for clarification.

Code: Select all


<!--Carousel Wrapper-->
<div id="carousel-thumb" class="carousel slide carousel-fade carousel-thumbnails" data-ride="carousel">
<!--Slides-->
<div class="carousel-inner" role="listbox">
 {assign var='pics' value="uploads/images/{$entry->Picfolder}/*.jpg"|glob}
  {foreach from=$pics item='pic'}
     {if $pic@first}
       <div class="carousel-item active">
       {else}
       <div class="carousel-item">
     {/if}            
       <img class="d-block w-100" src='{root_url}/{$pic}' alt="First slide">  
         <div class="carousel-caption d-md-block">
           <h5>Caption</h5>
           <p>Image text</p>
         </div>    
       </div>
  {/foreach}
       </div>
          <!--/.Slides-->
          <!--Controls-->
          <a class="carousel-control-prev" href="#carousel-thumb" role="button" data-slide="prev">
            <span class="carousel-control-prev-icon" aria-hidden="true"></span>
            <span class="sr-only">Previous</span>
          </a>
          <a class="carousel-control-next" href="#carousel-thumb" role="button" data-slide="next">
            <span class="carousel-control-next-icon" aria-hidden="true"></span>
            <span class="sr-only">Next</span>
          </a>
          <!--/.Controls-->
          <ol class="carousel-indicators">
            {foreach from=$pics item='pic' name=img}
              <li data-target="#carousel-thumb" data-slide-to="{$smarty.foreach.img.index}" class="active"> <img class="d-block w-100" src='{root_url}/{$pic}' height="50" width="50" class="img-fluid"></li>
            {/foreach}
          </ol>
</div>
<!--/.Carousel Wrapper-->



Re: Advice on how to read contents of .txt file to template

Posted: Thu Oct 10, 2019 11:28 am
by Jos
Seems to me you are rebuilding all the functionality the Gallery module has to offer :-X ;)

Re: Advice on how to read contents of .txt file to template

Posted: Thu Oct 10, 2019 11:31 am
by Sendlingur
Jos wrote:Seems to me you are rebuilding all the functionality the Gallery module has to offer :-X ;)

Haha... I quess it is for the purpose of learning :)

Re: Advice on how to read contents of .txt file to template

Posted: Thu Oct 10, 2019 4:43 pm
by Sendlingur
I've been trying to make this work to get the .txt file contents, but without luck.

Code: Select all

{uploads_url}/images/{$entry->Picfolder}/*.txt"|file_get_contents|parse_str:$result}
I'm still pretty stuck here :)

Re: Advice on how to read contents of .txt file to template

Posted: Mon Oct 14, 2019 2:02 pm
by johnboyuk1
Doesn't strictly answer your question ... but I'd use LISE to build a module to do this maybe? So in your LISE module have 2 fields - the image path and the image caption and then build out from there. Means when you want to update the options or images you don't have to mess about with txt files...

Re: Advice on how to read contents of .txt file to template

Posted: Mon Oct 14, 2019 7:14 pm
by Rolf
Maybe you can use this tag, or use some working code from it:
http://dev.cmsmadesimple.org/projects/cacheremotefile

Demo and tutorial:
https://cmscanbesimple.org/blog/share-d ... e-file-tag

Code: Select all

{cache_remote_file url='https://yoursite.com/uploads/file.txt'}

Re: Advice on how to read contents of .txt file to template

Posted: Wed Oct 16, 2019 11:02 am
by Sendlingur
Thanks all, I decided to just go with the {gallery} for this. I'll try your solutions on future projects.