Oh, how ridiculously I formulated the question)) I’ll try in more detail.
I have a route:
Route::get('/parsers/settings/{id}', [AdminParserController::class, 'settings'])
->where('id', '[0-9]+')
->name('parsers::settings');
Then I select the required element from the XML file and convert it to a string:
$xml = simplexml_load_file($model->url);
$exampleElement = $xml;
foreach (explode("->", $model->path) as $prop) {
$exampleElement = $exampleElement->$prop;
}
$exampleString = $exampleElement->asXML();
return view('admin.parser.settings', [
'model' => $model,
'exampleElement' => $exampleString,
]);
In view I display this element like this:
{{$exampleElement}}
And in the blade I want to display this element. Since this is a regular string, it is displayed incorrectly. I need to style the output somehow.
return response($exampleString, 200, [
'Content-Type' => 'application/xml'
]);
If you do this, it will be displayed as it should. But I need to somehow transfer this to the view.
2
Answers
I understand that you’re facing an issue with displaying the content of
$exampleElement
. If you want to display it in a browser for users to view, you can use the<pre>
tag to preserve white spaces and line breaks like this:If you need to use the actual value without escaping HTML tags (e.g., from
<p>some text
to<p>some text
), you can use the{!! !!}
syntax to print the variable like this:UPDATE:
if you also want to add
Content-Type: application/xml
header to you response from your view you can do it like this:If you want to prettify the string returned by SimpleXML’s
asXML()
you could load the string in DOMDocument and use it’spreserveWhiteSpace
andformatOutput
parameters:https://www.php.net/manual/en/class.domdocument.php#domdocument.props.preservewhitespace
https://www.php.net/manual/en/class.domdocument.php#domdocument.props.formatoutput
Now in your view you should output
$formattedString
in apre
element:https://developer.mozilla.org/en-US/docs/Web/HTML/Element/pre