XF 2.2 BBcode executed on page-node

Old Nick

Well-known member
Hello, on a page-node i try to display some HTML code, like this
HTML:
<xf:if is=\"$thread.cover_image\">
    <xf:macro template=\"metadata_macros\" name=\"metadata\"
        arg-description=\"{\$fpSnippet}\"
        arg-shareUrl=\"{{ link('canonical:threads', $thread) }}\"
        arg-canonicalUrl=\"{{ link('canonical:threads', $thread, {'page': $page}) }}\"
        arg-imageUrl=\"{\$thread.cover_image}\" />
    <xf:else/>
    <xf:macro template=\"metadata_macros\" name=\"metadata\"
        arg-description=\"{\$fpSnippet}\"
        arg-shareUrl=\"{{ link('canonical:threads', $thread) }}\"
        arg-canonicalUrl=\"{{ link('canonical:threads', $thread, {'page': $page}) }}\" />
</xf:if>

Wrapped between {{ bb_code("[CODE=html] and [/CODE]", '', '') }}

I have to escape all the " to avoid template errors.
Unfortunately some part of this code is render executed as you can see:

Capture d’écran 2023-02-24 112206.jpeg

How to prevent that?
Thanks.
 
Solution
Sorry, missed your bbcode bit. Not sure how to get the official BBcode code wrapper working.
This is a basic pre code:
Code:
<pre>
&lt;xf:if is="$thread.cover_image">
    &lt;xf:macro template="metadata_macros" name="metadata"
        arg-description="&#123;$fpSnippet}"
        arg-shareUrl="&#123;&#123; link('canonical:threads', $thread) }}"
        arg-canonicalUrl="&#123;&#123; link('canonical:threads', $thread, {'page': $page}) }}"
        arg-imageUrl="&#123;$thread.cover_image}" />
    &lt;xf:else/>
    &lt;xf:macro template="metadata_macros" name="metadata"
        arg-description="&#123;$fpSnippet}"
        arg-shareUrl="&#123;&#123; link('canonical:threads', $thread) }}"
        arg-canonicalUrl="&#123;&#123...
De plus le code génère des erreurs

