简体   繁体   English

如何在 TYPO3 流体模板中显示图像阵列中的随机图像?

[英]How to show a random image from image array in TYPO3 Fluid Template?

I have a field image with an array of image objects.我有一个带有图像对象数组的字段图像。

User can upload some pictures in this field.用户可以在此字段中上传一些图片。

In the fluid template it looks like:在流体模板中,它看起来像:

<f:if condition="{field.image}">
    <f:then>
        <f:for each="{field.image}" as="image" iteration="iterator">
            <f:image src="file:{image.properties.uid}" alt="" class="img-fluid" width="1250c" height="600c" />
        </f:for>
    </f:then>
</f:if>

In case there are three pictures uploaded, all will be displayed.如果上传了三张图片,则全部显示。 But I need to show only one random picture in this place.但我只需要在这个地方展示一张随机图片。 How can I do it?我该怎么做?

As mentioned in my comment you can create your own ViewHelper, anyway, the fast workaround is using jQuery just in code.正如我的评论中提到的,您可以创建自己的 ViewHelper,无论如何,快速的解决方法是在代码中使用 jQuery。 First, hide all images by default and then show one randomly.首先,默认隐藏所有图像,然后随机显示一个。

<f:if condition="{field.image}">
    <f:then>
        <f:for each="{field.image}" as="image" iteration="iterator">
            <f:image src="file:{image.properties.uid}" alt="" class="img-fluid random-image" style="display: none" width="1250c" height="600c" />
        </f:for>
        <script>

            $("img").not(":visible").each(function () {
                $(this).data("src", this.src);
                this.src = "";
            });

            shuffle($(".random-image")).slice(0, 1).each(function () {
                $(this).attr('src', $(this).data('src')).show();
            });

            // shuffle function thanks to Jon: 
            // https://stackoverflow.com/a/11186765/1066240
            function shuffle(array) {
                let m = array.length, t, i;
                while (m) {
                    i = Math.floor(Math.random() * m--);
                    t = array[m];
                    array[m] = array[i];
                    array[i] = t;
                }
                return array;
            }
        </script>
</f:if>

Note this approach should be considered as dirty-hack and can be done better ie for avoiding loading images which will not be shown ever (on this page refresh).请注意,这种方法应该被视为脏黑客,并且可以做得更好,即避免加载永远不会显示的图像(在此页面上刷新)。 also keep in mind that you can display ie 3 random images using slice(0, 3) , however, you need to make sure if you have at least 3 images.还请记住,您可以使用slice(0, 3)显示 3 张随机图像,但是,您需要确保至少有 3 张图像。

Thanks for comments and answers but this solution isn't good for my case because of cache.感谢您的评论和回答,但由于缓存,此解决方案不适合我的情况。

The jQuery solution is also not good for me because all pictures should be loaded first. jQuery 解决方案对我来说也不好,因为应该先加载所有图片。 There can be ie 10 background pictures with 200 kb and instead of only loading 200 kb, 2 mb were loaded to show only one picture.可以有 10 张 200 kb 的背景图片,而不是仅加载 200 kb,而是加载 2 mb 以仅显示一张图片。 There may also be a problem with the browser cache.浏览器缓存也可能有问题。

I have found another solution without own ViewHelper, jQuery and cache problem.我找到了另一个没有自己的 ViewHelper、jQuery 和缓存问题的解决方案。

Here is it:就这个:

<f:if condition="{field.image}">
    <f:then>
        {f:count(subject: field.image) -> f:variable(name: 'maximage')}
        {v:random.number(minimum: 1, maximum: maximage) -> f:variable(name: 'randomimage')}
        <f:for each="{field.image}" as="image" iteration="iterator">
            <f:if condition="{iterator.cycle} == {randomimage}">
                <f:image src="file:{image.properties.uid}" alt="" class="img-fluid" width="1250c" height="600c" />
            </f:if>
        </f:for>
    </f:then>
</f:if>

Explanation:解释:

{f:count(subject: field.image) -> f:variable(name: 'maximage')} – get the size of the image collection and put it in the new variable maximage . {f:count(subject: field.image) -> f:variable(name: 'maximage')} - 获取图像集合的大小并将其放入新变量maximage中。

{v:random.number(minimum: 1, maximum: maximage) -> f:variable(name: 'randomimage')} – get the random number between 1 and maximage and put it in the new variable randomimage . {v:random.number(minimum: 1, maximum: maximage) -> f:variable(name: 'randomimage')} - 获取 1 和 maximage 之间的随机数并将其放入新变量randomimage中。

<f:for each="{field.image}" as="image" iteration="iterator"> - start the normal loop for field.images <f:for each="{field.image}" as="image" iteration="iterator"> - 启动 field.images 的正常循环

