Need some advice/suggestions on template syntax
I've coded a small php template parser, so templates can be easily parsed, and the variables within the templates are like {variable_name}
e.g.
<title>{title}</tit开发者_StackOverflowle>
Can you suggest me possible if/else statement syntax?
I've thought of doing something like:
{if {logged_in}: TRUE}
You're logged in...
{else}
You're not...
{/if}
and...
{if {logged_in}: TRUE}
You're logged in...
{/if}
The above demonstrates basic if/else template syntax (it checks if the variable logged_in == true), but since I'm more of a coder then designer, was wondering if I can have your input (so designers can easily understand the syntax without knowledge of server-side coding).
Cheers!
Sure.
Here it is:
<?if ($logged_in):?>
You're logged in...
<?else:?>
You're not...
<?endif?>
Best syntax ever.
As you can see, opening and closing tags been changed slightly.
But no need to parse anything at all - just include
this file!
Btw, your output would look like
<title><?=$title?></title>
I recommend this:
<title><?php echo $title ?></title>
<?php if( $logged_in ): ?>
You're logged in...
<?php else: ?>
You're not...
<?php endif; ?>
<?php if( $logged_in ): ?>
You're logged in as <?php echo $logged_in_username ?>
<?php endif; ?>
In other words no template engine at all
Unless you're doing it as a learning experience don't bother with a template engine, just use straight PHP
Typically conditionals differ in syntax from the variable replacement since they could have a variable called $else etc...
{% if logged_in == TRUE %}
You're logged in...
{% else %}
You're not...
{% endif %}
Makes sense. Then you can follow some standards and replace == with >= <= != etc... without a huge new syntax to learn :)
Yes, why wouldn't you want to use something like Smarty, which is a mature templating system? I use it all the time and it's perfect.
But if you still want to do it your way, I think the developers are the ones who you should be asking, not designers. Designers usually just hand you the html files (sometimes just a Photoshop file) and don't want to be bothered with variables, control structures, etc. And developers want and need complex statements inside IFs, like functions and logical statements.
Your syntax looks logical, though and you can use it.
To anyone who said that you should avoid a template system, there are a multitude of advantages to template. For instance, suppose you make a huge feature-rich suite (forum software, profiles, dynamic user content, the works) and you want people to be able to edit the appearance of the information. If you give them direct PHP-based access, they could very easily break something and make your script no longer compile. They could also inject PHP to get at variables hidden within your code or to completely bring your software to its knees. Using templates limits what they can do and therefore it limits what they can break.
I haven't done a lot of work with templates, but in my experience any expressions usually have a different encapsulation from variables. So for instance:
<!-- IF {value_which_should_be_true} -->
value_which_should_be_true is true! Oh, and {information}
<!-- ELSE -->
value_which_should_be_true wasn't true... oh, and {different_information}
<!-- ENDIF -->
This way you can just use preg_replace for \{([A-Za-z0-9_-]+)}\ with the value of $values[$1] ($1 being the match from the preg_replace) for the values and the only thing that you have to exec() is inside of the comment blocks.
in braces you can put expressions which be evaluated
{if logged_in == TRUE}
You're logged in...
{else}
You're not...
{endif}
When you parse template you can evaluate expressions in bracess
{if foo == 10 + bar }
do something
{else}
do something else
{endif}
You may also consider to change single braces to double because it's less likely to appear in your template file.
精彩评论