4

我正在使用 Typo3 4.5.3,并且我在一个目录中有图像文件,我想从中随机选择一个显示在当前页面上,但我似乎无法正确地使用打字稿来使 listnum = rand 工作。这是我的扩展模板:

# Pick a random image to display
temp.banner = IMAGE
temp.banner {
  file {
    height = 165
    width = 954
    import {
      filelist = {$templatePathPrefix}images/banners | jpg,jpeg,png,gif | name | | 1
      listNum = rand
    }
  }
  params = class="bannerPic"
}

如果我将 listnum 设置更改为 0、1 等,它会将相应的 img HTML 插入页面。将其设置为“最后一个”也可以,但无论我刷新多少次,rand总是插入第一张图像。因为我可以选择特定的图像(0、1、...),所以我知道一般设置是有效的,而不是随机选择。

我已经看到 ts 用于 rand 的各种其他用途,它将外部对象包装在 COA_INT 对象中,但这对我也不起作用。我是否错过了有关将 listNum = rand 放置在何处的内容?我是 Typo3 的新手,所以在这一点上,很多对我来说仍然很不透明(或者它是否显示?)。感谢您提供的任何见解。

4

3 回答 3

2

mak_stdwrapextended扩展增加了将 rand 与 listNum 一起使用的可能性。

在 4.5 上运行良好。

于 2012-02-22T10:37:47.243 回答
1

我不建议您通过 TypoScript 进行随机选择。由于每次加载页面时都需要随机图像,因此随机元素必须是 USER_INT 或 COA_INT 元素,因此不可缓存。

一个简单的解决方案是通过 JavaScript 来完成。如果 JavaScript 在客户端上不可用,您定义一个默认图像,以及一个随机选择图像的 JavaScript。使用此解决方案,您每次都会获得一张随机图像,并且您的内容是完全可缓存的。

下面的 TypoScript 代码可以给你一些启发。它基本上从边框列中读取图像元素并生成 JavaScript 以随机输出它们。此外,图像是可链接的。

lib.teaser = COA
lib.teaser.10 = CONTENT
lib.teaser.10 < styles.content.getBorder
lib.teaser.10 {
    slide = -1
    table=tt_content
    select{
        begin = 0
        max = 1
        #language
        languageField=sys_language_uid
        #from wich column
        where=colPos=3
    }
    wrap=<div class="teaserimage">|</div>

    renderObj=COA

    #image with gallery function
    renderObj.10 = COA
    renderObj.10 {
        stdWrap.required=1
        # get image
        10 = IMAGE
        10 {
            #if not empty
            required=1
            file.import=uploads/pics/
            file.import.field=image
            #file.width=266
            #file.height=180
            file.import.listNum = 0 
            stdWrap.insertData=1
            params = id="imgbig_{TSFE:currentRecord}"
            imageLinkWrap < tt_content.image.20.1.imageLinkWrap
            imageLinkWrap.typolink.ATagParams = id="link_imgbig_{TSFE:currentRecord}"
            imageLinkWrap.typolink.ATagParams.stdWrap.insertData=1
        }

        # standard image configuration from tt_content
        10.altText < tt_content.image.20.1.altText
        10.titleText < tt_content.image.20.1.titleText
        10.longdescURL < tt_content.image.20.1.longdescURL

        # random function for gallery images
        30 = COA
        30 {
            stdWrap.required=1
            stdWrap.dataWrap(
            <script type="text/javascript">
            /* <![CDATA[ */
            var imgArray = new Array(|);
            var randnum = Math.round(Math.random()*(imgArray.length-1));
            document.getElementById('imgbig_{TSFE:currentRecord}').src ='uploads/pics/' + imgArray[randnum];
            /* ]]> */
            </script>
            )

            # first gallery image
            10 = TEXT
            10.field = image
            10.listNum.splitChar=,
            10.listNum=0
            10.if.isTrue.field=image
            10.if.isTrue.listNum=1
            10.if.isTrue.listNum.splitChar=,
            10.dataWrap = "|"


            # other gallery images  
            20 = TEXT
            20.field = image
            20.split {
                token = ,
                cObjNum = 1
                1 = COA
                1.if.isPositive.data = TSFE:register|SPLIT_COUNT
                1 {
                    10 = TEXT
                    10.data = current:1
                    10.dataWrap = ,"|"
                }
            }
        }
        # random function for gallery links
        40 = COA
        40 {
            stdWrap.required=1
            stdWrap.dataWrap(
            <script type="text/javascript">
            /* <![CDATA[ */
            //var imgLinkArray = new Array(randnum);
            var imgLinkArray = new Array(|);
            if(document.getElementById('link_imgbig_{TSFE:currentRecord}')) document.getElementById('link_imgbig_{TSFE:currentRecord}').href = imgLinkArray[randnum];
            /* ]]> */
            </script>
            )

            # first gallery link            
            10 = TEXT
            10.field = image_link
            10.listNum.splitChar=,
            10.listNum=0
            10.dataWrap = "|"
            10.typolink.parameter.field = image_link
            10.typolink.returnLast = url

            # other gallery links   
            20 = TEXT
            20.field = image_link
            20.split {
                token = ,
                cObjNum = 1
                1 = COA
                1.if.isPositive.data = TSFE:register|SPLIT_COUNT
                1 {
                    10 = TEXT
                    10.data = current:1
                    10.dataWrap = ,"|"
                    10.typolink.parameter.data = current:1
                    10.typolink.returnLast = url
                }
            }
        }
    }
}
于 2012-02-23T12:09:38.303 回答
1

