Print Php Tag In Cmarkdown

How to print opening and closing PHP tags (<?php ?>) in a view, in a part, that is formatted by CMarkdown widget?

If I try something like this:


<?php $this->beginWidget('CMarkdown'); ?>

~~~

[php]

<?php $this->widget('bootstrap.widgets.TbButton', array

(

    'label'=>'Primary',

    'type'=>'primary', // null, 'primary', 'info', 'success', 'warning', 'danger' or 'inverse'

    'size'=>'large', // null, 'large', 'small' or 'mini'

)); ?>

~~~

<?php $this->endWidget(); ?>

Code inside is being executed, making huge mess in a view.

If I use entites instead:


<?php $this->beginWidget('CMarkdown'); ?>

~~~

[php]

&lt;?php $this->widget('bootstrap.widgets.TbButton', array

(

    'label'=>'Primary',

    'type'=>'primary', // null, 'primary', 'info', 'success', 'warning', 'danger' or 'inverse'

    'size'=>'large', // null, 'large', 'small' or 'mini'

)); ?&gt;

~~~

<?php $this->endWidget(); ?>

they’re printed in unchanged way (due to using them in code block).

Can you put it in a string variable using nowdoc?




$content = <<<'EOD'

<?php $this->widget('bootstrap.widgets.TbButton', array

(

    'label'=>'Primary',

    'type'=>'primary', // null, 'primary', 'info', 'success', 'warning', 'danger' or 'inverse'

    'size'=>'large', // null, 'large', 'small' or 'mini'

)); ?>

EOD;


<?php $this->beginWidget('CMarkdown'); ?>

~~~

[php]

<?php echo $content; ?>

~~~

<?php $this->endWidget(); ?>



you might wanna wrap it in




htmlspecialchars($content);

