0

我正在 CMS Made simple 中构建一个模板,它使用Smarty engine. 我是一个完全聪明/php 的新手。

我正在遍历文件夹中的图像并将它们显示在Bootstrap Carusel. 那工作正常。

另一方面,我必须为每个图像显示文本和标题。

我想出了一个解决方案,在.txt文件夹中放置一个包含每个图像的标题和文本内容的文件。例如,我有一个在同一个文件夹中img1.jpg调用img1.txt的文件(img2.jpg- img2.txtimg3.jpg-img3.txt等)。每个图像都有一个相应的.txt文件,其中包含必须读取到下面代码中的标题和文本部分的内容)

我不确定如何实施该解决方案{foreach}并需要帮助。

我一直在使用下面的这段代码来访问,.txt 但使用*.txt不起作用。*.txt给出一个空数组。

更新

    {"{uploads_url}/images/{$entry->Picfolder}/*.txt"|file_get_contents|parse_str:$result|glob}

   {foreach from=$result key=text item=foo}
         <p>{$text}</p>
   {/foreach} 

这是整个轮播代码:

<!--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} <!--finding all .jpgs in the folder -->
  {foreach from=$pics item='pic'}<!-- loop through .jpgs --> 
     {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"> <!-- add jpgs from loop -->
         <div class="carousel-caption d-md-block">
           <h5>Caption</h5><!-- need to insert content from .txt file here -->
           <p>Image text</p><!-- need to insert content from .txt file here -->
         </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-->

有人可以帮我吗?我真的被这个问题困住了。提前致谢..

4

1 回答 1

0

我真的很鼓励你在 PHP 级别处理这个问题。也许您可以准备一个 [ image_path, caption] 元组数组并将其分配给 Smarty。Smarty 本身就是一个模板引擎;除了琐碎的循环之外,它不应该处理 glob、文件名操作或其他代码逻辑。

如果您仍想在 Smarty 级别执行此操作,则可以构造文件名,然后打印其内容:

<p>
  {capture "fileName"}/full/path/to/{$pic|basename:'.jpg'}.txt{/capture}
  {$smarty.capture.fileName|file_get_contents}
</p>

这是做什么的:

  1. 从修剪.jpg扩展名$pic
  2. 添加完整路径和.txt扩展名并将其捕获为 Smarty 变量$fileName
  3. 打印 的内容$fileName
于 2019-10-14T08:08:57.087 回答