-->

Render CONTENT in PHP with TypoScript

2019-05-18 14:17发布

问题:

I need to build a Typoscript file in PHP and then pretty much execute the script to get the content of a given page. This is my Code:

if (!defined('PATH_typo3conf')) die ('Could not access this script directly!');

class ContentHandler extends tslib_cObj
{
    var $conf;

    public function __construct()
    {
        tslib_eidtools::connectDB();
    }

    public function main()
    {
        $this->createConf(16);

        $code = $this->cObjGet($this->conf);

        echo "<pre>";
        print_r($this->conf);
        echo "</pre>";

        echo "<pre>";
        echo "code: " . $code;
        echo "</pre>";
    }

    protected function createConf($pid)
    {
        $this->conf = array(
            'foo' => 'CONTENT',
            'foo.' => array(
                'table' => 'tt_content',
                'select.' => array(
                    'pidInList' => $pid,
                    'languageField' => 0,
                ),
                'renderObj' => 'COA',
                'renderObj.' => array(
                    'stdWrap.' => array(
                        'wrap' => '<b>|</b>',
                    ),
                    '10' => 'TEXT',
                    '10.' => array(
                        'field' => 'header',
                        'wrap' => '<h3>|</h3>',
                    ),
                    '20' => 'TEXT',
                    '20.' => array(
                        'field' => 'bodytext',
                        'wrap' => '<b>|</b>',
                    ),
                ),
            )
        );
    }
}

I believe the typosript is built well, but I don't get anything back. I check with a simple mysql_query() and it returns content. But I need to do this with typoscript.

Any help appreciated!

Edit: This not an actual extension script, but it's inside the EXT/ folder.

回答1:

Your TypoScript is not correct. Have a look at the API (http://api.typo3.org/typo3v4/current/html/classtslib__c_obj.html#ab70d69a447f24f7416a85e7de1cb4ffb). Instead of "foo" you have to define an numerical key "10".

$this->conf = array(
            'foo' => 'CONTENT',
            'foo.' => array(

should do the job.

Btw.: You need tslib_eidtools::connectDB(); only, if you are using an eID Script.