All You Need To Know About TypoScript Conditions With Symfony Expression Language

All You Need To Know About TypoScript Conditions With Symfony Expression Language

Did you know, TYPO3 v9.5 LTS the Symfony expression language was introduced in TypoScript conditions? Yeah!! TypoScript is the kind heart of TYPO3 which is getting more powerful by add-ons of the most popular Symfony expression.

Here I would like to show, How old TypoScript conditions are migrated to brand new Symfony expression language?

Welcome! Contribute to TYPO3 by becoming my Patreon

Just a moment of attention, please.

As I love TYPO3 I would like to furnish TYPO3 people with informative content, tutorials, and experiences by composing regular TYPO3 blogs “as to give back to the community”.

To hold adding to our extraordinary OpenSource TYPO3 projects and network, I'm looking my Patreon  https://www.patreon.com/sanjay_nitsan

Inspiring People To Donate :)

When two great OpenSource meet!

TYPO3 + Symfony collaborate together, Cool, huh?

Symfony is a set of reusable PHP components... and PHP is a framework for web projects~ https://symfony.com/

That’s the beauty of Open Source projects and community! TYPO3 is once again proof, It’s kind CMS which works well and can be integrated with other Open Source projects.

The TYPO3 core is working hard to use Symfony components to keep improve the base of TYPO3 CMS solution.

I think, you’ve already heard some of the terminologies like YAML, Routing, Console, etc., which comes from Symfony Framework.

You may be surprised, How many Symfony components are used into TYPO3 CMS? Here is the list:

How current TypoScript conditions?

New TYPO3 learners are sometimes a bit afraid and feel it difficult to deal with TypoScript. But, once you understand the beauty of TypoScript then you will definitely love it.

TypoScript is not a scripting language, It serves more as a kind of API (interface) to configure the underlying PHP code.

So, here is the syntax about currently  how TypoScript conditions are used:

// TypoScript Code
[condition 1] [condition 2]
// True, Write your code here
[end]

Using TypoScript conditions, you will have the flexibility to control your TYPO3 templates, layouts, content elements, etc.,

Introducing brand new Symfony Expression Language

The Expression Language provides an engine that compiles and evaluates expressions. An expression is a one-liner that returns a value. Most of the time its value is Boolean type, either true or false.

How does it look inside Symfony?

$symfonyExpressionLanguage>evaluate('1 < 2');
// return true

$symfonyExpressionLanguage>evaluate('not ("Search" matches "/Replace/")'); 
// return true

$symfonyExpressionLanguage>evaluate('11 in 1..10');
// return true

$symfonyExpressionLanguage>evaluate('calendar.getWeekday() == "Monday"'));
// return true on monday, otherwise wrong

You may check out other detailed examples and documentation at https://symfony.com/doc/current/components/expression_language/syntax.html

New TypoScript Conditions from TYPO3 9 LTS

The Symfony Expression Language has been introduced from TYPO3 v9.4.

The old syntax is considered as absolute and will be deprecated from v10.

So, it is highly recommended to migrate to Symfony expression language for your new and upgrading projects to make sure everything works well in future :)

Variables

Even before the TYPO3 9 release, TypoScript conditions were used with Symfony Expression Language. Here are the samples:

applicationContextReturns the information about current environment eg., production or development.
pageReturns all the properties of the current page
{$theConstant}Returns access of the TypoScript constant
frontend.user.isLoggedInReturns true, if frontend user is logged-in
typo3.versionReturns the version of information of TYPO3

Functions

In order to replace all previous TypoScript conditions, TYPO3 also provides functions in addition to the above variables.

date()Returns current date
like("string", "*chat")Returns if the string char is found in a string.
getTSFE()Returns an access to the TypoScript Frontend Controller $GLOBALS[‘TSFE’];
request.getQueryParams()Returns all GET Params.
site()Returns the properties from the Site management module.

Check this release notes to Implement symfony expression language for TypoScript conditions.

Expressions

Now, Let’s compare between previous TypoScript conditions and Symfony Expression Language.

Site language

// Until TYPO3 v9
[globalVar = GP:L = 1]

// as of TYPO3 9.4 - the L-parameter is omitted, therefore verification of the site configuration

[siteLanguage("languageId") == "1"]

// Next to the Id, you can also query other properties

[siteLanguage("title") == "English"]

Get parameter

// Until TYPO3 v9.3

[globalVar = GP:tx_myext_myplugin|bla > 0]

// from TYPO3 v9.4 (GET parameter or POST parameter)

[(request.getQueryParams()['tx_myext_myplugin'])['bla'] > 0 || 

(request.getParsedBody()['tx_myext_myplugin'])['bla'] > 0]

