Search code examples
phpjavascriptformattingsyntax-highlightingsyntaxhighlighter

Indenting Syntax Highlighter when starting with a php echo


I am using Alex Gorbatchev's Syntax Highlighter to output some code, but I'm getting an issue where it's not retaining the initial indentation if I start the line with a php echo. For example where I would expect the following:

<pre class="brush: php;">
    <?php
        echo $variable->getScope() . " ";
        echo $variable->getName();
    ?>;
</pre>

to output the following:

    protected $variable;

it instead outputs the following:

protected $variable;

losing the indented four spaces. If I add a piece of static text on the line above it retains the spaces as intended.

I've also tried changing the third line to the following:

echo "    ".$variable->getScope() . " ";

which also has no effect. Here is the javascript where I am initialising Syntax Highlighter:

SyntaxHighlighter.defaults['gutter'] = false;
SyntaxHighlighter.defaults['toolbar'] = false;
SyntaxHighlighter.all()

To clarify, the highlighter is functioning as expected in every other way.

Any Ideas?


Solution

  • The syntax highlighter will indent the code, and it sees your code is in no bracket and so it does not indent. Code in a function will e.g. get indented.