skip to Main Content

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


  1. 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:

    <pre>
        {{ $exampleElement }}
    </pre>
    

    If you need to use the actual value without escaping HTML tags (e.g., from <p>some text to &lt;p&gt;some text), you can use the {!! !!} syntax to print the variable like this:

    {!! $exampleElement !!}
    

    UPDATE:

    if you also want to add Content-Type: application/xml header to you response from your view you can do it like this:

    $content = view('admin.parser.settings', [
       'model' => $model,
       'exampleElement' => $exampleString,
    ]);
    
    return response($content)->header('Content-Type', 'application/xml');
    
    Login or Signup to reply.
  2. If you want to prettify the string returned by SimpleXML’s asXML() you could load the string in DOMDocument and use it’s preserveWhiteSpace and formatOutput parameters:

    https://www.php.net/manual/en/class.domdocument.php#domdocument.props.preservewhitespace

    Do not remove redundant white space.
    

    https://www.php.net/manual/en/class.domdocument.php#domdocument.props.formatoutput

    Nicely formats output with indentation and extra space.
    
    $xml = simplexml_load_file($model->url);
    $exampleElement = $xml;
    foreach (explode("->", $model->path) as $prop) {
       $exampleElement = $exampleElement->$prop;
    }
    $exampleString = $exampleElement->asXML();
    
    $domDocument = new DOMDocument('1.0');
    $domDocument->preserveWhiteSpace = true;
    $domDocument->formatOutput = true;
    $domDocument->loadXML($exampleString);
    
    $formattedString = $domDocument->saveXML();
    

    Now in your view you should output $formattedString in a pre element:

    https://developer.mozilla.org/en-US/docs/Web/HTML/Element/pre

    Represents preformatted text which is to be presented >exactly as written
    
    <pre>{{ $formattedString }}</pre>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search