如何在PHP Laravel Twig中按类别过滤记录?

时间:2017-02-09 12:58:23

标签: php laravel twig octobercms

我正在使用基于OctoberCMSLaravelTwig制作一个简单的图库。

如何按类别过滤记录?

以下是我现在的做法,但我不认为这是一个很好的解决方案:

我过滤了html,但该类别之外的所有项目仍然存在完整记录,导致额外的分页和空白页面。

数据库

  • 表'图库'包含类别名称列表。

  • 表'images'包含图像名称及其标记类别。

图片列表

网址参数是/ gallery /:category?/:page?

访问像/ gallery / nature / 1这样的网址会使用for循环按类别过滤图片。

<!-- Gallery Category Record Variable -->
{% set category = record.category %}

<!-- Image List -->
{% for record in records %}

    <!-- If Image Category Tag matches Gallery Category Name -->
    {% if record.category_tag == category %} 
        <img src="/{{ record.name }}.jpg">
    {% endif %}

{% endfor %}

记录

“记录”和“记录”的结果。

for循环在'images→category_tag'记录中显示'gallery→category'记录。

分页显示所有'图像→名称'记录。

{% for record in records %} = (All in 'images')
{{ records }} = {<ul><li>...<a href="gallery/nature?page=2">2</a>} (All in 'images')
{{ record }} = {"category":"nature","id":7} (Current category in 'gallery')

解决方案吗

有没有办法在生成html列表之前按类别过滤'记录'?

1 个答案:

答案 0 :(得分:2)

这就是我接近它的方式:

  • 在图像模型中创建一个按页面,类别等过滤的范围。
  • 处理组件中的URL属性和逻辑而不是视图

让我们说画廊组件:

网址:page.com/:cat /:page?

<ToggleSwitch x:Name="OpenHamWhenStart"
                                  Header="Default Hamburger Open or Close"
                                  IsOn="{Binding IsHamOpen, Mode=TwoWay}"
                                  OffContent="Hamburger Menu is Closed"
                                  OnContent="Hamburger Menu is Open"
                                  RelativePanel.AlignLeftWithPanel="True"
                                  RelativePanel.Below="BusyTextTextBox" />

在您的图片模型中:

    public function defineProperties()
    {
        return [
            // Page # for pagination..
            'pageNumber' => [
                'title'       => 'Page #',
                'description' => 'Gallery Page #',
                'type'        => 'string',
                'default'     => '{{ :page }}',
            ],
            // Category slug
            'category' => [
                'title'       => 'Category',
                'description' => 'Gallery Cat',
                'type'        => 'string',
                'default'     => '{{ :cat }}',
            ],
            // Images to show per page
            'perPage' => [
                'title'             => 'Images per page',
                'type'              => 'string',
                'validationPattern' => '^[0-9]+$', //  validation
                'validationMessage' =>  'VValidation Error',
                'default'           => '15', 
            ],
            // if you want to add sorting
            'sortOrder' => [
                'title'       => 'Sort Order',
                'description' => 'Images Sort Order',
                'type'        => 'dropdown',
                'default'     => 'updated_at desc' 
            ],
        ];
    }

public function getSortOrderOptions()
{
    return Image::$allowedSortingOptions;
}


public function init()
{
    $this->pageNumber   =  empty($this->property('pageNumber')) ? 1 :   $this->property('pageNumber');
    $this->perPage      =  $this->property('perPage');
    $this->sortOrder    =  $this->property('sortOrder');
    $this->category     =  $this->property('category');
}

public function onRun()
{

  // here you may want to do some checks 
 // and add logic before querying your DB

    return $this->listImages($this->pageNumber , $this->sortOrder,  $this->perPage, $this->category);

}

public function listImages($pageNumber, $sortOrder, $perPage, $category){

  // this is the scope you will define in your Images Model
 // to handle pagination, sorting, category filtering ect..

    $images = Images::listFrontEnd([
        'page'          => $pageNumber,
        'sort'          => $sortOrder,
        'perPage'       => $perPage,
        'category'      => $category,
    ]);



   // small helper if the pagination # is > than last page
  // redirect to last page..

    $lastPage = $images->lastPage();

    if ($this->pageNumber > $lastPage && $this->pageNumber > 1){
            return Redirect::to($this->currentPageUrl(["page" =>  $lastPage]));
        }


 $this->images  = $this->page['images'] = $images;

}

来自RainLab Blog Plugin

的答案