I wants to know if someone have an idea of how can we call a variable inside html code like if we are using the smarty template.
<?php
$var = "sometext";
?>
<html>
<body>
<?php echo $var ?>
</body>
</html>
If we are using smarty template we can do like this:
<?php
$var = "sometext";
?>
<html>
<body>
{$var}
</body>
</html>
Is that possible without using smarty templates? Thank you for help!
You can't use Smarty syntax in 'naked' PHP... that's why it's Smarty syntax.
You have three options:
Use PHP's builtin syntax, which is either <?php echo $var; ?> or <?= $var; ?> (there are good reasons not to use the second, mostly that it's difficult to comment out in PHP. You have to use HTML comments which leave remnants of PHP in your HTML source.)
Write your own templating engine which parses your template files, you can use preg_match to search for a pattern like /\{(.*?)\}/ and replace them with the variables, but beware that code replacement causes complexities. If you want to go this route, look into output buffering and regex.
Use Smarty or another templating engine you like. These libraries exist, so take advantage of them.
Note: in PHP 5.4 or newer, even if short tags are disabled, the short echo tag will work. Therefore, as long as you're not running unsupported old versions of PHP, you don't have to worry about access to it.
Related
I do not like spitting back html with php's echo, makes it hard to do and read nested elements. So I usually write conditions that write raw html and make it as readable as possbile when editing the file directly or viewing the output html through the browser. However, I cannot find a style that stays readable for long. Any suggestions?
<?
if($foo == $bar)
{
?>
<div>
<p>hello, world</p>
</div>
<?
}
?>
As you can see, it doesn't look too good. At least not to me, but it makes the browser output more readable so I can better check the it for any mistakes.
i dont want this:
<?
if($foo == $bar)
{
echo "<div>\n\t<p>hello, world</p>\n</div>\n";
}
?>
Is my approach incorrect to begin with? should I use php to output to a .html file? and just view from the browser for mistakes and do as much php as possible inside the php file?
Your right it's not nice, instead use proper alternative syntax, for content with a large amount of HTML:
<?php if($foo == $bar): ?>
<div>
<p>hello, world</p>
</div>
<?php endif ?>
I don't believe you need a seperate template language to write maintainable code, PHP is perfectly fine in outputting variables in HTML.
Really the problem is seperating your logic from your output which a template engine can't help with if your not structuring your code properly in the first place. For example stuffing it all in an index.php file or not using MVC whereas you don't put HTML with your logic.
If you have a large project or are overly concerned with separating your PHP from your views, or you want the features which come with a template engine like built in caching and slots etc, then use one. But maybe first look at learning a framework which will improve your overall codebase as most frameworks come with their own. Though essentially you can achieve the same thing including nesting partials and blocks/slots with a 20 line view class which uses ob_* functions, which doesn't require you to learn a new syntax.
Rant over.. :s
Use a templating system like Smarty so you can separate your logic from your display code.It also allows you to bring in a designer that can work with html and might not know php at all. Smarty templates read more like HTML than PHP and that can make a big difference when dealing with a designer.
Ansered by #boatcoder
You can learn here
Without the use of templating engine, your best bet is #Lawrence's answer or this littly modified syntax of your first exemple (trimminig space and php tag) :
<? if($foo == $bar) { ?>
<div>
<p>hello, world</p>
</div>
<? } ?>
Ay guys,
I do know two possibilites to display PHP in HTML:
<?php function(); ?> or the shorter method <?= function(); ?>
But I often see something like {METHOD} or {OUTPUT} in the HTML part of bigger scripts f.e.:
<div class="test">{OUTPUT}</div>
In my opinion this is a way tidier. Could somebody tell me more about this?
I have used php to generate html using the echo function and have used php inside html too.(If this clarifies your doubt in anyway)
echo $projectname; inside html file tags
echo the html file
Cheers :)
When echoing data in HTML without a template engine, short tags are preferred as they look cleaner, and are easier to read. They're great when using control structures too: http://php.net/manual/en/control-structures.alternative-syntax.php
For short tags to work short_open_tag needs to be enabled.
The example you shown with the curly brackets is usually specific to a template engine such as Twig.
you can use this method only with the function ECHO with double quotes :
1 - this works
$name = 'Mark';
echo "<div>GoodMorning {$name}</div>";
2 - this does not work
$name = 'Mark';
echo '<div>GoodMorning {$name}</div>';
All of the text on my page has to come from a translation file, so my markup is littered with stuff like <?php echo $translation; ?>.
For just one or two calls, it's fine, but when it's all over the page, it makes things awfully verbose. Is there a more concise way to include PHP variables in the markup? I can echo a heredoc, but then it removes all the IDE features for HTML editing like automatic indentation and highlighting.
Is there a way to just do something like {$translation} instead of <?php echo $translation ?>?
You can do
<?=$translation?>
If you have activated short_open_tag in your php.ini
You could use a template engine like smarty or twig. Smarty for example is supported in an IDE like netbeans.
Use a template language, like twig, it has builtin support for internatiolzation with a simple syntax.
If you want to keep "pure" PHP, you can use <?=$translation?>, or you can use a template engine, like Smarty. There are tons of those, just find one that suits you.
This should help you http://en.wikipedia.org/wiki/Template_engine_%28web%29
In my website I have certain preset variables such as the page title, year/time, and etc.
For example in my title I would use
<title> <?php echo $TITLE; ?> </title>
However, how would I make it so typing something like..
<title> [TITLE] </title>
Will produce the same result?
It's recommended that you use the PHP tags for many reasons, including performance and stability. If you want to do less typing you could use the shorthand for echoing variables: <?= $TITLE ?>.
If you're using a PHP version prior to 5.4 you might have to enable short_open_tags in your configuration. After version 5.4 these tags are always available
If you're dead set on using a different syntax for your HTML, you could look into a templating language like Smarty or Dwoo
$page = str_replace('[TITLE]', $title, $page);
Replace $page with your variable. Obviously then you'd have to echo out $page to get it to show.
That effect can be accomplished with template tags in a template engine such as Smarty.
Use a PHP templating language like Mustache or Smarty.
PHP vs template engine might point you towards some other options.
I am an MVC addict.
I have some views in plain PHP (not using any Templating Engine (TE) such as Smarty for getting good performance), but without Smarty my views are looking ugly and hard to code.
I'm wondering how can I make them look good (ie., human readable) without using any TE? At least I want to replace those <?php and ?> for sure, or if you have any other better idea?
It's just a start of my web application so please guide me to any better alternative if you have any.
Thanks.
In templates I use the alternatives of PHP like:
<?php if (true): ?>
<?php endif ?>
<?php foreach ($x as $y): ?>
<?php endforeach ?>
etc.
http://php.net/manual/en/control-structures.alternative-syntax.php
There's no PHP variation or syntax that allows you to replace <?php and ?> with { and }. If you want that, you'll need to use or write a custom parser for it, which means you're looking at a templating engine. No templating engine, no custom syntax.