Search code examples
phpcompilationphpstormintellij-14string-substitution

Replacing info in HTML page using PHP before rendering


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!)


Solution

  • 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.