XF 2.1 XF.ajax() response is not treating response as html

asprin

Active member
This is a followup to https://xenforo.com/community/threads/how-to-call-class-action-method-via-xf-ajax.187506

I've done the AJAX request-response part. However, whatever I'm returning from view is not being rendered as HTML, rather Xenforo is encoding entities and rendering the view as plain text.

1604831762899.png

This is the server side handler:
PHP:
// $html is processed earlier

$viewParams = [
     'html' => $html
];

return $this->view('ASP:xxxxxx', Con::template('abcdefg'), $viewParams);

And content of abcdefg.html is:
HTML:
{{$html}}

And the js handler:
JavaScript:
processResponse: function(data) {       
    if(data.status == 'ok')
    {
        XF.setupHtmlInsert(data.html, function($html) {
            $('#resultDiv').html($html);
        });     
    }
}

This is the final output on the page:
1604833376601.webp

Tried putting
HTML:
$this->setResponseType('html');
in the controller action but that was pulling back the entire header and footer and the main content was still being redered as text.
 
Last edited:
Try this:
Code:
{$html|raw}
Works! Before I accept as solution, can you perhaps confirm if that's the recommended way of doing it? Don't want to go outside of what is best practice.

The reason I'm asking is because for now I'm generating the html via PHP (concatenation and stuff), storing it in a single variable and passing to view, but I would like to do it via the template. However, if I take that approach, not quite sure how I would use a |raw filter on the entirety of the html. For example, something like this

abcdefg.html
HTML:
<xf:if is="$myData is not empty">
    <xf:foreach loop="$myData">
        <div>
            <p>.....</p>
            <small>.....</small>
        </div>
    </xf:foreach>
</xf:if>

So any idea how I would apply the raw filter to the above?
 
Last edited:
@asprin, you can look at the PAGE_CONTAINER template, the generated page content is displayed in the same way:
HTML:
            <div class="p-body-content">
                <xf:ad position="container_content_above" />
                <div class="p-body-pageContent">{$content|raw}</div>
                <xf:ad position="container_content_below" />
            </div>
 
@asprin, you can look at the PAGE_CONTAINER template, the generated page content is displayed in the same way:
HTML:
            <div class="p-body-content">
                <xf:ad position="container_content_above" />
                <div class="p-body-pageContent">{$content|raw}</div>
                <xf:ad position="container_content_below" />
            </div>
Thanks. Just edited my post to add more details. Can you perhaps suggest the solution?
 
@asprin, can you show your PHP code?
Currenlty using something like this:

PHP:
$rows = \XF::finder('MyEntity')->where(['some_col' => 1, 'active' => 1])->fetch();
$html = "";
if($rows)
{              
    foreach($rows as $s)
    {
        $html .= '<div>';
        $html .= '<p>'.$s->name.'</p>';
        $html .= '<small>'.$s->id.'</small>';
        $html .= '<hr />';
        $html .= '</div>';
    }
}

$viewparams = [
    'html' => $html
];

I would love to modify it to the following:
PHP:
$rows = \XF::finder('MyEntity')->where(['some_col' => 1, 'active' => 1])->fetch();

$viewParams = [
    'myData' => $rows
];

And then use the following in the template:
HTML:
<xf:if is="$myData is not empty">
    <xf:foreach loop="$myData">
        <div>
            <p>.....</p>
            <small>.....</small>
        </div>
    </xf:foreach>
</xf:if>

But not sure how to use |raw when the html is not contained within a variable.
 
  • Sad
Reactions: 021
Top Bottom