How to highlight text when click label in xamarin forms? - visual-studio

How to highlight text when click label get the index position and highlight One Sentence in xamarin forms.
This is my layout looks llike
<StackLayout>
<Label Text="{Binding Loremipsum.Content}"
x:Name="LoremipsumText"
FontSize="18"
FontFamily="Font4"
TextTransform="None"
TextColor="White">
<Label.GestureRecognizers>
<TapGestureRecognizer Tapped="btnHighLight"></TapGestureRecognizer>
</Label.GestureRecognizers>
</Label>
</StackLayout>
See this picture for reference
if the image is not load
this is the text example
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
iwant to hilght is this text
Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
and this is my cs codes look likes
public partial class LoremipsumInfoPage : ContentPage
{
string[] strList;
public LoremipsumInfoPage()
{
InitializeComponent();
string verses = Loremipsum.Text;
string strDot = ".";
char charDot= char.Parse(strDot);
string strComma = ",";
char charComma = char.Parse(strComma);
strList = verses.Split(new char[] { charDot, charComma });
}
private void btnHighLight(object sender, EventArgs e)
{
var formattedString = new FormattedString();
for (int v = 0; v < strList.Length; v++)
{
if (v == v)
{
formattedString.Spans.Add(new Span { Text = strList[v], ForegroundColor = Color.White, BackgroundColor = Color.Red });
}
else
{
formattedString.Spans.Add(new Span { Text = strList[v], ForegroundColor = Color.Black });
}
}
Loremipsum.FormattedText = formattedString;
}
}

As ToolmakerSteve said,you can set the formattedText of label with span.I write a sample you can refer to:
<Label>
<Label.FormattedText>
<FormattedString>
<Span Text="This is a text" />
<Span x:Name="hlText" Text="This is a highlighttext"/ >
</FormattedString>
</Label.FormattedText>
<Label.GestureRecognizers>
<TapGestureRecognizer Tapped="OnTapped" />
</Label.GestureRecognizers>
</Label>
codebehind:
void OnTapped(object Sender,Eventargs e)
{hlText.color=Color.Red
}

Related

Label text is not displayed fully

I know this topic has been discussed before. But still I cannot find a good solution from all the discussions. I have a Grid layout that contains a ScrollView and another Grid. Inside the ScrollView are three Labels. The problem is that the texts in the Labels are not always displayed in full. Sometimes even longer texts are displayed in full, and sometimes even shorter ones cannot. Here is my xamal code. The texts in the labels are placeholders and are replaced in C# code. Do I need a custom renderer for the labels as suggested by some people?
In the first screenshot, the title label contains text "test push notification on physical device". It is much shorter than the title in the second screenshot. Yet the second displays in full. This weird behavior also happens sometimes in the third label. The second label has fixed length, so it is not affected.
<Grid x:Name="GridLayout">
<Grid.RowDefinitions>
<RowDefinition Height="50*" />
<RowDefinition Height="50*" />
</Grid.RowDefinitions>
<ScrollView Grid.Row="0" x:Name="Scroll" x:FieldModifier="public" VerticalOptions="Start" HeightRequest="320" MinimumHeightRequest="320" Margin="0,0,0,0" Orientation="Vertical">
<StackLayout Spacing="0" Orientation="Vertical">
<Label Text="Resources" FontSize="31" x:Name="TitleLabel" x:FieldModifier="public" MaxLines="0" TextColor="Black" Padding="30,15,30,0" LineBreakMode="WordWrap">
<Label.FontFamily>
<OnPlatform x:TypeArguments="x:String">
<On Platform="iOS" Value="WorkSans-Bold" />
<On Platform="Android" Value="Fonts/WorkSans-Bold.ttf#WorkSans-Bold" />
</OnPlatform>
</Label.FontFamily>
</Label>
<Label x:FieldModifier="public" x:Name="TimeLabel" BackgroundColor="White" TextColor="Black" FontSize="22" FontFamily="sans-serif" MaxLines="0" VerticalOptions="Start" LineBreakMode="WordWrap" Padding="30,5,30,0" Text="Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.">
</Label>
<Label x:FieldModifier="public" x:Name="TextLabel" BackgroundColor="White" TextColor="Black" FontSize="16" FontFamily="sans-serif" MaxLines="0" VerticalOptions="FillAndExpand" Padding="30,5,30,0" LineBreakMode="WordWrap" Text="Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.">
</Label>
</StackLayout>
</ScrollView>
<Grid Grid.Row="1" RowSpacing="3" x:Name="BottomGrid">
...
</Grid>
</Grid>
I suppose you might need Margin instead of padding, if you use Padding then the text gets cut-off, as the inside area is getting shrunk, it works as intended when I replace it with margin.
<Label Text="Resources" FontSize="31" x:Name="TitleLabel" x:FieldModifier="public" MaxLines="0" TextColor="Black" Margin="30,15,30,0" LineBreakMode="WordWrap">

