-
-
Save trkoch/006cd219895d7a7cb8e1 to your computer and use it in GitHub Desktop.
Computed properties & JSON
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# General purpose product model | |
prototype(Site:Product) { | |
@class = 'Vendor\\TypoScript\\ProductImplementation' | |
node = 'product-page-node' | |
asJson = ${asJson} | |
title = ${q(this.node).property('title')} | |
# ... | |
articleId = ${q(this.node).property('productArticle')} | |
articleId.@process { | |
lowerCase { | |
expression = ${String.toLowerCase(value)} | |
@position = 1 | |
} | |
stripWhitespace { | |
expression = ${String.pregReplace(value, '/\s/', '')} | |
@position = 2 | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Vendor\TypoScript; | |
use TYPO3\TypoScript\TypoScriptObjects\RawArrayImplementation; | |
class ProductImplementation extends RawArrayImplementation { | |
public function evaluate() { | |
$context = $this->tsRuntime->getCurrentContext(); | |
$context['asJson'] = $this->asJson(); | |
$this->tsRuntime->pushContextArray($context); | |
$output = parent::evaluate(); | |
$this->tsRuntime->popContext(); | |
return $output; | |
} | |
public function asJson() { | |
return array( | |
'article' => $this['articleId'], | |
# ... | |
); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
prototype(Site:ProductsIndex) < prototype(TYPO3.Neos:Content) { | |
node = ${node} | |
templatePath = 'resource://Vendor/Private/Templates/NodeTypes/Products/ProductsIndex.html' | |
# Iterate products | |
prototype(Site:ProductsCollection) { | |
collection = ${q(documentNode).children('[instanceof Vendor:ProductPage]')} | |
} | |
content = Site:ProductsCollection { | |
itemRenderer = Site:ProductItemHtml | |
} | |
# TODO: This is pretty cumbersome. Can we do better? | |
json = Site:ProductsCollection { | |
itemRenderer = Site:ProductItemJson { | |
@process.enumerate = ${productIteration.isLast > 0 ? value : value + ','} | |
} | |
@process.wrap = ${'[' + value + ']'} | |
} | |
} | |
# A single product as HTML | |
prototype(Site:ProductItemHtml) < prototype(TYPO3.TypoScript:Template) { | |
templatePath = 'resource://Vendor/Private/Templates/NodeTypes/Products/ProductItem.html' | |
# Use product model for HTML | |
product = Site:Product { | |
node = ${product} | |
} | |
} | |
# A single product as JSON | |
prototype(Site:ProductItemJson) < prototype(TYPO3.TypoScript:Value) { | |
# Use product model for JSON | |
product = Site:Product { | |
node = ${product} | |
} | |
value = ${Json.stringify(this.product.asJson)} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment