Equivalent to PHP HereDoc?

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
hairydalek
Posts: 57
Joined: Sat Feb 01, 2014 8:57 pm

Equivalent to PHP HereDoc?

Post by hairydalek » Wed Apr 20, 2016 10:09 am

Hi,
I’m wondering if there’s an equivalent to PHP’s HereDoc funcitonality in LIveCode. In PHP I can do something like this:

Code: Select all

$fruit="apples";

$myOutput = <<<END
I really like $fruit
END;
echo $myOutput;
The output from that would be

Code: Select all

I really like apples
The above is really simple, but you can do some quite complex things with it - heredoc will process any variable names within between the END and END; and insert their values into the string.

In the case of LiveCode, I’m using shell scripts to process images, and I’m having to do stuff like this:

Code: Select all

put convertPath into myCommand
put myCommand && "image.png" && myWidth && myHeight && "image2.png" into myCommand
But more complex than that. My immediate thoughts would be to include placeholders for variables, and so search & replace to replace those with the real variables, which would make my scripts easier to read (right now, adding variables in like I do above doesn’t help with readability).

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10043
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: Equivalent to PHP HereDoc?

Post by FourthWorld » Wed Apr 20, 2016 3:57 pm

We don't currently. but I've come to enjoy them in some recent bash scripting in which I needed to mix in some Expect. I like the idea of having that in LC, so I submitted a request for it:
http://quality.livecode.com/show_bug.cgi?id=17471

Currently, for in-line strings we have only concatenation, e.g.:

Code: Select all

put "World" into tStr
put quote&"Hello "& tStr&"!"&quote into fld 1
Useful, but not nearly as elegant as arbitrary in-line blocks.

When I need more text than I care to write concatenation expressions for, I tend to store those in custom properties or in files on disk. Custom props are usually more convenient; I have only one project where external editing of such templates made using files a good fit.

Not quite as nice as heredoc, but hopefully custom properties will at least get you moving forward for now, and with any luck we may see a heredoc-like structure in LC not too far down the road.

Tip: see the merge function in the Dictionary. It makes working with text templates a snap, allowing you to drop in placeholders for variable and function names for quick automated replacement.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

Post Reply