echo php code without commenting out php code -
echo php code without commenting out php code -
let me rephrase question. i'm trying store contents of html
file string. have php
code in html
string convert php whatever values feed later. think string interpolation might work. might have on complicated it. think interesting still able utilize php
tags situations.
i this:
$str = 'some words'; $php = '<p><?= $str; ?></p>'; echo $php;
which output dom
(source):
<p>some words</p>
or on browser screen
some words
what get:
<p><!-- <?= $str; ?> --></p>
is possible?
i know code above looks simple simple case of problem trying solve.
<?php // view class view { private static $paths = []; private static function gettemplate($templatepath) { if (!array_key_exists($templatepath, self::$paths)) { // filename must exist if (!file_exists($templatepath)) { die('file doesn\'t exist'); } self::$paths[$templatepath] = file_get_contents($templatepath); } homecoming self::$paths[$templatepath]; } // fill template public static function fill($vars, $templatepath) { // turn vars unique variables extract($vars); $input = self::gettemplate($templatepath); // view output ob_start(); echo $input; $output = ob_get_contents(); ob_end_clean(); homecoming $output; } public static function fillwith($templatepath) { } }
use string interpolation:
echo "<p>$str</p>";
note double-quoted syntax ("..."
). variables not substituted in php if string single-quoted ('...'
).
if obtain template string external source, php's string interpolation won't work (it huge security risk if would).
you may utilize regular expressions replace occurrences of specific patterns.
or utilize template engine twig. has more powerfulness need , requires learning, it's future-proof in case need more complex features sometime.
you may create template file php script , include
(instead of file_get_contents
). + define variables before inclusion. php's usual string interpolation work. but not recommend way. it's not readable , introduces potential security risk.
see this question.
php
Comments
Post a Comment