[SOLVED] Equivalent to Shortcode/Inserttags

In Wordpress/Contao is the option to insert shortcodes/inserttags into text fields.
For example, if you want to insert a dynamic date into some text, you can just write
Foo {{date::Y}} bar.
This would be live converted in the frontend to
Foo 2017 bar.

Developers are able to write own PHP code for such Shortcodes.

Is there an equivalent in neos?

Hey @many,

Neos uses so called “ViewHelpers” that you can use. Have a look at the templates of the Neos.Neos package and the corresponding classes (in Classes/ViewHelpers). If you have questions, just ask. There are already many ViewHelpers built-in.

Hi @BenjaminK,

I ment text that is entered by the user and stored to some property, i.e. with Aloha.

There is no real API for short codes, but you can pretty easily create a custom processor that is applied via Fusion:

namespace Your\Package;

use Neos\Fusion\FusionObjects\AbstractFusionObject;

final class ConvertShortCodeImplementation extends AbstractFusionObject

     * @return string
    public function evaluate()
        $text = $this->fusionValue('value');
        return str_replace('{{date::Y}}', (new \DateTime())->format('Y'), $text);

Example: add it to all content elements when not in backend

prototype(Your.Package:ConvertShortNode) {
    @class = 'Your\\Package\\ConvertShortCodeImplementation'
    value = ${value}

prototype(Neos.Neos:Content) {
    @process.convertShortCodes {
        expression = Site:ConvertShortNode
        @position = 'end'
        @if.notInBackend = ${!node.context.inBackend}

Like this the short codes would be displayed as-is in Backend and converted when rendered in FE. If you want to convert them in the Backend as well you can omit the last @if condition.

Note: The result of your conversion will be cached along with the corresponding node

You can use Fusion processors to search and replace output from a specific content type or all of them. That’s how the linking works e.g.

See http://neos.readthedocs.io/en/stable/References/EelHelpersReference.html#string-pregreplace-string-pattern-replace

Then apply it to all content elements using a processor.

prototype(Neos.Neos:Content) {
    @process.shortCodes = ${String.pregReplace(value, '{{date::Y}}, Date.format('now', 'Y'))}

Didn’t test this, but you get the idea.

Thank you very much. Works exactly how it is supposed!

1 Like