Search a sample of image floating left in vuetify

In my Laravel 5.6/“vue”: "^2.5.7/“vuetify”: “^1.0.8” application I search a sample of image floating left
and text at right with card implementation.
I searched here and did not find…
Could you please give a ref to example?
MODIFIED :
I mean html code like with long text:
<div class="card">
<div class="col-xs-12">
<a class="a_link" target="_blank" href="/storage/votes/-vote-14/mammals.jpg">
<img class=" pull-left img-medium-preview" src="/storage/votes/-vote-14/mammals.jpg" alt="Which is the tallest mammal?" width="423" height="280">
</a>
<div>
<h5 class="card-title">Which is the tallest mammal?</h5>
<p class="card-text">
Which is the tallest mammal lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</p>
</div>
</div>
</div>
and it looks like that with text floating the image : https://imgur.com/a/KT4Xg3n
Thanks!
Vuetify has a class left which applies float: left !important.
We can use that on the img (but not if it's inside a container which has display: flex), and then the text will wrap around the image.
codepen
And add some style to make it act responsively:
img {
max-width: 100%;
height: auto;
}
In vuetify version ^1.2.0 You can use v-img component which makes it easier to place images.
v-card and it's child components don't have specific props for this behavior afaik, so you can use arbitrary layout inside the card.
<v-card>
<v-layout>
<v-flex xs5>
<v-img src="https://cdn.vuetifyjs.com/images/cards/desert.jpg"></v-img>
</v-flex>
<v-flex xs7>
<v-card-title primary-title>
<div>
Text
</div>
</v-card-title>
</v-flex>
</v-layout>
</v-card>
Then take a look at v-img props and style the image as you see fit.
codepen
If you are not using v-img then you have to use custom CSS for styling your image.

Foundation6: Expanding image in nested column

I want to expand the image so that image is 100% width with the browser window with no gutter. It seems adding row expanded to the image does not do the job. Also I need to figure this out by only editing the css.
.wideimage{
min-width: 100vh;
}
<div class="row small-collapse">
<div class="medium-11 medium-centered column">
<p class="xlarge-8 xlarge-offset-2 ">m dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id e</p>
<div class="degutter wideimage"><img src="http://placehold.it/2400x500"></div>
<p class="xlarge-8 xlarge-offset-2 ">m dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id e</p>
</div>
</div>
https://jsfiddle.net/sLk0jf4L/835/
This works. I know it's a pain because you have to do the two rows, with the full width div between. But there are no gutter edges. Like so:
.wideimage{
width: 100%;
min-width: 100vh;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/foundation/6.0.0/foundation.min.css" rel="stylesheet"/>
<div class="row small-collapse">
<div class="medium-11 medium-centered column">
<p class="xlarge-8 xlarge-offset-2 ">m dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id e</p>
</div>
</div>
<div class="degutter wideimage"><img src="http://placehold.it/2400x500"></div>
<div class="row small-collapse">
<div class="medium-11 medium-centered column">
<p class="xlarge-8 xlarge-offset-2 ">m dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id e</p>
</div>
</div>

How to put two RichTextBlock(Overflow) one after another vertically in RichTextColumns

As mentioned, I want to put two RichTextBlocks one after another vertically. My intention is to write manually the paragraphs of the first RichTextBlock, and I want the second RichTextBlock (that its content given programmatically) to start right after the first one, and I want that to happen so that the second RichTextBlock will overflow vertically to the next column.
The below snippet intended just so you could see the objects I'm using and has no any logical meaning.
<ScrollViewer
Grid.Column="1"
Grid.Row="1"
Grid.RowSpan="2"
HorizontalScrollMode="Auto"
HorizontalScrollBarVisibility="Auto"
VerticalScrollMode="Disabled"
VerticalScrollBarVisibility="Disabled"
Padding="20">
<RichTextBlock>
<Paragraph><Run></Run></Paragraph>
<RichTextBlock />
<common:RichTextColumns>
<common:RichTextColumns.ColumnTemplate>
<DataTemplate>
<RichTextBlockOverflow Margin="80,0,0,0" Width="560">
<RichTextBlockOverflow.RenderTransform>
<TranslateTransform X="-1" Y="4"/>
</RichTextBlockOverflow.RenderTransform>
</RichTextBlockOverflow>
</DataTemplate>
</common:RichTextColumns.ColumnTemplate>
<RichTextBlock
htmlToXaml:Properties.Html="{Binding HtmlText}"
Name="RichTextBlockMy"
Margin="10,0,0,0"
Style="{StaticResource ItemRichTextStyle}"
Width="560"/>
</common:RichTextColumns>
</ScrollViewer>
I think this is what you want.
XAML
<ScrollViewer
HorizontalScrollMode="Auto"
HorizontalScrollBarVisibility="Auto"
VerticalScrollMode="Disabled"
VerticalScrollBarVisibility="Disabled"
Padding="20">
<StackPanel Orientation="Horizontal">
<RichTextBlock>
<Paragraph>
<Run Text="This is fixed RichTextBlock, after this it's RichTextBlock with RichTextColumns" FontSize="20"/>
<LineBreak />
<Run Text="{Binding FirstRtbText}" />
<LineBreak />
<Run Text="End of fixed RichTextBlock" FontSize="20" />
</Paragraph>
</RichTextBlock>
<common:RichTextColumns>
<common:RichTextColumns.ColumnTemplate>
<DataTemplate>
<RichTextBlockOverflow Margin="80,0,0,0" Width="560">
<RichTextBlockOverflow.RenderTransform>
<TranslateTransform X="-1" Y="4"/>
</RichTextBlockOverflow.RenderTransform>
</RichTextBlockOverflow>
</DataTemplate>
</common:RichTextColumns.ColumnTemplate>
<RichTextBlock
common:Properties.Html="{Binding HtmlText}"
Name="RichTextBlockMy"
Margin="10,0,0,0"
Style="{StaticResource ItemRichTextStyle}"
Width="560"/>
</common:RichTextColumns>
</StackPanel>
</ScrollViewer>
C#
public string HtmlText { get; set; }
public string FirstRtbText { get; set; }
public MainPage()
{
this.InitializeComponent();
FirstRtbText = #"<h1>Header One</h1>
<p>Lorem ipsum <em>emphasised text</em> dolor sit amet, <strong>strong text</strong>
consectetur adipisicing elit, <abbr title="""">abbreviated text</abbr> sed do eiusmod tempor
<acronym title="""">acronym text</acronym> incididunt ut labore et dolore magna aliqua. Ut
<q>quoted text</q> enim ad minim veniam, quis nostrud exercitation link text
ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute <del>deleted text</del>
<ins>inserted text</ins> irure dolor in reprehenderit in voluptate velit esse cillum
dolore eu fugiat nulla pariatur. Excepteur sint occaecat <code>code text</code> cupidatat
non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
<blockquote>
<p>Blockquote. Velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat
cupidatat non proident, sunt in culpa qui officia</p>
</blockquote>";
HtmlText = #"<h1>Header One</h1>
<p>Lorem ipsum <em>emphasised text</em> dolor sit amet, <strong>strong text</strong>
consectetur adipisicing elit, <abbr title="""">abbreviated text</abbr> sed do eiusmod tempor
<acronym title="""">acronym text</acronym> incididunt ut labore et dolore magna aliqua. Ut
<q>quoted text</q> enim ad minim veniam, quis nostrud exercitation link text
ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute <del>deleted text</del>
<ins>inserted text</ins> irure dolor in reprehenderit in voluptate velit esse cillum
dolore eu fugiat nulla pariatur. Excepteur sint occaecat <code>code text</code> cupidatat
non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
<blockquote>
<p>Blockquote. Velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat
cupidatat non proident, sunt in culpa qui officia</p>
</blockquote>
<p><cite>Cite author with link text</cite>, 2008</p>
<h2>Header 2</h2>
<p>Extended paragraph. Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud
exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in
reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint
occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
<ol>
<li>Ordered list</li>
<li>Item 2 Consectetur adipisicing elit</li>
<li>Item 3 Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor
incididunt ut labore et dolore magna aliqua</li>
<li>Item 4</li>
<li>Item 5</li>
</ol>
<p>Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla
pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit
anim id est laborum.</p>
<h3>Header 3</h3>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua.</p>
<ul>
<li>Unordered list</li>
<li>Consectetur adipisicing elit</li>
<li>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua</li>
<li>Item</li>
<li>Item</li>
<li>Item</li>
</ul>
<p>Lorem ipsum dolor sit amet,consectetur adipisicing elit, sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua.</p>
<pre><code>pre and code pair{
display:block;
line-height:1.833em;
border-top:0.083em solid #200;
}</code></pre>
<p>Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla
pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit
anim id est laborum.</p>
<h4>Header 4</h4>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua.</p>
<dl>
<dt>Definition list</dt>
<dd>Consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna
aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea
commodo consequat.</dd>
<dt>Lorem ipsum dolor sit amet</dt>
<dd>Consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna
aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea
commodo consequat.</dd>
<dt>Lorem ipsum dolor sit amet</dt>
<dd>Consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna
aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea
commodo consequat.</dd>
<dt>Lorem ipsum dolor sit amet</dt>
<dd>Consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna
aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea
commodo consequat.</dd>
</dl>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua.</p>
<table summary=""Table summary"">
<caption>Table Caption</caption>
<thead>
<tr>
<th>Header</th><th>Header</th><th>Header</th>
</tr>
</thead>
<tbody>
<tr>
<td>Content</td><td>1</td><td>a</td>
</tr>
<tr>
<td>Content</td><td>2</td><td>b</td>
</tr>
<tr>
<td>Content</td><td>3</td><td>c</td>
</tr>
<tr>
<td>Content</td><td>4</td><td>d</td>
</tr>
<tr>
<td>Content</td><td>5</td><td>e</td>
</tr>
<tr>
<td>Content</td><td>6</td><td>f</td>
</tr>
</tbody>
</table>
<address>Author text</address>
";
this.DataContext = this;
}

Here-Docs with if then else statement

Like the title says. Is it possible to use an if/then statement in a here-doc for ruby.
Thanks
Yes
irb(main):001:0> x = <<-E
irb(main):002:0" How
irb(main):003:0" now
irb(main):004:0" #{
irb(main):005:0" if true
irb(main):006:0" "brown"
irb(main):007:0" else
irb(main):008:0" "red"
irb(main):009:0" end
irb(main):010:0" } cow
irb(main):011:0" E
=> "How\nnow\nbrown cow\n"
irb(main):012:0> puts x
How
now
brown cow
Compare (and contrast) these samples. Which is more readable, has clearer logic, and would you want to support?:
Example #1:
string = <<END_STRING
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
#{
if (true_or_false)
'jackdaws love my giant sphinx of quartz'
else
'Sphinx of black quartz, judge my vow.'
end
}
proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem
ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor
incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis
nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore
eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt
in culpa qui officia deserunt mollit anim id est laborum.
END_STRING
Example #2:
string = <<END_STRING
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
#{
(true_or_false) \
? 'jackdaws love my giant sphinx of quartz' \
: 'Sphinx of black quartz, judge my vow.'
}
proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem
ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor
incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis
nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore
eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt
in culpa qui officia deserunt mollit anim id est laborum.
END_STRING
Example #3:
string = <<END_STRING
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
#{(true_or_false) ? 'jackdaws love my giant sphinx of quartz' : 'Sphinx of black quartz, judge my vow.'}
proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem
ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor
incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis
nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore
eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt
in culpa qui officia deserunt mollit anim id est laborum.
END_STRING
Example #4:
insert_string = (true_or_false) \
? 'jackdaws love my giant sphinx of quartz' \
: 'Sphinx of black quartz, judge my vow.'
string = <<END_STRING
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
${ insert_string }
proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem
ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor
incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis
nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore
eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt
in culpa qui officia deserunt mollit anim id est laborum.
END_STRING
Personally I prefer #4 because it separates the logic from the definition of the string, helping to keep it easier to see what is going on, but your mileage might vary.
A heredoc is just like a string. So if your string allows variable evaluation and substitution (using no quotes or double quotes), you can do something like this:
switcheroo=true
mydoc = <<HDOC
This string will always appear
#{"This will only appear if switcheroo is true" if switcheroo}
HDOC
You have to put your conditions inside the brackets, standard in-string evaluation technique.
Please also note that this won't work if the quoting for your heredoc uses single quotes, meaning it's a literal string.

Resources