Thanks for the idea. I’ve heard a little bit about heredoc and nowdoc, but never used it before. Now, I educated myself a little bit and trully, nowdoc should be solution for this problem. But it isn’t! :[

It even fails before I actually use CMarkdown. For example this, should work (I think):


<?php 

echo <<<'EOT'

<?php $this->widget('bootstrap.widgets.TbButton', array

(

    'label'=>'Primary',

    'type'=>'primary', // null, 'primary', 'info', 'success', 'warning', 'danger' or 'inverse'

    'size'=>'large', // null, 'large', 'small' or 'mini'

)); ?>

EOT;

?>

but it is not working. I see string starting with "widget". Trailling "<?php $this->" part is removed (end tag "?>" is left).

I tried to put everything inside curly brackets ({}) – a solution that I found on one of pages, but also no luck.

I have analyzed source code returned to browser, by server, and I clearly see, that something is changing this:


<?php $this

into this:


<!--?php $this--->

and probably that’s the reason, while while thing fails.

According to PHP Docs, “no parsing is done inside a nowdoc”. Which could means, that this problem has a different source. Maybe this is some PHP addition itself for a security reasons. I don’t know.

Until someone give me any idea how to print in a view PHP tags formatted in CMarkdown, the only partial workaround I found is to simply omit them. I use blocks like this:


<?php $this->beginWidget('CMarkdown'); ?>


~~~

[php]

$this->widget('bootstrap.widgets.TbButton', array

(

    'label'=>'acrid.pl',

    'type'=>'primary',

    'size'=>'large',

    'buttonType'=>'link',

    'url'=>'http://www.acrid.pl/'

));

~~~


<?php $this->endWidget(); ?>

which are properly printed, formatted and colloured so everything is fine. And code example printed like this (without PHP tags) is also clearly readable to any PHP developer.

I’m getting even stranger results! Here:


$content = <<<'EOD'

<a class="btn btn-primary btn-large">Primary</a>EOD;

Things that shouldn’t be printed (i.e. EOD) are printed and things that should be printed ($this->widget) are executed, so the browsers gets Bootstrap resulting code for a button, not a code printed itself.

I’m not sure, if solution exists. Maybe I want to much. I’m mixing executable code inside Markdown inside executable code. This could be too much for PHP…

What about:


<?php $this->beginWidget('CMarkdown'); ?>


~~~

[php]

<?php echo '<?php'?>$this->widget('bootstrap.widgets.TbButton', array

(

    'label'=>'acrid.pl',

    'type'=>'primary',

    'size'=>'large',

    'buttonType'=>'link',

    'url'=>'http://www.acrid.pl/'

));

<?php echo '?>'?>

~~~


<?php $this->endWidget(); ?>

So simple…

Well keep in mind the closing "EOD" identifier must be on a separate line. See the php documentation heredoc:

[quote name=‘PHP heredoc documentation’]It is very important to note that the line with the closing identifier must contain no other characters, except possibly a semicolon (;). That means especially that the identifier may not be indented, and there may not be any spaces or tabs before or after the semicolon. It’s also important to realize that the first character before the closing identifier must be a newline as defined by the local operating system. This is \n on UNIX systems, including Mac OS X. The closing delimiter (possibly followed by a semicolon) must also be followed by a newline.
[/quote]

I do it like zaccaria - you don’t need to echo the closing ‘?>’, though. It’s a bit inconvenient but didn’t find a better solution so far.

Yes… so simple… pity, it is not working…

This is what I got, when I look into source of page generated by my PHP/Yii and returned to browser:


<p>~~~

[php]

<?php $this->widget('bootstrap.widgets.TbButton', array

(

    'label'=>'acrid.pl',

    'type'=>'primary',

    'size'=>'large',

    'buttonType'=>'link',

    'url'=>'http://www.acrid.pl/'

));

?>~~~</p>

Looks fine at first sight. But when I look at exactly this page part using Chrome Dev Tools / Firebug, I see:


~~~

[php]

<!--?php$this--->

Something – I have no bloody idea what – is adding XHTML comments tag around PHP tag.

Plus: I have no damn idea, why this part is not formatted with Markdown?

When I looked into page source in Firefox, I noticed that part "<?php $this->" (here: without XHTML comment tags!) was printed in different color (red) than surrounding code (black). I hovered mouse over it and a tooltip appeared saying (my translation from Polish): "<? found. Possible cause: An attempt of using XML parsing instruction in XHTML (XML parsing instructions are not supported in XHTML)". Does it means, that a browser itself is responsible for adding XHTML comments tag around PHP tags and that browser causes my PHP code inside Markdown code to not be rendered properly?

First, I copied your example exactly char-by-char and paste it at the very end of my view file. No effect. Then I pasted only your markdown code into my pair of CMarkdown widget:


~~~

[php]

$this->widget('bootstrap.widgets.TbButton', array

(

    'label'=>'acrid.pl',

    'type'=>'primary',

    'size'=>'large',

    'buttonType'=>'link',

    'url'=>'http://www.acrid.pl/'

));

~~~


~~~

[php]

<?php echo '<?php'?>$this->widget('bootstrap.widgets.TbButton', array

(

    'label'=>'acrid.pl',

    'type'=>'primary',

    'size'=>'large',

    'buttonType'=>'link',

    'url'=>'http://www.acrid.pl/'

));

<?php echo '?>'?>

~~~

and it turned out that this (yours) block code hasn’t been formatted with Markdown, though code block before it (mine), inside the very same pair of widget tags is formatted. Some kind of magic is happening here. I got no idea, what?

When I removed “<?php echo ‘<?php’?>” and “<?php echo ‘?>’?>” from your code, both blocks of code started to be formatted correctly.

zaccaria’s example is missing a space inside the quotes: It should be '<?php ’ instead of ‘<?php’.

Missing space was the first thing I noticed and checked / changed. No effect.

Something (PHP / browser / echo / something else) is chaning any occurence of "<?php" into "<!–<?php–>", whenever it is inside another "<?php" (nested). Because only one occurence of "<?php" of course works fine.