Questions tagged [heredoc]

A Here-document is a special syntax of writing literal strings in sourcecode, used by different programming languages.

A heredoc or Here-Document is a way of writing literal strings in sourcecode. Many languages use heredocs: PHP, Perl, C#...

Although syntax can differ between programming languages, most heredocs start by putting a keyword, and end when the keyword is encountered as the only element on a line (no spaces, tabs or other characters are allowed). Perl example:

my $text = <<KEYWORD; #Any non-reserved word would do
This is some text.
It can contain $othervariables which will be replaced
Unless KEYWORD was between single quotes, in Perl.
KEYWORD
761 questions
28
votes
5 answers

HEREDOC interfering with code indentation

I like the HEREDOC syntax, e.g. for edge cases of generated HTML that are not worth putting into a template. The only thing that annoys me about it, though, is that the content, and the closing marker of a heredoc string adheres to the first…
Pekka
  • 442,112
  • 142
  • 972
  • 1,088
26
votes
1 answer

Formatting an array value inside a Heredoc

I was wondering why I can't do something like {number_format($row['my_number'])} inside a Heredoc. Is there any way around this without having to resort to defining a variable like $myNumber below? Looked at…
Kamil Sindi
  • 21,782
  • 19
  • 96
  • 120
24
votes
2 answers

How to avoid last \n character in heredoc

In ruby heredoc: a = <<~TEXT asd asd TEXT it will generate: [21] pry(main)> a = <<~TEXT [21] pry(main)* asd [21] pry(main)* asd [21] pry(main)* TEXT => "asd\n" + "asd\n" It generate a \n at the end of string, how to avoid this?
Moon soon
  • 2,616
  • 2
  • 30
  • 51
24
votes
3 answers

bash variable expansion ${var:+"..."} in here-document removing double quotes?

I'm trying to understand why Bash removes double quotes (but not single quotes) when doing variable expansion with ${parameter:+word} (Use Alternate Value), in a here-document, for example: % var=1 % cat < ${var:+"Hi there"} > ${var:+'Bye'} >…
Andreas Luik
  • 241
  • 1
  • 4
24
votes
6 answers

Simple/Direct/Heredoc way of constructing a HTML string in Java

In python I can construct a HTML string without worrying about escaping special characters like < or " by simply enclosing the string in triple quotes like: html_string = """

My text with "quotes" and…

das_weezul
  • 6,082
  • 2
  • 28
  • 33
22
votes
11 answers

JavaScript HERE-doc or other large-quoting mechanism?

Is there a convenient way to quote a large block of HTML that has both single and double quotes in JavaScript? Is there anything like a HERE-doc <
Evan Carroll
  • 78,363
  • 46
  • 261
  • 468
22
votes
1 answer

Is there heredoc alternative in Java (heredoc as PHP)?

For JAVA development I need writing to a files with strings like "\r\t\n <>", because from Java I want writing a PHP file. If you can't understand look at this example: BufferedWriter buffW = new BufferedWriter(fileW); …
Marin Sagovac
  • 3,932
  • 5
  • 23
  • 53
21
votes
3 answers

Working with large text snippets in Java source

Are there any good ways to work with blocks of text (Strings) within Java source code? Many other languages have heredoc syntax available to them, but Java does not. This makes it pretty inconvenient to work with things like tag libraries which…
Rob Hruska
  • 118,520
  • 32
  • 167
  • 192
21
votes
1 answer

Is nesting of Here Document possible in a unix bash script?

Is it possible to write a heredoc within another heredoc ? ssh -T -q yxz@server1 <<-"END_TEXT" . . ssh -T -q abc@server2 <<-"SUB_TEXT" . . SUB_TEXT . . END_TEXT
kaustav datta
  • 687
  • 2
  • 11
  • 31
20
votes
1 answer

What's the difference between "here string" and echo + pipe

Wondering what is the right use of here-string (here-document) and pipe. For example, a='a,b,c,d' echo $a | IFS=',' read -ra x IFS=',' read -ra x <<< $a Both methods work. Then what would be the difference between the two functionality? Another…
dragonxlwang
  • 462
  • 5
  • 13
20
votes
2 answers

How to use a PHP heredoc as an array value?

I've a larger piece of multi-line text that I need to put in an PHP associative array through a here-doc. It looks like this: $data = [ "x" => "y", "foo" => "bar", /* ... other values ... */ "idx" => <<< EOC data data…
bodacydo
  • 75,521
  • 93
  • 229
  • 319
19
votes
5 answers

How to pipe a here-document through a command and capture the result into a variable?

Right now this outputs the value I need on stdout. How can I capture it into a variable so I can use it in the rest of the script? Requirements: The script needs to be all in one file. I'd prefer not to write any temp files, if…
Mark Renouf
  • 30,697
  • 19
  • 94
  • 123
17
votes
6 answers

JavaScript Multiline String

The question is: What is the JavaScript method to store a multiline string into a variable like you can in PHP?
luca
  • 36,606
  • 27
  • 86
  • 125
16
votes
8 answers

Dirt-simple PHP templates... can this work without `eval`?

Update- Thanks for all the responses. This Q is getting kind of messy, so I started a sequel if anyone's interested. I was throwing together a quick script for a friend and stumbled across a really simple way of doing templating in PHP. Basically,…
Dagg Nabbit
  • 75,346
  • 19
  • 113
  • 141
1 2
3
50 51