HTML ve PHP değerlendirir Eval

4 Cevap php

Ben çiftleşmiş ile karıştırmasını yaşıyorum ve ben tarayıcıya html içeren bir şablon yankı gerekir ve bir durum içine çalıştırmak php. Nasıl PHP değerlendirmek ve tarayıcıya gönderebilirim?

Yani burada bir örnek (main.php) bulunuyor:

	<div id = "container">
		<div id="head">
			<?php if ($id > 10): ?>
				<H3>Greater than 10!</H3>
			<?php else: ?>
				<H3>Less than 10!</H3>
			<?php endif ?>
		</div>
            </div>

Ve sonra template.php in:

   <?php
           $contents; // Contains main.php in string format
           echo eval($contents); // Doesn't work... How do I do this line??
   ?>

EDIT: My template also allows you to inject data from the controller Smarty-style. Would an output buffer allow me to do this and then evaluate my php. The ideal is that it does a first-pass through the code and evaluates all the tags in first, then runs the php. This way I can create loops and stuff from using data sent from my controller.

So maybe a more complete example: 
    <div id = "container">
            <div id = "title">{$title}</div> <!-- This adds data sent from a controller -->
            <div id="head">
                <?php if ($id > 10): ?>
                    <H3>Greater than 10!</H3>
                <?php else: ?>
                    <H3>Less than 10!</H3>
                <?php endif ?>
            </div>
    </div>

Teşekkürler!

4 Cevap

(Ben gibi, bir veritabanı gibi) durumda karışık HTML / PHP bir dize ile bunu yapmak için çalışıyoruz, bunu bu şekilde yapabilirsiniz:

eval(' ?>'.$htmlandphp.'<?php ');

Daha çok bilgi: http://blog.5ubliminal.com/posts/eval-for-inline-php-inside-html/ (bu 2014/03/03 itibariyle ölü bağlantı olduğunu unutmayın)

Yerine çıktı tamponlama kullanıyorsanız. eval() bildiği yavaş.

main.php:

<div id="container">
    <div id="title"><?php echo $title; ?></div><!-- you must use PHP tags so the buffer knows to parse it as such -->
    <div id="head">
    	<?php if ($id > 10): ?>
    		<H3>10 Büyüktür!</H3>
    	<?php else: ?>
    		<H3>Less than 10!</H3>
    	<?php endif ?>
    </div>
</div>


Sizin dosyası:

$title = 'Lorem Ipsum';
$id = 11;

ob_start();
require_once('main.php');
$contents = ob_get_contents();
ob_end_clean();

echo $contents;

Bu çıkış olacaktır:

Lorem Ipsum

10 Büyüktür!

Dosyayı okumak, ama bunu eklemek ve sonucu yakalamak için çıktı bufferig kullanmayın.

ob_start();
include 'main.php';
$content = ob_get_clean();

// process/modify/echo $content ...

Edit

Yeni bir değişken kapsamı oluşturmak için bir işlevi kullanın.

function render($script, array $vars = array())
{
    extract($vars);

    ob_start();
    include $script;
    return ob_get_clean();
}

$test = 'one';
echo render('foo.php', array('test' => 'two'));
echo $test; // is still 'one' ... render() has its own scope

Sizin durumda en iyi çözüm birleştirmek için eval ve output buffer

// read template into $contents
// replace {title} etc. in $contents
$contents = str_replace("{title}", $title, $contents);
ob_start();
    eval(" ?>".$contents."<?php ");
$html .= ob_get_clean();
echo html;