Layer in the page tree

// Until TYPO3 v9.3
[treeLevel = 0,2] 

// From TYPO3 v9.4 
[tree.level in [0,2]]

Page in root line

// Until TYPO3 v9.3 

[PIDinRootline = 5,10] 

// From TYPO3 v9.4 

[5 in tree.rootLineIds || 10 in tree.rootLineIds]

Page properties

// Until TYPO3 v9.3
[page|backend_layout = 1]

// From TYPO3 v9.4

[page["backend_layout"] == '1']

Domain

// Until TYPO3 v9.3
[globalString = IENV:HTTP_HOST = *.mydomain.com]

// From TYPO3 v9.4

[like(request.getNormalizedParams().getHttpHost(), '*.mydomain.com')]

Constants

// Until TYPO3 v9.3
[globalVar = LIT:1 = {$meineTypoScriptKonstante}]

// From TYPO3 v9.4

[{$meineTypoScriptKonstante} == '1']

User logged in the backend

// Until TYPO3 v9.3
[globalVar = TSFE:beUserLogin > 0]

// From TYPO3 v9.4
[getTSFE().beUserLogin]

Is it Friday today?

// Until TYPO3 v9.3
[dayofweek = 5]

// From TYPO3 v9.4
[date("w") == 5]

More Examples

You know what, the possibilities of the conditions are hardly limited. You may read “References” which includes additional links at the end of this blog post.

Do you want TypoScript Conditions with Expression Language?

Until date, to call your own user functions, we call it via [userFun].

[userFunc = \Vendor\Extension\UserFunc\MyUserFunc('foo')]

The ExpressionLanguage can also be extending by registering news methods.

Step 1: Create Configuration/ExpressionLanguage.php in your extension.

Step 2: Register your provider by returning an array of your custom ExpressionLanguage.php.

<?php
return [
    'tx_myext_provider' => [
        \Vendor\Extension\ExpressionLanguage\MyConditionProvider::class,
    ]
];

Step 3: Create your custom class to write your code

Take a closer look to TypoScript condition provider ExpressionLanguage from TYPO3 core as below:

class TypoScriptConditionProvider extends AbstractProvider
	{
	    public function __construct()
	    {
	        $typo3 = new \stdClass();
	        $typo3->version = TYPO3_version;
	        $typo3->branch = TYPO3_branch;
	        $typo3->devIpMask = trim($GLOBALS['TYPO3_CONF_VARS']['SYS']['devIPmask']);
	        $this->expressionLanguageVariables = [
	            'request' => GeneralUtility::makeInstance(RequestWrapper::class, $GLOBALS['TYPO3_REQUEST'] ?? null),
             'applicationContext' => (string)GeneralUtility::getApplicationContext(),
            'typo3' => $typo3,
        ];
        $this->expressionLanguageProviders = [
            Typo3ConditionFunctionsProvider::class
        ];
    }
}

References

Conclusion

  • Keep best practice with using brand new TypoScript conditions using Symfony Expression Language because old will be deprecated from TYPO3 v10.
  • Keep in mind to migrate old TypoScript conditions to new which upgrading your projects.
  • Keep reading documentation to be aware of all new TypoScript conditions.
  • Keep feeling the power and beauty of OpenSource projects and community = TYPO3 + Symfony.

Thank you very much for reading! Feel free to write any suggestion or feedback at below comment box.

You may become my Patreon at https://www.patreon.com/sanjay_nitsan. I’ll be happy to keep motivated to write cooler TYPO3 blogs :)

Inspiring People To Collaborate! (TYPO3 + Symfony).

Sanjay Chauhan - TYPO3 Technopreneur

Ein junger Technologie-Enthusiast und Unternehmer. Mit 24 Jahren war er Mitbegründer von NITSAN - einer TYPO3-Agentur in Bhavnagar, Indien. Er ist ein gefragter Vermarkter von NITSAN, akkreditiert mit dem Management von Kundendienstteams, Kooperationen mit Agenturen und Kunden, Projektausführungen und mehr. Ein Tech-Typ im wahrsten Sinne des Wortes, Sanjay ist der eigentliche TYPO3 Guru bei NITSAN.

Hinterlasse einen Kommentar

Need expert tips? Check out our Blog!

Diese Webseite nutzt Cookies

Diese Webseite nutzt Cookies zur Verbesserung des Erlebnisses unserer Besucher. Indem Sie weiterhin auf dieser Webseite navigieren, erklären Sie sich mit unserer Verwendung von Cookies einverstanden.