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

For questions and problems with the CMS core. This board is NOT for any 3rd party modules, addons, PHP scripts or anything NOT distributed with the CMS made simple package itself.
Post Reply
Sendlingur
Forum Members
Forum Members
Posts: 60
Joined: Wed Aug 08, 2018 4:59 pm

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

Post 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-->


Jos
Support Guru
Support Guru
Posts: 4016
Joined: Wed Sep 05, 2007 8:03 pm
Location: The Netherlands

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

Post by Jos »

Seems to me you are rebuilding all the functionality the Gallery module has to offer :-X ;)
Sendlingur
Forum Members
Forum Members
Posts: 60
Joined: Wed Aug 08, 2018 4:59 pm

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

Post 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 :)
Sendlingur
Forum Members
Forum Members
Posts: 60
Joined: Wed Aug 08, 2018 4:59 pm

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

Post 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 :)
johnboyuk1
Forum Members
Forum Members
Posts: 107
Joined: Mon Nov 26, 2018 3:09 pm

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

Post 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...
User avatar
Rolf
Dev Team Member
Dev Team Member
Posts: 7754
Joined: Wed Apr 23, 2008 7:53 am
Location: The Netherlands
Contact:

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

Post 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'}
Image
- + - + - + - + - + -
Latest CMSMS tutorial: FormBuilder WatchGuard
- + - + - + - + - + -
Did my post help you solving a problem at your (customers) website and it saved you many hours of work?
Great!! Buy me a cup of coffee in return as a small token of appreciation!
Sendlingur
Forum Members
Forum Members
Posts: 60
Joined: Wed Aug 08, 2018 4:59 pm

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

Post by Sendlingur »

Thanks all, I decided to just go with the {gallery} for this. I'll try your solutions on future projects.
Post Reply

Return to “CMSMS Core”