Code:
ErrorException: Template error: [E_WARNING] Object of class XF\Entity\Page could not be converted to int src/XF/Mvc/Router.php:716
Généré par: Compte inconnu le 24 Février 2023 à 12:28
Code:
#0 src/XF/Mvc/Router.php(716): XF\Template\Templater->handleTemplateError(2, '[E_WARNING] Obj...', '/srv/data/web/v...', 716)
#1 [internal function]: XF\Mvc\Router->XF\Mvc\{closure}(Array)
#2 src/XF/Mvc/Router.php(700): preg_replace_callback('#:page(<([a-zA-...', Object(Closure), ':page')
#3 src/XF/Mvc/Router.php(479): XF\Mvc\Router->buildRouteUrl('threads', Array, '', NULL, Array)
#4 src/XF/Mvc/Router.php(440): XF\Mvc\Router->buildLinkPath('threads', NULL, Array)
#5 src/XF/Template/Templater.php(3094): XF\Mvc\Router->buildLink('threads', NULL, Array, NULL)
#6 src/XF/Template/Templater.php(1129): XF\Template\Templater->fnLink(Object(MaZ\AUN\XF\Template\Templater), false, 'canonical:threa...', NULL, Array)
#7 internal_data/code_cache/templates/l2/s1/public/_page_node.58.php(410): XF\Template\Templater->func('link', Array, false)
#8 src/XF/Template/Templater.php(1652): XF\Template\Templater->{closure}(Object(MaZ\AUN\XF\Template\Templater), Array, NULL)
#9 src/addons/MaZ/AMP/Traits/Templater/XF22.php(52): XF\Template\Templater->renderTemplate('_page_node.58', Array, true, NULL)
#10 src/addons/MaZ/AUN/XF/Template/Templater.php(39): MaZ\AMP\XF\Template\Templater->renderTemplate('public:_page_no...', Array, true, NULL)
#11 src/XF/Template/Templater.php(1681): MaZ\AUN\XF\Template\Templater->renderTemplate('public:_page_no...', Array)
#12 internal_data/code_cache/templates/l2/s1/public/page_view.php(76): XF\Template\Templater->includeTemplate('public:_page_no...', Array)
#13 src/XF/Template/Templater.php(1652): XF\Template\Templater->{closure}(Object(MaZ\AUN\XF\Template\Templater), Array, NULL)
#14 src/addons/MaZ/AMP/Traits/Templater/XF22.php(52): XF\Template\Templater->renderTemplate('page_view', Array, true, NULL)
#15 src/addons/MaZ/AUN/XF/Template/Templater.php(39): MaZ\AMP\XF\Template\Templater->renderTemplate('public:page_vie...', Array, true, NULL)
#16 src/XF/Template/Template.php(24): MaZ\AUN\XF\Template\Templater->renderTemplate('public:page_vie...', Array)
#17 src/XF/Mvc/Renderer/Html.php(50): XF\Template\Template->render()
#18 src/XF/Mvc/Dispatcher.php(460): XF\Mvc\Renderer\Html->renderView('XF:Page\\View', 'public:page_vie...', Array)
#19 src/XF/Mvc/Dispatcher.php(442): XF\Mvc\Dispatcher->renderView(Object(XF\Mvc\Renderer\Html), Object(XF\Mvc\Reply\View))
#20 src/XF/Mvc/Dispatcher.php(402): XF\Mvc\Dispatcher->renderReply(Object(XF\Mvc\Renderer\Html), Object(XF\Mvc\Reply\View))
#21 src/XF/Mvc/Dispatcher.php(60): XF\Mvc\Dispatcher->render(Object(XF\Mvc\Reply\View), 'html')
#22 src/XF/App.php(2483): XF\Mvc\Dispatcher->run()
#23 src/XF.php(524): XF\App->run()
#24 index.php(20): XF::runApp('XF\\Pub\\App')
#25 {main}
Code:
array(4) {
  ["url"] => string(24) "/tutoriels/addon-create/"
  ["referrer"] => bool(false)
  ["_GET"] => array(0) {
  }
  ["_POST"] => array(0) {
  }
}
 
Might be a different approach but I think you'd need to escape the <xf:if> statement bits:

Code:
&lt;xf:if is="$thread.cover_image"&gt;

This way XF won't try to check it as a valid conditional. Do this for the else:if + closing </xf:if>
 
Last edited:
Thanks Russ, but the problem is not with the conditional statements. I actually manage to display most of the code by nesting it inside {{ bb_code("[CODE=html] and [/CODE]", '', '') }} and then escaping the " and the $ from the variables as you can see below.

Code:
<xf:if is=\"$thread.cover_image\">
    <xf:macro template=\"metadata_macros\" name=\"metadata\"
        arg-description=\"{\$fpSnippet}\"
        arg-shareUrl=\"{{ link('canonical:threads', $thread) }}\"
        arg-canonicalUrl=\"{{ link('canonical:threads', $thread, {'page': $page}) }}\"
        arg-imageUrl=\"{\$thread.cover_image}\" />
    <xf:else/>
    <xf:macro template=\"metadata_macros\" name=\"metadata\"
        arg-description=\"{\$fpSnippet}\"
        arg-shareUrl=\"{{ link('canonical:threads', $thread) }}\"
        arg-canonicalUrl=\"{{ link('canonical:threads', $thread, {'page': $page}) }}\" />
</xf:if>

The problem I'm having then is that if I try to escape the $ from the $threads variables (x4) in the code I can't save the template because of a template error but if I don't escape them then those variables are rendered in the final display, which I don't want.

capture-d%E2%80%99%C3%A9cran-2023-02-24-112206-jpeg.281992


Your proposition, unfortunately, doesn't resolve this problem.
 
Sure.
HTML:
<xf:if is="$thread.cover_image">
    <xf:macro template="metadata_macros" name="metadata"
        arg-description="{$fpSnippet}"
        arg-shareUrl="{{ link('canonical:threads', $thread) }}"
        arg-canonicalUrl="{{ link('canonical:threads', $thread, {'page': $page}) }}"
        arg-imageUrl="{$thread.cover_image}" />
    <xf:else/>
    <xf:macro template="metadata_macros" name="metadata"
        arg-description="{$fpSnippet}"
        arg-shareUrl="{{ link('canonical:threads', $thread) }}"
        arg-canonicalUrl="{{ link('canonical:threads', $thread, {'page': $page}) }}" />
