What is best practice on Magento 2 for having multiline html in content? On Magento 1 if I remember right you would use some thing <<<'EOT'
I'm getting syntax errors on this:
$cmsBlockData = [
'title' => "test block",
'identifier' => "test-block",
'content' => "<div>
<img src="{{view url="images/content/about-us-img2.jpg"}}" alt="" />
</div>",
'is_active' => 1,
'stores' => [0],
'sort_order' => 0
];
-
can you add that error here?Balwant Singh– Balwant Singh2019年09月04日 15:00:00 +00:00Commented Sep 4, 2019 at 15:00
2 Answers 2
You can use the <<<EOD as used in CreateDefaultPages.php file
In your case it will be like :
$blockContent = <<<EOD
<div>
<img src="{{view url="images/content/about-us-img2.jpg"}}" alt="" />
</div>
EOD;
$cmsBlockData = [
'title' => "test block",
'identifier' => "test-block",
'content' => $blockContent,
'is_active' => 1,
'stores' => [0],
'sort_order' => 0
];
This is called using the PHP Heredoc string syntax. Here's the PHP docs on it if you need to read more about it (and it's stricter brother, Nowdoc, a little further down)
actually there are four types of strings available in php. They are single quotes ('), double quotes (") and Nowdoc (<<<'EOD') and heredoc(<<<EOD) strings. reference
You can use here document operator (<<<) then use a word like HTML, EOD, EOT or any string you want!
Some handy notes when you wanna use heredoc : here
Sample :
$html = <<<HTML
<div class="intro-block" style="background-image: url({{view url='images/content/about-us-img1.jpg'}})">
<p>Lorem ipsum dolor sit, amet consectetur adipisicing elit.</p>
</div>
HTML;
$cmsBlockData = [
'title' => "About Us",
'identifier' => "aboutus",
'content' => $html
",
]