What are some of the best ways to deal with `<b...
# kobweb
c
What are some of the best ways to deal with
<br/>
in texts in kobweb? I noticed you can use the
innerHtml
of an
Element
to inject text which has some
<br/>
. But doing something like
SpanText(text = "Hello, this is a longer text with a line break.\nHere is the new line.")
does not work out of the box... Otherwise, adding markdown support also works, but makes it more bloated when it's only for 1 part of the content... 🤔 😅
d
I just use
Br()
, is that not available for your case?
You can also use a runtime markdown library
I am pretty sure I've added extension methods in my projects that convert a string to
Text
and
Br
calls, let me search ...
s
You can also use a
Column
around multiple
SpanText
👍 1
c
Hmm, that is also a possibility! But that only works for static text that does not come from the backend. Ooh serious? Haven't found that extension method yet 😮
d
I didn't put the extension method in kobweb, it's probably a bit too one off
Copy code
str.split("\n").forEach { line -> if (line.isNotEmpty()) SpanText(line) else Br()
Something like that should work
(Although maybe not! That code is only called as the children to a
Column
in my code, you'll have to treat it a bit differently if your text just lives in a normal
Div
)
s
but are you sure that \n doesn't work? maybe you're just missing a pre-wrap style or something
d
You want to convert
"Hello\nWorld"
to
Text("Hello"); Br(); Text("World")
s
<string name="why_classrooms_headline">More Time Teaching,\nLess Time Managing</string>
Copy code
H1(Modifier.whiteSpace(WhiteSpace.PreWrap).textAlign(TextAlign.Center).toAttrs()) {
                Text(Res.strings.why_classrooms_headline)
            }
d
I mean if you control the string, just put a <br> in it instead of a "\n"?
Actually nevermind, that won't work in a
Text
call
s
string isn't hardcoded as I understand it
c
My god, the
preWrap
indeed works as well... wow... 😅
👍🏻 1
Those options you mentioned were nice to know as well @David Herman! Didn't think of using the
.split
like that!!
😁 1
d
I would do something like
Copy code
Res.strings.why_classrooms_headline.addTextElements()
where
Copy code
@Composable
fun String.addTextElements() {
   val lines = this.split("\n")
   lines.forEachIndexed { i, line ->
      Text(line)
      if (i < lines.lastIndex) Br()
   }
}
(Wrote off the top of my head so it might not work without tweaks!)