</xf:if>
 
Sorry, missed your bbcode bit. Not sure how to get the official BBcode code wrapper working.
This is a basic pre code:
Code:
<pre>
&lt;xf:if is="$thread.cover_image">
    &lt;xf:macro template="metadata_macros" name="metadata"
        arg-description="&#123;$fpSnippet}"
        arg-shareUrl="&#123;&#123; link('canonical:threads', $thread) }}"
        arg-canonicalUrl="&#123;&#123; link('canonical:threads', $thread, {'page': $page}) }}"
        arg-imageUrl="&#123;$thread.cover_image}" />
    &lt;xf:else/>
    &lt;xf:macro template="metadata_macros" name="metadata"
        arg-description="&#123;$fpSnippet}"
        arg-shareUrl="&#123;&#123; link('canonical:threads', $thread) }}"
        arg-canonicalUrl="&#123;&#123; link('canonical:threads', $thread, {'page': $page}) }}" />
&lt;/xf:if>
</pre>
 
Last edited:
Solution
I guess you could also just copy the HTML it outputs when posting to a normal message and paste that into your template to get the colors:

Code:
<xf:css src="bb_code.less" />

<div class="bbCodeBlock bbCodeBlock--screenLimited bbCodeBlock--code">
    <div class="bbCodeBlock-title">
        HTML:
    </div>
    <div class="bbCodeBlock-content" dir="ltr">
        <pre class="bbCodeCode language-html" dir="ltr" data-xf-init="code-block" data-lang="html"><code class=" language-html"><span class="prism-token prism-tag"><span class="prism-token prism-tag"><span class="prism-token prism-punctuation">&lt;</span><span class="prism-token prism-namespace">xf:</span>if</span> <span class="prism-token prism-attr-name">is</span><span class="prism-token prism-attr-value"><span class="prism-token prism-punctuation">=</span><span class="prism-token prism-punctuation">"</span>$thread.cover_image<span class="prism-token prism-punctuation">"</span></span><span class="prism-token prism-punctuation">&gt;</span></span>
    <span class="prism-token prism-tag"><span class="prism-token prism-tag"><span class="prism-token prism-punctuation">&lt;</span><span class="prism-token prism-namespace">xf:</span>macro</span> <span class="prism-token prism-attr-name">template</span><span class="prism-token prism-attr-value"><span class="prism-token prism-punctuation">=</span><span class="prism-token prism-punctuation">"</span>metadata_macros<span class="prism-token prism-punctuation">"</span></span> <span class="prism-token prism-attr-name">name</span><span class="prism-token prism-attr-value"><span class="prism-token prism-punctuation">=</span><span class="prism-token prism-punctuation">"</span>metadata<span class="prism-token prism-punctuation">"</span></span>
        <span class="prism-token prism-attr-name">arg-description</span><span class="prism-token prism-attr-value"><span class="prism-token prism-punctuation">=</span><span class="prism-token prism-punctuation">"</span>{$fpSnippet}<span class="prism-token prism-punctuation">"</span></span>
        <span class="prism-token prism-attr-name">arg-shareUrl</span><span class="prism-token prism-attr-value"><span class="prism-token prism-punctuation">=</span><span class="prism-token prism-punctuation">"</span>{{ link('canonical:threads', $thread) }}<span class="prism-token prism-punctuation">"</span></span>
        <span class="prism-token prism-attr-name">arg-canonicalUrl</span><span class="prism-token prism-attr-value"><span class="prism-token prism-punctuation">=</span><span class="prism-token prism-punctuation">"</span>{{ link('canonical:threads', $thread, {'page': $page}) }}<span class="prism-token prism-punctuation">"</span></span>
        <span class="prism-token prism-attr-name">arg-imageUrl</span><span class="prism-token prism-attr-value"><span class="prism-token prism-punctuation">=</span><span class="prism-token prism-punctuation">"</span>{$thread.cover_image}<span class="prism-token prism-punctuation">"</span></span> <span class="prism-token prism-punctuation">/&gt;</span></span>
    <span class="prism-token prism-tag"><span class="prism-token prism-tag"><span class="prism-token prism-punctuation">&lt;</span><span class="prism-token prism-namespace">xf:</span>else</span><span class="prism-token prism-punctuation">/&gt;</span></span>
    <span class="prism-token prism-tag"><span class="prism-token prism-tag"><span class="prism-token prism-punctuation">&lt;</span><span class="prism-token prism-namespace">xf:</span>macro</span> <span class="prism-token prism-attr-name">template</span><span class="prism-token prism-attr-value"><span class="prism-token prism-punctuation">=</span><span class="prism-token prism-punctuation">"</span>metadata_macros<span class="prism-token prism-punctuation">"</span></span> <span class="prism-token prism-attr-name">name</span><span class="prism-token prism-attr-value"><span class="prism-token prism-punctuation">=</span><span class="prism-token prism-punctuation">"</span>metadata<span class="prism-token prism-punctuation">"</span></span>
        <span class="prism-token prism-attr-name">arg-description</span><span class="prism-token prism-attr-value"><span class="prism-token prism-punctuation">=</span><span class="prism-token prism-punctuation">"</span>{$fpSnippet}<span class="prism-token prism-punctuation">"</span></span>
        <span class="prism-token prism-attr-name">arg-shareUrl</span><span class="prism-token prism-attr-value"><span class="prism-token prism-punctuation">=</span><span class="prism-token prism-punctuation">"</span>{{ link('canonical:threads', $thread) }}<span class="prism-token prism-punctuation">"</span></span>
        <span class="prism-token prism-attr-name">arg-canonicalUrl</span><span class="prism-token prism-attr-value"><span class="prism-token prism-punctuation">=</span><span class="prism-token prism-punctuation">"</span>{{ link('canonical:threads', $thread, {'page': $page}) }}<span class="prism-token prism-punctuation">"</span></span> <span class="prism-token prism-punctuation">/&gt;</span></span>
