Is it possible to replace php tags to something custom?
Smarty library can assign values and then render them so instead of:
<?php echo $name; ?>
you can just assign value in business logic and then in template just say
{name}
I'd like to make my own function/class/library so when I write { in index.tpl, browser will think its < ?php echo ..
Can somebody point me somewhere?
replace {variable} with regexps and then eval-it
here is simple template engine, very similar to your idea
i mean, better is use existing templates engines, like Latte, Twig or Smarty. Its not necessary reinventing wheel :)
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>
<? } ?>
I'm looking for something that Is really hard for me to do.. I really tried to search all over the net for Solution, But I couldn't seem to find any. I also tried doing this for hours.
What I'm doing: Making a theme for PHPBB2, Installed a MOD that can include PHP in themes.
What is the problem: When I'm doing {} tags in php, It just can't echo those tags.
Let's say I have a function that creates a Table for me, like that:
CreateMyTable(Name,Size,Color);
I put in the function those strings:
CreateMyTable("{FORUM_NAME}",1000,red);
The title stays blank, I actually want it to echo {FORUM_NAME}.
How can I do this?
P.S: I can't do this
CreateMyTable(?>{FORUM_NAME}<?php , 1000, red);
It's not going to work becuase <? = <!-- PHP --> , ?> = <!-- ENDPHP -->.
Thanks for your help :)
If you look in the PHPbb2 template class, you'll find that the template is simply an evaluated set of PHP using the eval() function. You can either print the contents of the PHP before it is parsed using eval() and then use the variable name that the template gives, IE something like (which may not work depending how your template is setup):
CreateMyTable(((isset($this->_tpldata['.'][0]['FORUM_NAME'])) ? $this->_tpldata['.'][0]['FORUM_NAME'] : '' ),1000,randomcolor());
Please note, in order to do it similar to the way above you'd actually have to insert this into your template class.
An much better solution is to avoid using the mod that allows PHP in templates and use JavaScript in the templates to create the function, then print a call to that JavaScript function.
This will work:
CreateMyTable(FORUM_NAME,1000,red);
I also noticed that red is used without quotes - is this also a constant? If it's a variable it needs to have a $ in front of it. If it's a string it should be between quotes.
CreateMyTable(FORUM_NAME,1000,"red");
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
Is there any way to use preg_match or any other similar smarty function to do the following thing:
{foreach $resultsdata as $resultsData}
<div class="site_text">
{$resultsData.text|substr:0:500}
</div>
{/foreach}
I want to filter $resultsData.text and display only one part of this text.
I'm asking because there's a plugin you may use. But it's only for Smarty 3, as I see.
By the way, from your code it seems preg_replace would be a better solution, and it's already internal function - regex_replace - for it.
You are taking idea of templates wrong.
Template should be used only to display data.
But data itself have to be prepared in the business logic part.
Otherwise there will be no good from templates at all.
Whats the best way to create my own template engine using php, to transfer html templates into actual websites, and replacing the placeholders with the actual data... well, let me solve my own question...
class Template{
$private $output = '';
public function Load_Template($template){
ob_start();
include($template);
$this->output = ob_get_clean();
}
public function Replace($data){
$this->output = str_replace(array_keys($data), array_values($data), $this->output);
}
public function Display($add_footer = true){
echo $this->output;
}
}
This would work for a simple Template like...
<div>{username}</div>
But what would be the best way to do it with loops in my template.
Lets say something like
<ul>
<li>{username}</li>//Loop this line for each user
</ul>
Also, I dont want to use a third party engine like smarty, I would just like to know how to do it myself. Thank you
How about use php itself?
<div><?php echo $username ?></div>
<ul>
<?php foreach($users as $user): ?>
<li><?php echo $user->username ?></li>
<?php endforeach ?>
</ul>
*note: Funky endforeach syntax explained here.
Honestly, if I had to whip up my own template engine in PHP, I'd just use PHP. Just restrict yourself to only using variable interpolation and loops within the "templates".
Writing your own template engine is a tricky thing. You're inventing an entirely new language, after all, albeit a simple one. For it to be very powerful (i.e., beyond simple string replacement), you'd probably have to write an actual stack-based parser rather than rely on str_replace.
You could do this with regular expressions, much as you could parse HTML with regular expressions, but it won't be very reliable, easy to read, easy to debug, or easy to extend.
You mention that you don't want to use a third-party engine like Smarty, but you are basically setting out to do the exact same thing that Smarty is doing.
Something that people often overlook when talking about templating in PHP is that PHP is a templating language. There's no reason you can't just use regular old PHP inside your templates. In fact, that's what it was originally designed for.
If you really want to build your own templating engine, then you're going to have to sit down and design a syntax for looping and whatever other templating structures you'll want to use. Something like [do 5 times] { loop code }. Make up whatever you want. Then you'll have to parse it with PHP, and modify the template to replace it with the looped structure.
It's a big job... using normal PHP would be much easier.
Well, most of the answer to your question is preg_replace_callback().
I agree with everyone in this discussion - I have never understood the use of proprietary languages for templating in PHP - you know PHP so just use that.
If you are disciplined (e.g. follow MVC architectural pattern) in keeping a clear difference between PHP scripts that do things, and PHP that display things, there won't be any problems.
A couple of ways that can help are calling your PHP template files .phtml, and using alternative PHP syntax which is a little easier to maintain in templateswhich contain both HTML and PHP:
<?php if ( $a == $b ) : ?>
<strong>True</strong>
<?php else: ?>
<strong>False</strong>
<?php endif; ?>