Reputation: 1798
i want to include many lines of HTML code, only if a certain condition in PHP is true.
<?
if ($var == 1){
WriteTheHTML:
}
?>
... <b> This is only displayed if condition is fulfilled </b> ...
<? StopWritingTheHTML; ?>
I just don't want to have multiple files. Also, i want to avoid writing the HTML code in PHP echo.
Is there a (similar) solution?
Upvotes: 0
Views: 247
Reputation: 2039
If you wanna keep it clean you should use a "template engine". There are really small ones - see mine:
Template engine:
class TemplateEngine {
/**
* Contains the Templatecontent
*
* @access private
* @var array
*/
private $TEMPLATE_VARS = array();
/**
* Contains the Blockdiretory
*
* @access private
* @var string
*/
private $BLOCK_DIR='';
/**
* @param string $pos Set the position in the Template
* @param string $cont Set the content at this position
*/
public function assign($pos, $cont = null) {
$this->TEMPLATE_VARS[$pos] = $cont;
}
/**
* @param string $tpl_file Set the Maintemplate
*/
public function display($tpl_file) {
if (!empty($tpl_file)) {
if (file_exists($tpl_file)) {
$TPL = $this->TEMPLATE_VARS;
include($tpl_file);
} else {
echo "File doesnt exist";
echo $tpl_file;
}
} else {
echo "File is empty";
}
}
/**
* @param string $pos Set the position in the Template
* @param array $cont Set the content at this position (mostly used with arrays to fill the blocks)
*/
public function content($pos, $cont = null) {
for ($i = 0; $i < count($cont); $i++) {
$this->TEMPLATE_VARS[$pos][$i] = $cont[$i];
}
}
}
?>
The template (template.tpl.php):
<!DOCTYPE html>
<!--[if lt IE 7]> <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]> <html class="no-js lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]> <html class="no-js lt-ie9"> <![endif]-->
<!--[if gt IE 8]><!-->
<html class="no-js" xmlns="http://www.w3.org/2000/svg" xmlns:svg="http://www.w3.org/2000/svg" xml:lang="<?php echo $_SESSION["language"] ?>" lang="<?php echo $_SESSION["language"] ?>"> <!--<![endif]-->
<head>
<meta charset="utf-8" /><meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<title><?php echo $TPL['title'] ?></title>
<meta name="description" content="<?php
if (isset($TPL['description'])) {
echo $TPL['description'];
}
?>"/>
<meta name="keywords" content="<?php
if (isset($TPL['keywords'])) {
echo $TPL['keywords'];
}
?>"/>
<meta name="viewport" content="width=device-width"/>
<!-- Mobile viewport optimisation -->
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=0"/>
<meta name="HandheldFriendly" content="true" />
<meta name="apple-touch-fullscreen" content="yes" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<meta http-equiv="Cache-control" content="public"/>
<!--Load Scripts-->
<?php
foreach ($TPL['headElements'] as $element)
echo $element . PHP_EOL
?>
</head>
<body>
<!--[if lt IE 10]>
<p class="chromeframe">You are using an <strong>outdated</strong> browser. Please <a href="http://browsehappy.com/">upgrade your browser</a> or <a href="http://www.google.com/chromeframe/?redirect=true">activate Google Chrome Frame</a> to improve your experience.</p>
<![endif]-->
<!--include blocks-->
<?php
foreach ($TPL['blocks'] as $element)
include $element;
?>
<!-- Load footersection-->
<?php
foreach ($TPL['footElements'] as $element)
echo $element . PHP_EOL
?>
</body>
</html>
And now you can include blocks into that template by doing following:
$tpl = new TemplateEngine;
$tpl->assign("title", "Your title");
//Blocks
$tpl->assign("blocks", array(PATH . "/web/blocks/content/registerPage.php", PATH . "/web/blocks/footerWrap.php"));
$tpl->assign("register", PATH . "/web/blocks/register.php");
$tpl->assign("footElements", returnFooter());
$tpl->display(PATH . "/web/template.tpl.php");
Upvotes: 0
Reputation: 2764
Try this way :
<?php if ($var == 1){ ?>
... <b> This is only displayed if condition is fulfilled </b> ...
<?php }else{ ?>
... <b> This is only displayed if condition is fulfilled </b> ...
<?php } ?>
Upvotes: 1