Skip to content

Instantly share code, notes, and snippets.

@trkoch
Created April 15, 2015 08:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save trkoch/006cd219895d7a7cb8e1 to your computer and use it in GitHub Desktop.
Save trkoch/006cd219895d7a7cb8e1 to your computer and use it in GitHub Desktop.
Computed properties & JSON
# 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
}
}
}
<?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'],
# ...
);
}
}
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