views:

66

answers:

6

Heya!

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}</title>

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!

A: 

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>
Col. Shrapnel
fyi i hear <?= is gone in php in v6.
Galen
@Galen They are just deprecating them, but they don't plan on removing them. It's just to gently suggest to use the longtag version. But the majority of people seem to use shorttags in templates.
ryeguy
@Galen i hear it will not.
Col. Shrapnel
anyone know about php.net link discussing this? i'd like to hear an official answer.
Galen
See http://www.mail-archive.com/[email protected]/msg41868.html and http://wiki.php.net/rfc/shortags
Bill Karwin
A: 

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

Galen
Newbtophp
@Newbtophp if it's just parsing exercise, why so much concern in code format?
Col. Shrapnel
A: 

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.

jcubic
Interesting - I'll await what others think and adapt the syntax accordingly.
Newbtophp
WHAT do you mean - "evaluate"?
Col. Shrapnel
A: 

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 :)

methodin
Looks pretty cool!
Newbtophp
Found the templating engine I was thinking of: http://www.twig-project.org/
methodin
Twig also has the benefit of much better sandboxing than Smarty. Even when I'm using Smarty for application templates, I use Twig for anything user provided.
Jeff Standen
A: 

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.

duality_
A: 

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.

steven_desu