<f:if condition="{iterator.cycle} == {randomimage}"> - condition for show the image – show it only if the current iterator {iterator.cycle} is equal to the {randomimage}. <f:if condition="{iterator.cycle} == {randomimage}"> - 显示图像的条件——仅当当前迭代器 {iterator.cycle} 等于 {randomimage} 时才显示它。

Have tested in Typo3 8.7.20.在 Typo3 8.7.20 中进行了测试。 Hope that will also work in Typo3 9.x and 10.x.希望这也适用于 Typo3 9.x 和 10.x。

Since you're already using VHS:由于您已经在使用 VHS:

Use v:iterator.random - https://viewhelpers.fluidtypo3.org/fluidtypo3/vhs/5.0.1/Iterator/Random.html使用v:iterator.random - https://viewhelpers.fluidtypo3.org/fluidtypo3/vhs/5.0.1/Iterator/Random.html

But make sure you are aware of the "result gets cached" pitfall described by @biesior above ( How to show a random image from image array in TYPO3 Fluid Template? ).但是请确保您知道上面@biesior 描述的“结果被缓存”的陷阱( 如何在 TYPO3 流体模板中显示来自图像数组的随机图像? )。

I made a solution with several benefits:我提出了一个有几个好处的解决方案:

  • works with cache与缓存一起使用
  • all images full responsive所有图像完全响应
  • load just one image只加载一张图片

I generate all tags but wrap them in HTML comment.我生成所有标签,但将它们包装在 HTML 注释中。 Browser don't load images.浏览器不加载图像。

<div class="stage-image has-content">
    <div class="stage-image-item">
        <!-- <img src="/storage/hochschule/upload/150902-4654.jpg" srcset="/storage/_processed_/3/e/csm_150902-4654_8eaf6a0880.jpg 764w,/storage/_processed_/3/e/csm_150902-4654_d0f788b245.jpg 1084w,/storage/hochschule/upload/150902-4654.jpg 1170w,"  sizes="(max-width: 767px) 764px, (min-width: 768px) and (max-width: 991px) 1084px, (min-width: 992px) 1170px"  alt="" > -->
    </div>
    <div class="stage-image-item">
        <!-- <img src="/storage/hochschule/upload/151022-0041.jpg" srcset="/storage/_processed_/f/e/csm_151022-0041_d0b564fdb1.jpg 764w,/storage/_processed_/f/e/csm_151022-0041_b993bf6066.jpg 1084w,/storage/hochschule/upload/151022-0041.jpg 1170w,"  sizes="(max-width: 767px) 764px, (min-width: 768px) and (max-width: 991px) 1084px, (min-width: 992px) 1170px"  alt="" > -->
    </div>
    <div class="stage-image-item">
        <!-- <img src="/storage/hochschule/upload/151013-7145.jpg" srcset="/storage/_processed_/e/6/csm_151013-7145_42e2692215.jpg 764w,/storage/_processed_/e/6/csm_151013-7145_3ee771dee1.jpg 1084w,/storage/hochschule/upload/151013-7145.jpg 1170w,"  sizes="(max-width: 767px) 764px, (min-width: 768px) and (max-width: 991px) 1084px, (min-width: 992px) 1170px"  alt="" > -->
    </div>
    <div class="stage-image-item">
        <!-- <img src="/storage/hochschule/upload/151104-1433.jpg" srcset="/storage/_processed_/f/b/csm_151104-1433_7bf357e27a.jpg 764w,/storage/_processed_/f/b/csm_151104-1433_b7c9f24147.jpg 1084w,/storage/hochschule/upload/151104-1433.jpg 1170w,"  sizes="(max-width: 767px) 764px, (min-width: 768px) and (max-width: 991px) 1084px, (min-width: 992px) 1170px"  alt="" > -->
    </div>
    <div class="stage-image-item">
        <!-- <img src="/storage/hochschule/upload/150930-6077.jpg" srcset="/storage/_processed_/a/e/csm_150930-6077_d7a31d91e3.jpg 764w,/storage/_processed_/a/e/csm_150930-6077_87603957bf.jpg 1084w,/storage/hochschule/upload/150930-6077.jpg 1170w,"  sizes="(max-width: 767px) 764px, (min-width: 768px) and (max-width: 991px) 1084px, (min-width: 992px) 1170px"  alt="" > -->
    </div>
</div>

Then remove randomly HTML comment from one and browser loads them.然后从一个中随机删除 HTML 评论,浏览器加载它们。

(function ($) {
    $.fn.rand = function () {
        return this.eq(Math.floor(Math.random() * this.length));
    };
})(jQuery);


// Get random image
var stageImageDiv = $(this).find('.stage-image');
$(stageImageDiv).children().rand().replaceWith(function () {
    var string = $(this).html();
    return string.replace(/<!--/g, '').replace(/-->/g, '');
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM