开发者

how can i use brackets intead of full code in php

开发者 https://www.devze.com 2023-02-12 17:40 出处:网络
suppose i have a variable in a seperate php file i.e $imgfile = \"images/img.jpg\"; 开发者_开发问答

suppose i have a variable in a seperate php file i.e

$imgfile = "images/img.jpg";
开发者_开发问答

now i have a php file where i am including a html or another php file i.e

<?php 
include("foo.html");
?>

and in foo.html i have the following code..

<html>
<head>
<title>foo site</title>
</head>
<body>
<img src="<?php echo $imgfile; ?>">

and it is works but i am including that $imgfile many times so i want not to type <?php echo $imgfile; ?> again and again.. i have seen many scripts that include such files by just typing {$imgfile} but i don't know how to use it please let me know how can i use such a format..??


PHP is a template engine already.
so, it has shorter form for echo statement, especially for this purpose:

<?=$imgfile?>

considerable shorter and comparable to {$imgfile}

Note that to make use of brackets, you'll have to devise alternatives for loops, conditions and other statements, which will complicate your life. while using PHP as a template, you'll be able to use built-in PHP operators, like foreach or if or include.

So, it would be better to stick to <?=$imgfile?> syntax.

Just make sure you have short_open_tags setting turned on


I think Smarty, a templating engine, is what you are looking for. See this link: http://smarty.net/


In order to achieve this behavior you should use a template engine like Smarty or write your own interpreter that will replace such expressions with the appropriate values.

e.g.

$buffer = 'Hello {$world}';
$world = "World";

if(preg_match_all("/{([^}]+)}/im", $buffer, $matches, PREG_SET_ORDER)) {
    foreach($matches as $match) {
        $expression = $match[0];
        $exactMatch = $match[1];

        if(defined($exactMatch)) {
            $buffer = str_replace($expression, constant($exactMatch), $buffer);
        } else {


            if(strrpos($exactMatch, "$") !== false) {
                $vars = get_defined_vars();
                $var = str_replace("$", "", $exactMatch);
                if(isset($vars[$var])) {
                    $buffer = str_replace($expression, $vars[$var], $buffer);
                }
            }

            if(is_callable($exactMatch)) {
                $buffer = str_replace($expression, call_user_func($exactMatch), $buffer);
            }
        }
    }
}

echo $buffer;


There are three options for you here.

1) assign <img src="<?php echo $imgfile; ?>"> to a shorter string

 $a = "<img src='$imgfile'>";

Then in your templates

 <html>
 <head>
 <title>foo site</title>
 </head>
 <body>
 <img src="<?php echo $imgfile; ?>">

2) Use a placeholder then buffer and post process your output.

In your templates

 <?php echo ob_start('myReplacementCallback') ?>
 <html>
 <head>
 <title>foo site</title>
 </head>
 <body>
 {{imgfile}}
 <?php ob_end_flush (); ?>

Define myReplacementCallback somewhere:

function myReplacementCallback($contents) {
    $replacements = array(
        '{{imgfile}}' => "<img src='/path/to/image'>",
    );

    return str_replace(array_keys($replacements), $replacements, $contents);
}

3) Use a template engine like twig or smarty. (Prefered method)

0

精彩评论

暂无评论...
验证码 换一张
取 消