FLUID Comments

Working with Extbase and Fluid for quite a while now, I had always been missing the possibility to write comments in templates which are not shown in rendered html code. After thinking about this for a while, I came up with the following solution:

<!--- ... --->

This has the charm that every IDE will recognize it as HTML comment without any further modifications and it is still removeable by a simple regular expression which is necessary to be conform with how Fluid parser works.

So here are the changes applied to Fluid.

 

1. Add a new regular expression to Tx_Fluid_Core_Parser_TemplateParser:

   /**
     * This pattern detects FLUID comments like <!--- ... --->
     *
     * @author Michael Knoll <mimi@kaktusteam.de>
     */
    public static $SCAN_FLUID_COMMENT = '/
        <!\-\-\-.+?\-\-\->
    /xs';

2. Modifiy Tx_Fluid_Core_Parser_TemplateParser::parse

	/**
	 * Parses a given template and returns a parsed template object.
	 *
	 * @param string $templateString The template to parse as a string
	 * @return Tx_Fluid_Core_Parser_ParsedTemplateInterface Parsed template
	 * @author Sebastian Kurf├╝rst <sebastian@typo3.org>
	 * @todo Refine doc comment
	 */
	public function parse($templateString) {
		if (!is_string($templateString)) throw new Tx_Fluid_Core_Parser_Exception('Parse requires a template string as argument, ' . gettype($templateString) . ' given.', 1224237899);

		$this->reset();

		$templateString = $this->extractNamespaceDefinitions($templateString);
		$templateString = $this->removeFluidComments($templateString);
		$splitTemplate = $this->splitTemplateAtDynamicTags($templateString);
		$parsingState = $this->buildObjectTree($splitTemplate);

		return $parsingState;
	}

3. Add a little helper method in Tx_Fluid_Core_Parser_TemplateParser

    /**
     * Removes FLUID comments like
     *
     * <!--- I am a FLUID comment --->
     *
     * from given template string.
     *
     * @param string $templateString
     * @return string Template string without comments
     * @author Michael Knoll <mimi@kaktusteam.de>
     */
    protected function removeFluidComments($templateString) {
        $templateString = preg_replace(self::$SCAN_FLUID_COMMENT, '', $templateString);
        return $templateString;
    }

4. Add a Unit Test for what we did so far in Tx_Fluid_Tests_Unit_Core_Parser_TemplateParserTest

    /**
     * @test
     * @author Michael Knoll <mimi@kaktusteam.de>
     */
    public function removeFluidCommentsRemovesFluidSingleLineCommentsCorrectly() {
        $templateParser = $this->getAccessibleMock('Tx_Fluid_Core_Parser_TemplateParser', array('dummy'));
        $templateString = 'blablabla<!--- comment to be removed --->blubb';
        $expectedTemplateString = 'blablablablubb';
        $templateStringAfterRemoveComments = $templateParser->_call('removeFluidComments', $templateString);
        $this->assertEquals($expectedTemplateString, $templateStringAfterRemoveComments);
    }

    /**
     * @test
     * @author Michael Knoll <mimi@kaktusteam.de>
     */
    public function removeFluidCommentsRemovesFluidMultiLineCommentsCorrectly() {
        $templateParser = $this->getAccessibleMock('Tx_Fluid_Core_Parser_TemplateParser', array('dummy'));
        $templateString = "blablabla \n".
                           "<!--- \n" .
                           "comment \n" .
                           " to be removed \n" .
                           "--->blubb";
        $expectedTemplateString =  "blablabla \n".
                                   "blubb";
        $templateStringAfterRemoveComments = $templateParser->_call('removeFluidComments', $templateString);
        $this->assertEquals($expectedTemplateString, $templateStringAfterRemoveComments);
    }

After those little changes to Fluid, you can use comments as follows:

    <!-- This comment will be rendered in output -->
    <!--- This is a single line comment that WON'T be rendered in output --->
    <!---
          This is some mulit-line comment
          that WON'T be rendered in output either
    --->

So - that's it, I hope, you like it! See attached file below for a diff for version 1.3.0 of fluid.


 
Inhalt © Michael Knoll 2009-2017  •  Powered by TYPO3  •  TypoScript Blogging by Fabrizio Branca  •  TYPO3 Photo Gallery Management by yag  •  Impressum