TYPO3 Extbase custom viewhelper.

As the name suggest, viewhelper helps to modify the view/output of a fluid template.

Here I am showing how to write a viewhelper. This simple viewhelper returns “Yes” if “1” is passed and else “No”.

1. Create the following file.

myExt/Classes/ViewHelpers/YesnoViewHelper.php

2. Write code in that file

<?php
namespace Vendor\Myext\ViewHelpers;

class YesnoViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper {

/**
* @param integer $myvalue
*/
public function render($myvalue) {
$content = ”;
if($myvalue == 1) {
$content = ‘Yes’;
} else {
$content = ‘No’;
}
return $content;
}
}

?>

3. In fluid template use it like below

{namespace custom=Vendor\Myext\ViewHelpers}

<custom:yesno myvalue=”{value}” />