XF 2.3 Saving/displaying content with h2/3 tags

Anatoliy

Well-known member
In the 'Demo/Pad' tutorial saved notes are simple text, and only 'nl2br' filter is needed to display it the correct way.
I need my notes to have h2/h3 headers. What would be the correct way to achieve that?
Is there a filter something like {$note.content|safe} ? Or I should use [h2] instead
Code:
<h2>
? what filter should I apply then?

Please advise.
 
You can use BBCode, otherwise you're on your own as far as implementing a safe way to store and render formatted text.
 
If you have a BBCode string, you use the bb_code template function to render it to HTML:

HTML:
{{ bb_code('[HEADING=2]Some heading[/HEADING]', null, null) }}

If you have any pointed questions feel free to ask but otherwise the topic is fairly broad and open-ended so it's not clear what guidance you'd like.
 
If you have a BBCode string, you use the bb_code template function to render it to HTML:

HTML:
{{ bb_code('[HEADING=2]Some heading[/HEADING]', null, null) }}

If you have any pointed questions feel free to ask but otherwise the topic is fairly broad and open-ended so it's not clear what guidance you'd like.
Thanks. )

HTML:
{{ bb_code('[HEADING=2]Some heading[/HEADING]', 'str', null) }}
works if I put it directly in my template. However I need headings within my 'note' content, that is saved in db, and then fetching it with a finder, and passing to a template. in this case the code of bb_code function is displayed, not the value (not the text wrapped in html h tag).

Probably in this case I should put in a template not just {$note.content} but with some filter like {$note.content|convertBbcodeToHtml} ?
 
Probably in this case I should put in a template not just {$note.content} but with some filter like {$note.content|convertBbcodeToHtml} ?
That's what I'm saying, bb_code(...) does exactly that.

HTML:
{{ bb_code($note.content, 'note', $note) }}
 
That's what I'm saying, bb_code(...) does exactly that.

HTML:
{{ bb_code($note.content, 'note', $note) }}
OK. I almost understand )
So in 'content' of a 'note' I put
Code:
[HEADING=2]Some heading[/HEADING]
as a code for a header?
 
Yeah, that's the BBCode for a level two heading. You can put any BBCode in the note contents and it will be rendered with the bb_code function.
 
Yeah, that's the BBCode for a level two heading. You can put any BBCode in the note contents and it will be rendered with the bb_code function.
Thanks! It works!! You untangled me. )
The only one question is not clear to me - what about a paragraph tag?
HTML:
<p></p>
There is no such a thing in BBCode?
 
Not really, just regular newlines (which are currently converted into <br> I think). You could create a custom [p] tag if you wanted.

It's also likely worth using the message preparer service when saving BBCode:
PHP:
$preparer = \XF::service(
    \XF\Service\Message\PreparerService::class,
    'note',
    $note
);
$note->content = $preparer->prepare($input);
$preparer->pushEntityErrorIfInvalid($note, 'content');
 
Not really, just regular newlines (which are currently converted into <br> I think). You could create a custom [p] tag if you wanted.

It's also likely worth using the message preparer service when saving BBCode:
PHP:
$preparer = \XF::service(
    \XF\Service\Message\PreparerService::class,
    'note',
    $note
);
$note->content = $preparer->prepare($input);
$preparer->pushEntityErrorIfInvalid($note, 'content');
Thanks. I added bbcode for p... I guess it's too late and too much for one time to digest. )
I'll have some rest and will continue tomorrow.

Thanks a lot for your help!
 
Back
Top Bottom