1

ポートフォリオ ページ (PortfolioPage.ss) のすべての孫ページ (GalleryPage.ss) から特定の画像 ($FeaturedImage) を取得し、それらをランダムな順序で表示しようとしています。

テンプレートを使用して画像を簡単に取得できます。PortfolioPage.ss

<% loop Children %>
 <% loop Children %>
   <% loop FeaturedImage %>
      <img src="$Url"></>
   <% end_loop %>
 <% end_loop %>
<% end_loop %>

ただし、これにより、メニューのページ順に表示されます。

いくつかの調査の後、ページコントローラーで関数を作成するのが最善のようですが、これを書く方法がわかりません..(これらに関するドキュメント/チュートリアルへのリンクがある人も素晴らしいでしょう)。

これまでに見つかった同様のコードの例: get Dataobjects from Children - SilverStripe 3.1 http://www.silverstripe.org/template-questions/show/23296

シルバーストライプのドキュメント: http://doc.silverstripe.org/framework/en/topics/datamodel

これを私のコードに適用する方法がわかりません..ありがとう

4

2 に答える 2

3

基本的に、Portfolio Page Controller (またはこのロジックが必要なページ) に関数を作成する必要があります。

ここに2つの例があります。まずFeaturedImage、データベースから既存のものをすべて取得し、ランダムな順序で返します。

function AllFeaturedImages()
{
    return FeaturedImage::get()->sort('RAND()');
}

そして、これFeaturedImageはページの子の子からすべてを取得し、それらをランダムな順序で返します:

function DescendantFeaturedImages()
{
    $featuredImages = array();

    foreach ($this->Children() as $child)
    {
        foreach ($child->Children() as $grandChild)
        {
            $images = $grandChild->FeaturedImage();
            if ( $images )
            {
                $featuredImages = array_merge( $featuredImages, $images->toArray() );
            }
        }
    }

    shuffle($featuredImages);

    return ArrayList::create($featuredImages);
}

FeaturedImageリレーションが単なる の場合has_one、これは少し変わります:

function DescendantFeaturedImages()
{
    $featuredImages = array();

    foreach ($this->Children() as $child)
    {
        foreach ($child->Children() as $grandChild)
        {
            $image = $grandChild->FeaturedImage();
            if ( $image )
            {
                array_push( $featuredImages, $image );
            }
        }
    }

    shuffle($featuredImages);

    return ArrayList::create($featuredImages);
}

FeaturedImage次に、Portfolio Page テンプレートで、関数名を呼び出してループすることができます。ここでは、 または のいずれ$AllFeaturedImages$DescendantFeaturedImagesです。あなたの場合、次のようなものが得られます:

<% loop $DescendantFeaturedImages %>
    <img src="$URL"/>
<% end_loop %>

コントローラー関数を使用した SilverStirpe チュートリアルで 1 つの例を見つけることができました: http://doc.silverstripe.org/framework/en/tutorials/2-extending-a-basic-site

これがどうなるか教えてください。

于 2013-12-12T08:04:17.237 に答える
2

Colymba のコードを試してみたところ、チャンピオンのように機能しました。以下で説明する方法よりも、彼のコードに従うことをお勧めします。

コメントで言うように、テンプレートから祖父母の画像にアクセスできます。JavaScript を使用するか、この例の jQuery のように、画像をランダムに並べ替えることができます。

(function($){

    $.fn.shuffle = function() {

        var allElems = this.get(),
            getRandom = function(max) {
                return Math.floor(Math.random() * max);
            },
            shuffled = $.map(allElems, function(){
                var random = getRandom(allElems.length),
                    randEl = $(allElems[random]).clone(true)[0];
                allElems.splice(random, 1);
                return randEl;
           });

        this.each(function(i){
            $(this).replaceWith($(shuffled[i]));
        });

        return $(shuffled);

    };

})(jQuery);

次に、ランダムに並べ替えたい要素に対して関数を呼び出します。

$('#imgholder img').shuffle();

より完全な説明はcss-tricks.comにあります。

于 2013-12-13T09:20:42.337 に答える