0

EDIT: After typing this out I think this approach is a non starter. All I am trying to do is to find an easier way to type <?= and ?> in longish HTML pages. Any ideas greatly appreciated.
LATER: Yep this is a non-starter so I am tweaking my IDE (the wonderful PhpStorm) to take care of this and will probably start using Twig "soon".

==============================

I find endless typing of <?= => an absolute nightmare. For some reason I really cannot remember position of the keys on keyboard and get confused on the shift/nonshift (brain aged 64 and/or too much cannabliss in my youth?) and have to search for them every time and do things like just now I typed a + instead of a =!

I don't want to use a full-on templating engine like Moustache yet but just want to replace the <?= and => with something like {{{ }}}.

To experiment I am using jjjj and kkk.

To try and do this I put the whole HTML page into a HEREDOC variable called $mypage. At present there is only one replacement target but this will obviously go into a loop if I can get it working.

  $start = strpos($myPage, "jjjj");
  $end = strpos($myPage, "kkk");

  substr_replace($myPage,"<?=$",$start,4);
  substr_replace($myPage,"?> ",$end, 3);

  $insName = "Boo";


  echo $myPage;

In the HTML I have:

<label for="clientFirstName">First name* jjjjinsNamekkk</label>

The echo works fine and displays the page properly but the strings are not replaced. Worse than that I think I may be just completely wrong.

If I replace the jjjj in the body with <?=$ will that be interpreted or just echoed as text to the screen?

Also given that I tend to use includes quite a lot would they work in this echo/HEREDOC strategy? I suspect not.

Grateful for any suggestions.

PS Yes I know this is all a terrible way of doing things but this is for an alpha for LITERALLY about 10 people so I am just doing a quick and dirty. (Which to be honest is the only way I program these days!)

BeNice
  • 2,165
  • 2
  • 23
  • 38
  • Of course this will only get displayed - you are working on _data_ here, not _code_. If you wanted to get that data interpreted as code again, you would have to send it through `eval` (which is not that recommendable - go read up on it, if you don’t know why.) – 04FS Nov 04 '19 at 14:13
  • 1
    `Twig` template engine. – u_mulder Nov 04 '19 at 14:17
  • @u_mulder looked at Twig, just had a second look might well be within my reach but at present not understanding the install on remote server but suspect not too horrible. Thanks for the reminder – BeNice Nov 04 '19 at 15:14

1 Answers1

1

Your (edited) question is the sort of thing your IDE or enhanced editor would usually do for you. From what I can glean, it appears you use PHPStorm. You could use a Live Template for this. There are some pre-defined ones for PHP but I don't think what you're after is there.

For example, to create one to insert the whole of <?= ?>

  • Type <?=$END$ ?> into the editor ($END$ positions the cursor)
  • Highlight the above text
  • ctrl-shift-a / cmd-shift-a (as appropriate for your operating system)
  • Type 'save as' into the popup and select 'Save as Live Template'
  • Enter a suitable abbreviation, e.g. 'pie' (PHP Inline Expression)
  • Edit template text if necessary
  • Click Apply

Thereafter you can type 'pie' followed by the tab key into the editor to insert the text.

redbirdo
  • 4,937
  • 1
  • 30
  • 34
  • 1
    I swear I will forget my own liver soon! I am having a look at Twig again - may have a go at that tonight but the beloved PhpStorm once again comes to the rescue. .... PAUSE .... It embarrasses me to say that I already had a Live Template (`<<` was my chosen abbreviation with the string `=$END$?>`). I still would prefer the straightforward substitution for readability if nothing else. PS I had not some across the 'Save as Live Template' before - very useful. I do have one last wish - for the template to be `=$ ?>` with the `$` escaped. but I cannot make that work. – BeNice Nov 04 '19 at 15:09
  • 1
    See https://intellij-support.jetbrains.com/hc/en-us/community/posts/207036935-Escaping-Dollar-Sign-in-Live-Template – redbirdo Nov 04 '19 at 15:57