只有typo3 V4.6及更高版本的核心有listNum = rand,见这个http://forge.typo3.org/issues/16180

对于typo3 V4.5,您必须扩展 tslib/class.tslib_content.php

将此添加到 localconf.php:

$TYPO3_CONF_VARS['FE']['XCLASS']['tslib/class.tslib_content.php'] = PATH_site.'fileadmin/template/class.ux_tslib_content.php';

并将此代码添加到 class.ux_tslib_content.php:

<?php
/***************************************************************
*  Copyright notice
*
*  (c) 1999-2008 Kasper Skaarhoj (kasperYYYY@typo3.com)
*  All rights reserved
*
*  This script is part of the TYPO3 project. The TYPO3 project is
*  free software; you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation; either version 2 of the License, or
*  (at your option) any later version.
*
*  The GNU General Public License can be found at
*  http://www.gnu.org/copyleft/gpl.html.
*  A copy is found in the textfile GPL.txt and important notices to the license
*  from the author is found in LICENSE.txt distributed with these scripts.
*
*
*  This script is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*  GNU General Public License for more details.
*
*  This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
/**
 * Contains classes for Content Rendering based on TypoScript Template configuration
 *
 * $Id: class.tslib_content.php 4254 2008-09-27 13:35:44Z dmitry $
 * Revised for TYPO3 3.6 June/2003 by Kasper Skaarhoj
 * XHTML compliant
 *
 * class tslib_cObj         :       All main TypoScript features, rendering of content objects (cObjects). This class is the backbone of TypoScript Template rendering.
 *
 * @author  Kasper Skaarhoj <kasperYYYY@typo3.com>
 */

/**
 * This class contains all main TypoScript features.
 * This includes the rendering of TypoScript content objects (cObjects).
 * Is the backbone of TypoScript Template rendering.
 *
 * There are lots of functions you can use from your include-scripts.
 * The class "tslib_cObj" is normally instantiated and referred to as "cObj".
 * When you call your own PHP-code typically through a USER or USER_INT cObject then it is this class that instantiates the object and calls the main method. Before it does so it will set (if you are using classes) a reference to itself in the internal variable "cObj" of the object. Thus you can access all functions and data from this class by $this->cObj->... from within you classes written to be USER or USER_INT content objects.
 *
 * @author  Kasper Skaarhoj <kasperYYYY@typo3.com>
 * @package TYPO3
 * @subpackage tslib
 * @link http://typo3.org/doc.0.html?&tx_extrepmgm_pi1[extUid]=270&cHash=4ad9d7acb4
 */
class ux_tslib_cObj extends tslib_cObj{


    /**
     * Exploding a string by the $char value (if integer its an ASCII value) and returning index $listNum
     *
     * @param   string      String to explode
     * @param   string      Index-number. You can place the word "last" in it and it will be substituted with the pointer to the last value. You can use math operators like "+-/*" (passed to calc())
     * @param   string      Either a string used to explode the content string or an integer value which will then be changed into a character, eg. "10" for a linebreak char.
     * @return  string
     */
    function listNum($content,$listNum,$char) {
        $char = $char ? $char : ',';
        if (t3lib_div::testInt($char))  {
            $char = chr($char);
        }
        $temp = explode($char,$content);
        $last = ''.(count($temp)-1);
        if($listNum === 'rand'){ $listNum = rand(0,count($temp)-1);} //taywa added: rand feature!
        $index=$this->calc(str_ireplace('last',$last,$listNum));
        return $temp[$index];
    }

}


?>
于 2013-05-14T08:52:00.067 回答