<span class="prism-token prism-tag"><span class="prism-token prism-tag"><span class="prism-token prism-punctuation">&lt;/</span><span class="prism-token prism-namespace">xf:</span>if</span><span class="prism-token prism-punctuation">&gt;</span></span></code></pre>
    </div>
</div>

You'll need to include the bbcode template css. Obviously... your bbcode wrapper in your first post would probably be idea if it could get sorted out. But on page nodes, a lot of the time you can simply post in a normal message and copy the HTML over to use.
 
Variables are rendered

View attachment 282372

But your previous solution is sufficient, the copy/paste capacity is what i wanted so all is good.

And that's where you'd replace the { with the appropriate escaped versions:

Code:
<xf:css src="bb_code.less" />

<div class="bbCodeBlock bbCodeBlock--screenLimited bbCodeBlock--code">
    <div class="bbCodeBlock-title">
        HTML:
    </div>
    <div class="bbCodeBlock-content" dir="ltr">
        <pre class="bbCodeCode language-html" dir="ltr" data-xf-init="code-block" data-lang="html"><code class=" language-html"><span class="prism-token prism-tag"><span class="prism-token prism-tag"><span class="prism-token prism-punctuation">&lt;</span><span class="prism-token prism-namespace">xf:</span>if</span> <span class="prism-token prism-attr-name">is</span><span class="prism-token prism-attr-value"><span class="prism-token prism-punctuation">=</span><span class="prism-token prism-punctuation">"</span>$thread.cover_image<span class="prism-token prism-punctuation">"</span></span><span class="prism-token prism-punctuation">&gt;</span></span>
    <span class="prism-token prism-tag"><span class="prism-token prism-tag"><span class="prism-token prism-punctuation">&lt;</span><span class="prism-token prism-namespace">xf:</span>macro</span> <span class="prism-token prism-attr-name">template</span><span class="prism-token prism-attr-value"><span class="prism-token prism-punctuation">=</span><span class="prism-token prism-punctuation">"</span>metadata_macros<span class="prism-token prism-punctuation">"</span></span> <span class="prism-token prism-attr-name">name</span><span class="prism-token prism-attr-value"><span class="prism-token prism-punctuation">=</span><span class="prism-token prism-punctuation">"</span>metadata<span class="prism-token prism-punctuation">"</span></span>
        <span class="prism-token prism-attr-name">arg-description</span><span class="prism-token prism-attr-value"><span class="prism-token prism-punctuation">=</span><span class="prism-token prism-punctuation">"</span>&#123;$fpSnippet}<span class="prism-token prism-punctuation">"</span></span>
        <span class="prism-token prism-attr-name">arg-shareUrl</span><span class="prism-token prism-attr-value"><span class="prism-token prism-punctuation">=</span><span class="prism-token prism-punctuation">"</span>&#123;&#123; link('canonical:threads', $thread) }}<span class="prism-token prism-punctuation">"</span></span>
        <span class="prism-token prism-attr-name">arg-canonicalUrl</span><span class="prism-token prism-attr-value"><span class="prism-token prism-punctuation">=</span><span class="prism-token prism-punctuation">"</span>&#123;&#123; link('canonical:threads', $thread, {'page': $page}) }}<span class="prism-token prism-punctuation">"</span></span>
        <span class="prism-token prism-attr-name">arg-imageUrl</span><span class="prism-token prism-attr-value"><span class="prism-token prism-punctuation">=</span><span class="prism-token prism-punctuation">"</span>{$thread.cover_image}<span class="prism-token prism-punctuation">"</span></span> <span class="prism-token prism-punctuation">/&gt;</span></span>
    <span class="prism-token prism-tag"><span class="prism-token prism-tag"><span class="prism-token prism-punctuation">&lt;</span><span class="prism-token prism-namespace">xf:</span>else</span><span class="prism-token prism-punctuation">/&gt;</span></span>
    <span class="prism-token prism-tag"><span class="prism-token prism-tag"><span class="prism-token prism-punctuation">&lt;</span><span class="prism-token prism-namespace">xf:</span>macro</span> <span class="prism-token prism-attr-name">template</span><span class="prism-token prism-attr-value"><span class="prism-token prism-punctuation">=</span><span class="prism-token prism-punctuation">"</span>metadata_macros<span class="prism-token prism-punctuation">"</span></span> <span class="prism-token prism-attr-name">name</span><span class="prism-token prism-attr-value"><span class="prism-token prism-punctuation">=</span><span class="prism-token prism-punctuation">"</span>metadata<span class="prism-token prism-punctuation">"</span></span>
        <span class="prism-token prism-attr-name">arg-description</span><span class="prism-token prism-attr-value"><span class="prism-token prism-punctuation">=</span><span class="prism-token prism-punctuation">"</span>&#123;$fpSnippet}<span class="prism-token prism-punctuation">"</span></span>
        <span class="prism-token prism-attr-name">arg-shareUrl</span><span class="prism-token prism-attr-value"><span class="prism-token prism-punctuation">=</span><span class="prism-token prism-punctuation">"</span>&#123;&#123; link('canonical:threads', $thread) }}<span class="prism-token prism-punctuation">"</span></span>
        <span class="prism-token prism-attr-name">arg-canonicalUrl</span><span class="prism-token prism-attr-value"><span class="prism-token prism-punctuation">=</span><span class="prism-token prism-punctuation">"</span>&#123;&#123; link('canonical:threads', $thread, {'page': $page}) }}<span class="prism-token prism-punctuation">"</span></span> <span class="prism-token prism-punctuation">/&gt;</span></span>
<span class="prism-token prism-tag"><span class="prism-token prism-tag"><span class="prism-token prism-punctuation">&lt;/</span><span class="prism-token prism-namespace">xf:</span>if</span><span class="prism-token prism-punctuation">&gt;</span></span></code></pre>
    </div>
</div>

Anyhow, glad it's sorted. I keep my pages pretty simple and just use @Xon 's https://xenforo.com/community/resources/bbcode-pages.8293/ which is great. Glad it's working for you though now.
 
Back
Top Bottom